Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created December 19, 2008 14:56
Show Gist options
  • Save cbilson/38018 to your computer and use it in GitHub Desktop.
Save cbilson/38018 to your computer and use it in GitHub Desktop.
#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