-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise_15.f90
51 lines (47 loc) · 1.81 KB
/
Exercise_15.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
! Created by EverLookNeverSee@Guthub on 06/18/20
! I could not have done it without @ivan-pi
module continued_fractions
implicit none
private ! makes this module private
! makes these entities accessible from outside of the module
public cf, pi_seq
! Simple continued fraction expansion of Pi taken from https://oeis.org/A001203
integer, parameter :: pi_seq(20) = [3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, &
14, 2, 1, 1, 2, 2, 2, 2]
contains
recursive function cf(b) result(pq)
! Returns the *simple* continued fraction of the sequence b
! declaring dummy parameters and local variables
integer, intent(in) :: b(:)
real :: pq(2)
real :: pq_n1(2), pq_n2(2)
integer :: n
n = size(b)
! conditions to satisfy `Euler-Wallis` recurrence relations
if (n > 2) then
pq_n2 = cf(b(1:n-2))
pq_n1 = cf(b(1:n-1))
pq = b(n)*pq_n1 + pq_n2
else if (n == 2) then
pq_n1 = cf(b(1:1))
pq(1) = b(2)*pq_n1(1) + 1.0
pq(2) = b(2)
else if (n == 1) then
pq(1) = b(1)
pq(2) = 1.0
end if
end function cf
end module continued_fractions
program main
use continued_fractions, only: cf, pi_seq
implicit none ! disabling implicit type assignment feature
! declaring and initializing variables and constants
real :: result, pq(2)
real, parameter :: PI = 4.0 * atan(1.0)
! calling function and calculating continued fraction
pq = cf(pi_seq)
result = pq(1) / pq(2)
! printing results
print *, "Continued fraction result:", result
print *, "PI exact value:", PI
end program main