Skip to content

Project Euler Problem 44

November 26, 2011

Project Euler Problem 44 solution:

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

open System

let upperBound = 20000

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

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

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

let pentagonals: int array = Array.zeroCreate upperBound

let mutable result = Int32.MaxValue
let mutable delta = 0

let problem044 () =
    for i = 0 to upperBound - 1 do
        pentagonals.[i] <- makePentagonal (i+1)
        for j = 0 to i - 1 do
            if isPentagonal(pentagonals.[i] + pentagonals.[j]) &&
                  isPentagonal(pentagonals.[i] - pentagonals.[j]) then
                delta <- pentagonals.[i] - pentagonals.[j]
                if result > delta then
                    result <- delta
    result
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: