proffreda

Gibbons_spigot.py

Apr 25th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. # pi.py -- imlementation of Gibbons' spigot algorithm for pi
  2. # John Zelle 4-5-06
  3.  
  4. def pi_digits():
  5. """generator for digits of pi"""
  6. q,r,t,k,n,l = 1,0,1,1,3,3
  7. while True:
  8. if 4*q+r-t < n*t:
  9. yield n
  10. q,r,t,k,n,l = (10*q,10*(r-n*t),t,k,(10*(3*q+r))//t-10*n,l)
  11. else:
  12. q,r,t,k,n,l = (q*k,(2*q+r)*l,t*l,k+1,(q*(7*k+2)+r*l)//(t*l),l+2)
Advertisement
Add Comment
Please, Sign In to add comment