-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem37.fsx
40 lines (32 loc) · 1.11 KB
/
problem37.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
34
35
36
37
38
39
40
(* Project Euler Problem 37
* By Weisi Dai <[email protected]>
*)
let ubound = 1000000
let primesUpTo n =
let rec sieve sub (current: int[]) =
if current.[sub] > (n |> float |> sqrt |> int)
|| sub > (current.GetUpperBound 0) then current else
current
|> Array.filter (fun x -> (x % current.[sub] <> 0)
|| (x <= current.[sub]))
|> sieve (sub + 1)
sieve 0 [| 2 .. n |]
let primesArr = primesUpTo ubound
let primes = primesArr |> Set.ofArray
let isPrime x = Set.contains x primes
let truncable x =
let rec leftTruncable x =
let str = x.ToString()
if not <| isPrime x then false else
if str.Length = 1 then true else
leftTruncable (int str.[1 .. ])
let rec rightTruncable x =
if not <| isPrime x then false else
if x < 10 then true else
rightTruncable (x / 10)
(x >= 10) && (leftTruncable x) && (rightTruncable x)
let problem37 = primesArr
|> Array.filter truncable
|> Array.sum
let main = printfn "The answer is %d." (problem37)
main