-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem50.fsx
33 lines (25 loc) · 1022 Bytes
/
problem50.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(* Project Euler Problem 50
* By Weisi Dai <[email protected]>
*)
open Weisi.Dai.Algorithms
let ubound = 1000000 // one million
let primes = PrimesUpTo ubound
let isPrime x =
None <> BinarySearch 0
(1 + primes.GetUpperBound 0)
(fun sub -> Compare x primes.[sub])
let findFrom (sum, count) left =
let primesIncludingLeft = Array.filter ((<=) left) primes
let rec findTo (sum, count) index current =
if index > primesIncludingLeft.GetUpperBound 0 then (sum, count) else
let newSum = current + primesIncludingLeft.[index]
if newSum > ubound then (sum, count) else
let newCount = index + 1
if newCount > count && isPrime newSum then
findTo (newSum, newCount) (index + 1) (newSum)
else
findTo (sum, count) (index + 1) (newSum)
findTo (sum, count) 0 0
let problem50 = Array.fold findFrom (0, 0) primes |> fst
let main() = printfn "The answer is %d." (problem50)
main()