Last active
March 5, 2017 12:22
-
-
Save smcl/cab4baa25f22624a51df9fa1bd69123b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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