Skip to content

Project Euler Problem 30

November 21, 2011

Project Euler Problem 30 is about one of these cute number artifacts. It can be noticed that as numbers grow there is a limit to number of digits representable as sum of fifth powers of digits. Max number representable for given number of digits nd is nd*(9**5).

As soon as the upper limit is known the rest is straightforward: for each number between 10 (lowest number for which sum of digits is defined) and the upper limit check if it is equal to sum of its fifth powers of digits; if affirmative, add it to the answer:

let maxAsSumOfDigitsPow5 =
    let ninePow5 = 9*9*9*9*9
    let rec maxN (n: int) =
        if  pown 10 n > ninePow5 * n then
            ninePow5 * n
        else
            maxN (n + 1)
    maxN 2

let asSumOfDigitsPow5 (n: int) =
    Seq.unfold (fun x ->
        if x = 0 then None else Some(pown (x%10) 5, x/10) ) n
    |> Seq.sum

let problem030 () =
    {10..maxAsSumOfDigitsPow5}
    |> Seq.filter (fun x -> x = asSumOfDigitsPow5 x) |> Seq.sum
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: