gpt4 book ai didi

algorithm - 用于计算 Pi 的 Bellard 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:19 25 4
gpt4 key购买 nike

对于一个大学项目,我想在 Fortran 中实现 Bellard 的算法来计算圆周率的第 n 位。我在 math.stackexchange 上偶然发现了这个问题:https://math.stackexchange.com/questions/1776840/confusion-with-bellards-algorithm-for-pi

有了那个问题的答案,我设法实现了代码,但我没有得到结果,我也不知道我做错了什么:

PROGRAM pi

IMPLICIT NONE

INTEGER :: find_number_of_primes, number_primes, last_digit, digit, N, &
eps, i, j, multiplicity, test
REAL :: log2, base, v_max, m, s, v, b, A, pi_sum
INTEGER, ALLOCATABLE :: primes(:)

digit = 3
eps = 2
base = 10
N = INT((digit+eps)*log2(base))
pi_sum = 0

number_primes = find_number_of_primes(2*N)
ALLOCATE(primes(number_primes))
CALL get_primes(number_primes, primes)

DO i=1,SIZE(primes)
v_max = INT(log(REAL(2*N))/log(REAL(primes(i))))
m = primes(i)**v_max
s = 0
v = 0
b = 1
A = 1
DO j = 1,(N)
b = MOD((j/(primes(i)**multiplicity(digit, j)) * b), m)
A = MOD((2*j-1)/(primes(i)**multiplicity(primes(i), (2*j-1)))*A, m)
v = v - multiplicity(primes(i),j)+multiplicity(primes(i),(2*j-1))
IF (v > 0) THEN
s = MOD(s*j*b*(A**(-1))*a**(v_max-v), m)
END IF
END DO
s = MOD(s * base**(digit-1), m)
pi_sum = pi_sum + MOD((s/m), REAL(1))
END DO

PRINT*, pi_sum

END PROGRAM

自定义函数 find_number_of_primesget_primeslog2multiplicity 正在按预期工作。第一个找到给定区间内素数的个数,第二个返回数组中的素数,第三个计算

log_2(x) = log(x)/log(2) 

最后一个通过检查第二个数字除以第一个数字直到除法的其余部分不再为零来计算多重性(我想这就是所谓的)。以下是源代码:

对数:

real function log2(x)
implicit none
real, intent(in) :: x

log2 = log(x) / log(2.)
end function

求区间内素数的个数:

integer function find_number_of_primes(limit) result(number_primes)
implicit none
INTEGER, INTENT(IN) :: limit
INTEGER :: i, j
LOGICAL :: is_prime

number_primes = 0

DO i = 3,(limit-1)
is_prime = .TRUE.
DO j = 2, (i-1)
IF (MODULO(i, j) == 0) THEN
is_prime = .FALSE.
EXIT
END IF
END DO
IF (is_prime .EQV. .TRUE.) THEN
number_primes = number_primes + 1
END IF
END DO
end function find_number_of_primes

获取实际素数:

SUBROUTINE get_primes(number_primes, primes)
IMPLICIT NONE
INTEGER, INTENT(IN) :: number_primes
INTEGER :: i, found_primes, j
LOGICAL:: is_prime
INTEGER, INTENT(OUT) :: primes(number_primes)
i = 3
found_primes = 0
DO
is_prime = .TRUE.
DO j = 2, (i-1)
IF (MODULO(i,j) == 0) THEN
is_prime = .FALSE.
END IF
END DO
IF (is_prime .EQV. .TRUE.) THEN
found_primes = found_primes + 1
primes(found_primes) = i
IF (found_primes == number_primes) EXIT
END IF
i = i + 1
END DO


end SUBROUTINE get_primes

integer function multiplicity(a, b)
implicit none
INTEGER, INTENT(IN) :: a, b

multiplicity = 0
DO
multiplicity = multiplicity + 1
IF (MOD(b, (a**(multiplicity+1))) /= 0) THEN
EXIT
END IF
END DO
end function multiplicity

整个文件的 Pastebin:https://pastebin.com/0F4zQYaH

现在在我链接的问题中,在内部循环中计算 b 时,在 a^{v(n, k)} 中分母,但在问题的答案中,分母中的项是 a^{v(a, k)}。此外,问题中的内部循环从 1N,而答案中的循环从 12N.

最终结果是 NaN,这是 digit = 2eps = 1 的一些控制台输出:

************ i =           1 ***************************
v_max = 2.00000000
m = 9.00000000
------------- j = 1 -------------------------------
b = 0.00000000
A = 0.00000000
v = 0.00000000
------------- j = 2 -------------------------------
b = 0.00000000
A = 0.00000000
v = 0.00000000
------------- j = 3 -------------------------------
b = 0.00000000
A = 0.00000000
v = 0.00000000
------------- j = 4 -------------------------------
b = 0.00000000
A = 0.00000000
v = 0.00000000
------------- j = 5 -------------------------------
b = 0.00000000
A = 0.00000000
v = 1.00000000
v > 0 --> s = NaN
------------- j = 6 -------------------------------
b = 0.00000000
A = 0.00000000
v = 1.00000000
v > 0 --> s = NaN

等等,完整的输出:https://pastebin.com/ucJNg6Vi

谁能帮我弄清楚我做错了什么?

最佳答案

您的代码中存在错误:

v = v - multiplicity(primes(i),j)+multiplicity(primes(i),(2*j-1))

应该是

v = v - multiplicity(primes(i),j)- multiplicity(primes(i),(2*j-1))

此外,

        s = MOD(s*j*b*(A**(-1))*a**(v_max-v), m)

应该是

        s = MOD(s + j*b*(A**(-1))*a**(v_max-v), m)

关于algorithm - 用于计算 Pi 的 Bellard 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52835997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com