Python 寻找一种无限生成圆周率数字的方法
请纠正我,如果这是一个愚蠢的和不可能的问题,但有没有办法让脚本不断生成圆周率的数字?例如,脚本可以生成一个圆周率数字,然后将其存储在.txt文件中,然后重复,或者只是打印出它生成的数字。这是使用迭代方法Python 寻找一种无限生成圆周率数字的方法,python,numbers,pi,data-generation,Python,Numbers,Pi,Data Generation,请纠正我,如果这是一个愚蠢的和不可能的问题,但有没有办法让脚本不断生成圆周率的数字?例如,脚本可以生成一个圆周率数字,然后将其存储在.txt文件中,然后重复,或者只是打印出它生成的数字。这是使用迭代方法 #!/usr/bin/env python3 # https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py # Find PI to the Nth Digit # Have the user e
#!/usr/bin/env python3
# https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py
# Find PI to the Nth Digit
# Have the user enter a number 'n'
# and print out PI to the 'n'th digit
def calcPi(limit): # Generator function
"""
Prints out the digits of PI
until it reaches the given limit
"""
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
decimal = limit
counter = 0
while counter != decimal + 1:
if 4 * q + r - t < n * t:
# yield digit
yield n
# insert period after first digit
if counter == 0:
yield '.'
# end
if decimal == counter:
print('')
break
counter += 1
nr = 10 * (r - n * t)
n = ((10 * (3 * q + r)) // t) - 10 * n
q *= 10
r = nr
else:
nr = (2 * q + r) * l
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
def main(): # Wrapper function
# Calls CalcPi with the given limit
pi_digits = calcPi(int(input(
"Enter the number of decimals to calculate to: ")))
i = 0
# Prints the output of calcPi generator function
# Inserts a newline after every 40th number
for d in pi_digits:
print(d, end='')
i += 1
if i == 40:
print("")
i = 0
if __name__ == '__main__':
main()
#/usr/bin/env蟒蛇3
# https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py
#找到第n位的PI
#让用户输入一个数字“n”
#并将圆周率打印到第n位
def calcPi(极限):#发电机功能
"""
打印出圆周率的数字
直到它达到给定的极限
"""
q、 r,t,k,n,l=1,0,1,1,3,3
十进制=极限
计数器=0
而柜台!=十进制+1:
如果4*q+r-t
这是使用迭代方法
#!/usr/bin/env python3
# https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py
# Find PI to the Nth Digit
# Have the user enter a number 'n'
# and print out PI to the 'n'th digit
def calcPi(limit): # Generator function
"""
Prints out the digits of PI
until it reaches the given limit
"""
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
decimal = limit
counter = 0
while counter != decimal + 1:
if 4 * q + r - t < n * t:
# yield digit
yield n
# insert period after first digit
if counter == 0:
yield '.'
# end
if decimal == counter:
print('')
break
counter += 1
nr = 10 * (r - n * t)
n = ((10 * (3 * q + r)) // t) - 10 * n
q *= 10
r = nr
else:
nr = (2 * q + r) * l
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
def main(): # Wrapper function
# Calls CalcPi with the given limit
pi_digits = calcPi(int(input(
"Enter the number of decimals to calculate to: ")))
i = 0
# Prints the output of calcPi generator function
# Inserts a newline after every 40th number
for d in pi_digits:
print(d, end='')
i += 1
if i == 40:
print("")
i = 0
if __name__ == '__main__':
main()
#/usr/bin/env蟒蛇3
# https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py
#找到第n位的PI
#让用户输入一个数字“n”
#并将圆周率打印到第n位
def calcPi(极限):#发电机功能
"""
打印出圆周率的数字
直到它达到给定的极限
"""
q、 r,t,k,n,l=1,0,1,1,3,3
十进制=极限
计数器=0
而柜台!=十进制+1:
如果4*q+r-t
有几种方法。最困难的部分是原始的计算量和内存空间。Ramanujan有一个非常优雅的公式来生成piDo的数字。实际上,你想要编写一个程序来实现这一点,还是想要一个自由、成熟、高性能的解决方案,比如这里的各种方法:有几种方法。最困难的部分是原始的计算量和内存空间。Ramanujan有一个非常优雅的公式来生成piDo的数字。你真的想写一个程序来实现这一点,还是想得到一个自由、成熟、高性能的解决方案,比如这里的各种方法: