You're viewing a comment by Daniel and its responses.
You're viewing a comment by Daniel and its responses.
I am being sponsored by Syntress! They bought me an amazing dedicated server to run catonmat on. If you're looking web services, I highly recommend the Syntress guys!
I love to read science books. They make my day and I get ideas for awesome blog posts, such as Busy Beaver, On Functors, Recursive Regular Expressions and many others.
Take a look at my
Amazon wish list, if you're curious about what I have planned reading next, and want to surprise me. :)
If you are interested in advertising on catonmat.net, contact me.
Free tools for coding on Vietstarsoft.com.
Programming homework help.


Hi Peter,
The prime number one liner with the regexp is very interesting and surprisingly short.
I'm not a perl programmer, but at first glance I thought the regexp solution would be very slow (because the "base 1" representation would take a lot of space) and I thought I could write something slightly longer, but faster.
So, I came up with this thing:
perl -MList::Util=max -lne 'foreach (max(2,max(keys %p)+1)..$_) {$i=$_;scalar(grep(($i % $_) == 0, keys %p)) == 0 and $p{$i}=1}; $p{$_} and print "$_ is prime"'which is more like two full lines on my terminal, but uses a set of already found primes (%p) and only does computations if the input line contains a number larger than all previous (so it should be better, right?..).
Well, guess what -- when testing with larger numbers, your regexp returned results instantly while my code needed 5 seconds for 10000 and about half a minute for 20000!..
So, maybe in Perl shorter does mean faster, too.
Reply To This Comment