[Fortran] 纯文本查看 复制代码
program pi_chudnovsky
use fmzm
implicit none
type(im) :: L, L0, X, X0, K
type(fm) :: M, C, t, P1, P2, error
integer(kind=4) :: i, n
character(:), allocatable :: str
character(20) :: fmt, tmp1, tmp2
print *, "Input required number of digits = "
read(*,*) n
write(tmp1, *) n + 3
write(tmp2, *) n - 3
write(fmt, *) "F"//trim(adjustl(tmp1))//"."//trim(adjustl(tmp2))
call fm_set(n)
allocate(character(n) :: str)
L = to_im('13591409')
L0 = to_im('545140134')
X = 1
X0 = to_im('-262537412640768000')
K = 6
M = to_fm('1')
C = to_fm('426880') * sqrt(to_fm('10005'))
t = to_fm(L)
P1 = C / t
error = to_fm(10)**(-n)
i = 0
do
i = i + 1
M = M * to_fm((K * K - 16 ) * K) / to_fm(i)**3
K = K + 12
L = L + L0
X = X * X0
t = t + M * L / X
P2 = C / t
if(abs(P2 - P1) < error) exit
P1 = P2
end do
call fm_form(fmt, P2, str)
write(*,*) "Pi= ", str
end program pi_chudnovsky