$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 [1]:
for i=0:9
    println(i, " ",factorial(i))
end


0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880

In [7]:
addprocs(7)
function brute(m)
    @parallel for i=10:m
        s = 0
        n = i
        while i != 0
            s += factorial(mod(i, 10))
            i = div(i, 10)
        end
        if s == n
            println("x -> ", s)
        end
    end
end
@time brute(9999999)


  0.012744 seconds (17.55 k allocations: 1.286 MB)
Out[7]:
14-element Array{Any,1}:
 RemoteRef{Channel{Any}}(13,1,26)
 RemoteRef{Channel{Any}}(14,1,27)
 RemoteRef{Channel{Any}}(15,1,28)
 RemoteRef{Channel{Any}}(2,1,29) 
 RemoteRef{Channel{Any}}(3,1,30) 
 RemoteRef{Channel{Any}}(4,1,31) 
 RemoteRef{Channel{Any}}(5,1,32) 
 RemoteRef{Channel{Any}}(6,1,33) 
 RemoteRef{Channel{Any}}(7,1,34) 
 RemoteRef{Channel{Any}}(8,1,35) 
 RemoteRef{Channel{Any}}(9,1,36) 
 RemoteRef{Channel{Any}}(10,1,37)
 RemoteRef{Channel{Any}}(11,1,38)
 RemoteRef{Channel{Any}}(12,1,39)
	From worker 13:	x -> 145
	From worker 13:	x -> 40585

In [ ]:
vcat([1, 2], [3, 4])

In [8]:
145+40585


Out[8]:
40730

In [ ]: