Solution to problem 34 in Julia
This commit is contained in:
parent
6ec7ed6537
commit
ee4185b3d8
42
src/Julia/Problem034.jl
Normal file
42
src/Julia/Problem034.jl
Normal file
@ -0,0 +1,42 @@
|
||||
#=
|
||||
Created on 01 Sep 2021
|
||||
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 34 of Project Euler
|
||||
https://projecteuler.net/problem=34
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
|
||||
function Problem34()
|
||||
#=
|
||||
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.
|
||||
=#
|
||||
|
||||
ans = 0
|
||||
|
||||
for num in 10:2_540_160
|
||||
sum_of_factorial = 0
|
||||
for digit in reverse(digits(num))
|
||||
sum_of_factorial += factorial(digit)
|
||||
if sum_of_factorial == num
|
||||
ans += sum_of_factorial
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return ans
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem 34:")
|
||||
@btime Problem34()
|
||||
println("")
|
||||
println("Result for Problem 34: ", Problem34())
|
Loading…
x
Reference in New Issue
Block a user