From ee4185b3d8ced6af8053fd3def51d97dcce56ff4 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Wed, 1 Sep 2021 10:52:53 +0200 Subject: [PATCH] Solution to problem 34 in Julia --- src/Julia/Problem034.jl | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/Julia/Problem034.jl diff --git a/src/Julia/Problem034.jl b/src/Julia/Problem034.jl new file mode 100644 index 0000000..d5088e9 --- /dev/null +++ b/src/Julia/Problem034.jl @@ -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())