Skip to content

Project Euler Problem 45

November 26, 2011

Project Euler Problem 45 solution:

// Positive root of x*(3*x - 1)/2 = y is x = (1 + sqrt(1 + 24*y))/6
// Positive root of x*(2*x - 1) = y is x = (1 + sqrt(1 + 8*y))/4

open System

let sqrtn n = n |> float |> sqrt |> int64

let makePentagonal n =
    n*(3L*n - 1L)/2L

let isPentagonal x =
    x = makePentagonal ((1L + sqrtn(24L*x + 1L))/6L)

let makeHexagonal n =
    n*(2L*n - 1L)

let isHexagonal x =
    x = makeHexagonal ((1L + sqrtn(8L*x + 1L))/4L)

let makeTriangle n =
    n * (n + 1L) / 2L

let problem045 () =
    seq { 286L .. Int64.MaxValue }
    |> Seq.skipWhile (fun x -> (not (isPentagonal(makeTriangle(x))))
                            || (not (isHexagonal(makeTriangle(x)))))
    |> Seq.head
    |> fun x -> makeTriangle x
Advertisements

From → Project Euler

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: