-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem14.fsx
37 lines (27 loc) · 857 Bytes
/
problem14.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
(* Project Euler Problem 14
* By Weisi Dai <[email protected]>
*)
let ubound = 1000000
let collatz = function
| x when x % 2L = 0L -> x / 2L
| x -> 3L * x + 1L
(*
* I wanted to use hash maps to store all past results to
* accelerate future computation. Not really needed.
*
* See ``problem14-map.fsx''.
*)
let distance x =
let rec dist current = function
| 1L -> current + 1
| x -> dist (current + 1) (collatz x)
dist 0 x
let seqDistancePair = seq { for i in 1 .. ubound do yield (i, distance (int64 i)) }
let findMax (curMaxKey, curMaxValue) (key, value) =
if value > curMaxValue then
(key, value)
else
(curMaxKey, curMaxValue)
let (problem14, _) = Seq.fold findMax (1, 1) seqDistancePair
let main = printfn "The answer is %d." (problem14)
main