You're replying to a comment by Shlomi Fish.

November 07, 2009, 08:22

Hi Peteris!

Thanks for this post.

A few notes about your solutions:

1. Your primality check may be short but it has awful complexity:

shlomi:~$ time factor 5612912
5612912: 2 2 2 2 53 6619
0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+242minor)pagefaults 0swaps
shlomi:~$ time perl -le '$_=shift; (1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is prime"' 5612912
2.76user 0.02system 0:02.79elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+3919minor)pagefaults 0swaps
shlomi:~$

See this post to Hackers-IL.

perl -MList::Util=sum -alne 'push @S,@F END { print sum @S }'

You do realise you are constructing an incredibly large array (and arrays in Perl have a lot of memory overhead), and then summing it all at once? Furthermore you are missing a semicolon between "@F" and "END":

shlomi:~$ perl -MList::Util=sum -alne 'push @S,@F END { print sum @S }'
Bareword found where operator expected at -e line 1, near "@F END"
        (Missing operator before END?)
syntax error at -e line 1, near "@F END "
syntax error at -e line 1, near ";}"
Execution of -e aborted due to compilation errors.

I suggest the following instead:

perl -MList::Util=sum -alne '$s += sum @F; END { print $s }'

Though one should consider using a big numbers package here.

I'm posting what I have now so I'll be sure the comment will be OK. (Got no comment preview after all).

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "sandbox_165": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.