Skip to content

Instantly share code, notes, and snippets.

@smcl
Last active March 5, 2017 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcl/cab4baa25f22624a51df9fa1bd69123b to your computer and use it in GitHub Desktop.
Save smcl/cab4baa25f22624a51df9fa1bd69123b to your computer and use it in GitHub Desktop.
// attempt 2 - use recursion, threading the index and current triangle number to each call
// less memory intensive, shouldn't be too terrible on stack (tail recursion)
// but the "countFactors" function sticks out as a little nasty
//
// run in f# jupyter notebook, or using mono (fsharpc prob12.fs && mono prob12.exe)
let minFactors = 5
// let minFactors = 500
let isFactor x y = (x % y = 0UL)
let countFactors n =
[ 1UL.. 1UL + (n/2UL) ]
|> List.filter (isFactor n)
|> List.length
let rec prob20 n triangle =
if (countFactors triangle) >= minFactors
then triangle
else prob20 (n+1UL) (triangle+n+1UL)
prob20 1UL 1UL
// |> printfn "%u" // for printing to stdout when running in cmdline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment