Skip to content

Instantly share code, notes, and snippets.

@monkeygroover
Last active March 30, 2016 21:00
Show Gist options
  • Save monkeygroover/41e367beab92ee1d099c to your computer and use it in GitHub Desktop.
Save monkeygroover/41e367beab92ee1d099c to your computer and use it in GitHub Desktop.
euler14
//n → n/2 (n is even)
//n → 3n + 1 (n is odd)
let collatzSeq = Seq.unfold (fun n -> match n with
| 0L -> None
| 1L -> Some(1L, 0L)
| n -> Some(n, if n % 2L = 0L then n/2L else 3L*n+1L) )
let r = {1L..999999L} |> Seq.map(fun i -> (i, Seq.length(collatzSeq(i)))) |> Seq.maxBy(fun (i,s) -> s)
printfn "%A" r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment