Fortran Implementation of Problem 16
View source code here on GitHub!
- integer Problem0016/p0016()
1! Project Euler Problem 16
2!
3! Problem:
4!
5! 2**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
6!
7! What is the sum of the digits of the number 2**1000?
8
9module Problem0016
10 use constants
11 implicit none
12contains
13 pure integer(i18t) function p0016() result(answer)
14 integer(i18t), dimension(18) :: numbers
15 integer(i18t) :: power
16 integer :: i, j
17
18 numbers = 0
19 numbers(1) = 1
20 do i = 1, 1000
21 do j = 1, size(numbers)
22 numbers(j) = numbers(j) * 2
23 end do
24 do j = 1, size(numbers) - 1
25 if (numbers(j) > ten17) then
26 numbers(j + 1) = numbers(j + 1) + numbers(j) / ten17
27 numbers(j) = mod(numbers(j), ten17)
28 end if
29 end do
30 end do
31 answer = 0
32 power = 1
33 do i = 1, 18
34 do j = 1, size(numbers)
35 answer = answer + mod((numbers(j) / power), 10_i18t)
36 end do
37 power = power * 10
38 end do
39 end function p0016
40end module Problem0016