Calculate the sum of all the primes below two million.
Using the Sieve of Eratosthenes sequence developed earlier we can write the solution like this:
sieve 2L Map.empty |>
Seq.takeWhile (fun n -> n < 2000000L) |>
Seq.sum |>
printfn "sum = %A"
Arguably there is some inefficiency in generating a subsequence just consisting of the primes less than two million and then summing that sequence. I think the clarity of the resulting code makes up for the slight inefficiency.
No comments:
Post a Comment