Advertisement
flypip

Flypi2 (pi algorithme)

Nov 22nd, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import with_statement
  3. import decimal
  4.  
  5. def pi():
  6.     D = decimal.Decimal
  7.     with decimal.localcontext() as pop:
  8.         pop.prec += 2                
  9.         pip = lambda am,x,r: (am + x)**2 / (4 * r)                
  10.         am, x, r, o = 1, 1/D(2).sqrt(), 1/D(4), 1                
  11.         pi, pif = pip(am, x, r), None
  12.         while pi != pif:
  13.             pif = pi
  14.             an = (am + x) / 2
  15.             x = (am * x).sqrt()
  16.             r -= o * (am - an)**2
  17.             am, o = an, 2*o
  18.             pi = pip(am, x, r)                
  19.     return +pi
  20.  
  21. decimal.getcontext().prec = input("le nombre de chiffre en tout : ")
  22. print pi()
  23.  
  24.  
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement