-
Notifications
You must be signed in to change notification settings - Fork 1
/
p3.f90
51 lines (43 loc) · 1.04 KB
/
p3.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function is_prime(n) result(prime)
implicit none
integer, parameter :: big = selected_int_kind(13)
integer(big), intent(in) :: n
logical :: prime
integer(big) :: m
prime = .true.
m = 2
do while (m**2 <= n .and. prime)
if (mod(n,m) == 0) then
prime = .false.
end if
m = m + 1
end do
end function is_prime
function largest_prime_factor(n) result(factor)
implicit none
integer, parameter :: big = selected_int_kind(13)
integer(big), intent(in) :: n
integer(big) :: factor
logical :: is_prime
integer(big) :: m
integer(big) :: d
m = n
d = 2
do while (d <= m)
if (mod(m,d) == 0) then
m = m / d
factor = d
else
d = d + 1
do while (.not. is_prime(d))
d = d + 1
end do
end if
end do
end function largest_prime_factor
program project_euler_3
implicit none
integer, parameter :: big = selected_int_kind(13)
integer(big) :: largest_prime_factor
print *, 'Largest prime factor: ', largest_prime_factor(600851475143_big)
end program project_euler_3