Fortran Implementation of Problem 5

View source code here on GitHub!

Includes

Problem Solution

integer Problem0005/p0005()
 1! Project Euler Problem 5
 2!
 3! I modeled this after the C++ solution, because it was by far the simplest
 4!
 5! Problem:
 6!
 7! A palindromic number reads the same both ways. The largest palindrome made from
 8! the product of two 2-digit numbers is 9009 = 91 × 99.
 9!
10! Find the largest palindrome made from the product of two 3-digit numbers.
11
12module Problem0005
13    use constants
14    use primes
15    implicit none
16contains
17    integer function p0005() result(answer)
18        integer(i2t), dimension(20) :: factor_tracker, local_factor_tracker
19        integer(i18t) :: p, q
20        integer :: i, j
21        answer = 1
22        factor_tracker = 0
23        local_factor_tracker = 0
24        do i = 2, 20
25            q = i
26            do while (q > 1)
27                call prime_factor(q, p)
28                local_factor_tracker(p) = local_factor_tracker(p) + 1_i2t
29            end do
30            do j = 2, 19
31                factor_tracker(j) = max(factor_tracker(j), local_factor_tracker(j))
32                local_factor_tracker(j) = 0
33            end do
34        end do
35        do i = 2, 19
36            do j = 1, factor_tracker(i)
37                answer = answer * i
38            end do
39        end do
40    end function p0005
41end module Problem0005

Tags: fortran-iterator, divisibility, factorization, prime-number