本文介绍了高斯 - 勒让德算法蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助计算圆周率。我想写一个python程序,将计算圆周率至X位。我已经试了从Python邮件列表,它是慢于我的使用。我已阅读有关高斯 - 勒让德算法,我已经尝试过,但没有成功移植到了Python。

我从阅读此处,我会AP preciate任何输入到哪里我错了!

它输出:0.163991276262

 从__future__进口师
进口数学
DEF广场(X):返回X * X
a = 1时
B = 1 /的Math.sqrt(2)
T = 1/4
X = 1
因为我在范围内(1000):
    Y = A
    一个=(A + B)/ 2
    B =的Math.sqrt(B * Y)
    T =  -  X *平方((Y-A))
    X = 2 * X

PI =(平方((A + B)))/ 4 * T
打印圆周率
进行raw_input()
 

解决方案
  1. 您忘了周围的 4 * T 括号:

      PI =(A + B)** 2 /(4 * T)
     

  2. 您可以使用十进制以更高的precision进行计算。

     #!的/ usr /斌/包膜蟒蛇
    从__future__进口with_statement
    进口十进制
    
    
    高清pi_gauss_legendre():
        D = decimal.Decimal
        与decimal.localcontext(),为CTX:
            CTX。preC + = 2
            的a,b,T,P = 1,1 / D(2).sqrt(),1 / D(4),1
            PI =无
            而1:
                一个=(A + B)/ 2
                B =(A * B).sqrt()
                笔 -  = P *(一 - 一)*(一 - 一)
                一个,P =一个,2 * P
                piold =圆周率
                圆周率=(A + B)*(A + B)/(4 * t)的
                如果PI == piold:#内给予precision等于
                    打破
        返回+ PI
    
    
    decimal.getcontext()。preC = 100
    打印pi_gauss_legendre()
     

输出:

  3.141592653589793238462643383279502884197169399375105820974944592307816406286208 \
    998628034825342117068
 

I need some help calculating Pi. I am trying to write a python program that will calculate Pi to X digits. I have tried several from the python mailing list, and it is to slow for my use.I have read about the Gauss-Legendre Algorithm, and I have tried porting it to Python with no success.

I am reading from Here, and I would appreciate any input as to where I am going wrong!

It outputs: 0.163991276262

from __future__ import division
import math
def square(x):return x*x
a = 1
b = 1/math.sqrt(2)
t = 1/4
x = 1
for i in range(1000):
    y = a
    a = (a+b)/2
    b = math.sqrt(b*y)
    t = t - x * square((y-a))
    x = 2* x

pi = (square((a+b)))/4*t
print pi
raw_input()
解决方案
  1. You forgot parentheses around 4*t:

    pi = (a+b)**2 / (4*t)
    

  2. You can use decimal to perform calculation with higher precision.

    #!/usr/bin/env python
    from __future__ import with_statement
    import decimal
    
    
    def pi_gauss_legendre():
        D = decimal.Decimal
        with decimal.localcontext() as ctx:
            ctx.prec += 2
            a, b, t, p = 1, 1/D(2).sqrt(), 1/D(4), 1
            pi = None
            while 1:
                an    = (a + b) / 2
                b     = (a * b).sqrt()
                t    -= p * (a - an) * (a - an)
                a, p  = an, 2*p
                piold = pi
                pi    = (a + b) * (a + b) / (4 * t)
                if pi == piold:  # equal within given precision
                    break
        return +pi
    
    
    decimal.getcontext().prec = 100
    print pi_gauss_legendre()
    

Output:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208\
    998628034825342117068

这篇关于高斯 - 勒让德算法蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:36