python计算小数点后1000位_如何用python 2.7计算pi到1000位?[复制品]

只需调用函数(注意最后一行):

# Pi Calculator

# Python 2.7.3

# After running, type "pi(n)" where n is the number of decimals for pi. For

# example, if you would like to calculate 100 decimals for pi, type "pi(100)".

# import python libraries

from decimal import Decimal, getcontext

from time import time, strftime

import datetime

# arccot function using power formula arccot = 1/x - 1/(3x^3) + 1/(5x^5) ...

def arccot(x, digits):

# set precision and starting values

getcontext().prec = digits

total = 0

n = 1

# loop while new term is large enough to actually change the total

while Decimal((2 * n - 1) * x ** (2 * n - 1)) < Decimal(10 ** digits):

# find value of new term

term = ((-1)**(n - 1)) * 1 / Decimal((2 * n - 1) * x ** (2 * n - 1))

# add the new term to the total

total += term

# next n

n += 1

# return the sum

return total

# pi function

def pi(decimals):

# start timer

timestart = time()

# find pi using Machin's Formula pi = 4 * (4 * arccot(5) - arccot(239))

# and the power formula for arccot (see arccot function above)

print "pi = " + str(Decimal(4 * (4 * arccot(5, decimals + 3) - arccot(239, decimals + 3))).quantize(Decimal(10) ** (-decimals)))

# display elapsed time

timeelapsedint = round(time() - timestart, 2)

timeelapsedstr = str(datetime.timedelta(seconds = round(timeelapsedint, 0)))

print "runtime: " + timeelapsedstr + " or " + str(timeelapsedint) + " seconds."

pi(1000)

输出:

pi = 3.14159265358979323....more digits

runtime: 0:00:00 or 0.22 seconds.

另外,您的代码格式不正确,但我无法编辑它。这里的代码段格式正确。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您好!要将圆周率小数点后的前1000000输出到一个txt文件中,可以使用Python中的以下代码: ```python import math # 计算圆周率的值 pi = str(math.pi) # 将小数点后的数字提取出来,保留前1000000 digits = pi.split('.')[1][:1000000] # 将结果写入txt文件 with open('pi_digits.txt', 'w') as file: file.write(digits) ``` 该代码中,我们首先使用math库计算出圆周率的值,并将其转换为字符串类型。然后,我们使用字符串方法split()将圆周率字符串分割为两部分,分别是小数点前的整数和小数点后的数字。我们只需要后者,因此我们使用字符串切片[:1000000]提取前1000000数字。最后,我们将结果写入一个名为“pi_digits.txt”的文本文件中,使用Python的with语句确保在写入完成后正确关闭文件。 希望这可以帮助您! ### 回答2: 要想输出圆周率小数点后面1000到一个txt文件中,可以使用Python中的第三方库`mpmath`来实现此功能。以下是具体的步骤: 1. 首先,确保你已经安装了`mpmath`库。可以使用以下命令来安装: ```python pip install mpmath ``` 2. 导入`mpmath`库: ```python import mpmath ``` 3. 设置要输出的小数数为1000万,可以使用`mp.dps`属性进行设置: ```python mpmath.mp.dps = 10000000 ``` 4. 使用`mpmath.pi`函数来计算圆周率,并将结果转为字符串: ```python pi_str = str(mpmath.mp.pi) ``` 5. 将圆周率小数点后面1000的字符串内容写入到txt文件中: ```python with open('pi.txt', 'w') as file: file.write(pi_str) ``` 以上就是使用Python输出圆周率小数点后面1000到一个txt文件中的步骤。执行完以上代码后,该txt文件将会包含圆周率小数点后面1000的内容。 ### 回答3: 要将圆周率(&pi;)小数点后面的1000输出到一个txt文件中,可以使用Python中的库来计算&pi;的值,并将结果写入文件。 首先,需要安装math库,并引入它: ``` import math ``` 然后,使用math库中的pi属性来获取&pi;的近似值: ``` pi = str(math.pi) ``` 接下来,创建一个新的txt文件,并打开它以便写入内容: ``` file = open("pi_digits.txt", "w") ``` 然后,将&pi;的1000之后的数字提取出来,写入文件: ``` file.write(pi[2:10000002]) ``` 最后,关闭文件: ``` file.close() ``` 完整的代码如下: ```python import math pi = str(math.pi) file = open("pi_digits.txt", "w") file.write(pi[2:10000002]) file.close() ``` 执行该代码后,会生成一个名为"pi_digits.txt"的txt文件,其中包含圆周率小数点后面的1000数字。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值