Fortran Implementation of Problem 34
View source code here on GitHub!
Includes
Problem Solution
- integer Problem0034/p0034()
1! Project Euler Problem 34
2!
3! This ended up being a filtering problem. The problem with my solution is that I
4! am not satisfied with my filter at all. I feel like there is a more efficient
5! way to go about it.
6!
7! Problem:
8!
9! 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
10!
11! Find the sum of all numbers which are equal to the sum of the factorial of
12! their digits.
13!
14! Note: as 1! = 1 and 2! = 2 are not sums they are not included.
15
16module Problem0034
17 use constants
18 use math
19 implicit none
20contains
21 pure integer(i18t) function p0034() result(answer)
22 integer :: i, j
23 integer(i18t) tmp
24 character(len=5) string
25
26 answer = 0
27 do i = 10, 99999
28 tmp = 0
29 write(string, '(I0)') i
30
31 do j = 1, len_trim(string)
32 tmp = tmp + factorial(ichar(string(j:j)) - ichar('0'))
33 end do
34
35 if (tmp == i) then
36 answer = answer + i
37 end if
38 end do
39 end function p0034
40end module Problem0034