-
Notifications
You must be signed in to change notification settings - Fork 1
/
p50.fs
46 lines (38 loc) · 1.16 KB
/
p50.fs
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
43
44
45
46
// Utilities
let rec mapseqInternal f output input =
match input with
| [] -> output
| head :: tail -> (mapseqInternal f ((f (head :: tail)) :: output) tail)
let mapseq f sequence = Seq.rev (mapseqInternal f [] sequence)
// Prime sieve
let max = 1000000
let composites =
[2..max]
|> Seq.map (fun n -> seq { for i in 2..(max / n) -> i * n })
|> Seq.concat
let primes =
[2..max]
|> Seq.except composites
let primeSet =
primes
|> Set.ofSeq
// Find longest consecutive prime sum under the limit
let rec primeSumUnderInternal sum count best sequence =
match sequence with
| [] -> best
| head :: tail ->
let newSum = (sum + head)
let newCount = (count + 1)
if newSum > max
then best
else (primeSumUnderInternal newSum newCount (if Set.contains newSum primeSet then (newSum, newCount) else best) tail)
let primeSumUnder sequence =
primeSumUnderInternal 0 0 (0, 0) sequence
// Start at each index and find the longest
let solution =
primes
|> List.ofSeq
|> mapseq primeSumUnder
|> Seq.maxBy (fun (sum, count) -> count)
|> fun (sum, count) -> sum
printfn $"{solution}"