12 Jul 2008
Problem’s quite simply stated this time:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
First typed in solution was simply:
[2..1999999] |> Seq.filter(isprime) |> Seq.fold1(+)
Which got me a number pretty quickly that looked lovely, but was completely wrong. I ran it again, apparently hoping that’d it magically be right the second time. Then I tried for those below 1000000 instead and the sum was negative. Oops! So, the quick fix and working version (changing the “int” range to “bigint” range):
[2I..1999999I] |> Seq.filter(fun x -> isprime(int x)) |> Seq.fold1(+)
I wonder if there’s a checked-overflow-arithmetic version of F#? Might be useful for some of these questions, since they’re so computationally light anyway.