-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem35.fsx
42 lines (34 loc) · 1.12 KB
/
problem35.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
41
42
(* Project Euler Problem 35
* 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 allRotations x =
let rotate (str: string) n =
let left = str.[ .. str.Length - n - 1]
let right = str.[str.Length - n .. ]
right + left
let str = x.ToString()
[| 0 .. str.Length - 1 |]
|> Array.map (rotate str)
|> Array.map int
let isCircle x =
allRotations x
|> Array.fold (fun status element ->
(status && (Set.contains element primes)))
true
let problem35 = primesArr
|> Array.filter isCircle
|> Array.length
let main = printfn "The answer is %d." (problem35)
main