145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.


In [13]:
open System.Numerics

let bigOne = BigInteger(1)
let bigTwo = BigInteger(2)

let rec factorial n = 
    if n <= 1 then bigOne
    else BigInteger(n) * (factorial (n - 1))
    
let facs = 
    [| 0 ..9 |]
    |> Array.map (fun n -> factorial(n))
    
let sumFactorial n =
    n.ToString().ToCharArray()
    |> Array.map (string >> int)
    |> Array.map (fun i -> facs.[i])
    |> Array.sum
    
let equalToSumFactorial n =
    let sumFac_n = (sumFactorial n)
    n = sumFac_n
    
seq { 3 .. 10000000 }
|> Seq.map (fun i -> BigInteger(i))
|> Seq.filter equalToSumFactorial
|> Seq.sum


Out[13]:
40730

In [ ]: