Created
December 19, 2008 14:56
-
-
Save cbilson/38018 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
#light | |
(* | |
Problem 12: Deep Thought Edition | |
*) | |
open System | |
open System.Collections.Generic | |
open Microsoft.FSharp.Math | |
let N = | |
Seq.unfold (fun x -> Some(x, x + 1I)) 1I | |
let triangleNumbers = | |
N |> Seq.map (fun x -> Seq.take_while (fun y -> y <= x) N | |
|> Seq.sum) | |
let sqrtI = BigInt.ToDouble >> sqrt >> ceil >> string >> BigInt.Parse | |
let factors n = | |
let max = sqrtI n | |
Seq.take_while (fun x -> x <= max) N | |
|> Seq.filter (fun x -> n % x = 0I) | |
|> Seq.map (fun x -> match n with | |
| _ when x = max -> (x, x) | |
| _ -> (x, n / x)) | |
let firstTriangleNumberWith_n_Divisors n = | |
triangleNumbers | |
|> Seq.first (fun x -> let fs = factors x | |
match Seq.length (fs) with | |
| y when y >= n / 2 -> Some(x, fs) | |
| _ -> None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment