r/dailyprogrammer • u/rya11111 3 1 • Feb 23 '12
[2/23/2012] Challenge #14 [intermediate]
Your task is to implement the sieve of Sundaram and calculate the list of primes to 10000.
this is also an interesting article about it.
18
Upvotes
3
u/prophile Feb 23 '12
Python:
2
u/foggylucidity Feb 23 '12
C:
2
u/drb226 0 0 Feb 24 '12 edited Feb 24 '12
Who says Haskell doesn't have mutable state? Just contain it to the scope where it belongs with the ST monad!
http://hpaste.org/64258
It's quite straightforward to write an array-based imperative algorithm in Haskell; you just have to know a few things like
forM_and read/write/new ref/array.Notice my algorithm uses:
And you can just change the type signatures from Int to Int32 to squeeze even more juice out of it, assuming the compiler doesn't notice this optimization by itself. Or change the signature to Integer, and implement a correct
isqrt, and you can get infinite precision for arbitrarily large primes.1
u/luxgladius 0 0 Feb 23 '12
Perl
1
u/DLimited Feb 23 '12
D2.058:
1
u/PrivatePilot Feb 23 '12
ruby
1
u/robin-gvx 0 2 Feb 23 '12
Déjà Vu: http://hastebin.com/raw/wafuhawose
It segfaults or worse for n > 1337, which is one hell of a coincidence. And also really frustrating.
1
u/lukz 2 0 Feb 23 '12
Common Lisp
For info, I got 1228 primes.
1
u/agph Feb 23 '12
Haskell
1
u/_autumn Feb 24 '12
I tried a messy Haskell solution.
1
u/funny_falcon Feb 24 '12
Another Ruby:
1
u/cooper6581 Feb 24 '12
Javascript:
http://jsfiddle.net/py3Qv/