Download code
From LiteratePrograms
Back to Pi_with_the_BBP_formula_(Python)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
pibbp.py
1 # Copyright (c) 2010 the authors listed at the following URL, and/or 2 # the authors of referenced articles or incorporated external code: 3 # http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?action=history&offset=20080921002305 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining 6 # a copy of this software and associated documentation files (the 7 # "Software"), to deal in the Software without restriction, including 8 # without limitation the rights to use, copy, modify, merge, publish, 9 # distribute, sublicense, and/or sell copies of the Software, and to 10 # permit persons to whom the Software is furnished to do so, subject to 11 # the following conditions: 12 # 13 # The above copyright notice and this permission notice shall be 14 # included in all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 # 24 # Retrieved from: http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?oldid=14672 25 26 def S(j, n): 27 # Left sum 28 s = 0.0 29 k = 0 30 while k <= n: 31 r = 8*k+j 32 s = (s + float(pow(16, n-k, r)) / r) % 1.0 33 k += 1 34 # Right sum 35 t = 0.0 36 k = n + 1 37 while 1: 38 newt = t + pow(16, n-k) / (8*k+j) 39 # Iterate until t no longer changes 40 if t == newt: 41 break 42 else: 43 t = newt 44 k += 1 45 return s + t 46 47 def pi(n): 48 n -= 1 49 x = (4*S(1, n) - 2*S(4, n) - S(5, n) - S(6, n)) % 1.0 50 return "%014x" % int(x * 16**14) 51
pibbp2.py
1 # Copyright (c) 2010 the authors listed at the following URL, and/or 2 # the authors of referenced articles or incorporated external code: 3 # http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?action=history&offset=20080921002305 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining 6 # a copy of this software and associated documentation files (the 7 # "Software"), to deal in the Software without restriction, including 8 # without limitation the rights to use, copy, modify, merge, publish, 9 # distribute, sublicense, and/or sell copies of the Software, and to 10 # permit persons to whom the Software is furnished to do so, subject to 11 # the following conditions: 12 # 13 # The above copyright notice and this permission notice shall be 14 # included in all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 # 24 # Retrieved from: http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?oldid=14672 25 26 D = 14 # number of digits of working precision 27 M = 16 ** D 28 SHIFT = (4 * D) 29 MASK = M - 1 30 31 def S(j, n): 32 # Left sum 33 s = 0 34 k = 0 35 while k <= n: 36 r = 8*k+j 37 s = (s + (pow(16,n-k,r)<<SHIFT)//r) & MASK 38 k += 1 39 # Right sum 40 t = 0 41 k = n + 1 42 while 1: 43 xp = int(16**(n-k) * M) 44 newt = t + xp // (8*k+j) 45 # Iterate until t no longer changes 46 if t == newt: 47 break 48 else: 49 t = newt 50 k += 1 51 return s + t 52 53 def pi(n): 54 n -= 1 55 x = (4*S(1, n) - 2*S(4, n) - S(5, n) - S(6, n)) & MASK 56 return "%014x" % x 57
sequential_pi.py
1 # Copyright (c) 2010 the authors listed at the following URL, and/or 2 # the authors of referenced articles or incorporated external code: 3 # http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?action=history&offset=20080921002305 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining 6 # a copy of this software and associated documentation files (the 7 # "Software"), to deal in the Software without restriction, including 8 # without limitation the rights to use, copy, modify, merge, publish, 9 # distribute, sublicense, and/or sell copies of the Software, and to 10 # permit persons to whom the Software is furnished to do so, subject to 11 # the following conditions: 12 # 13 # The above copyright notice and this permission notice shall be 14 # included in all copies or substantial portions of the Software. 15 # 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 # 24 # Retrieved from: http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?oldid=14672 25 26 from gmpy import mpq, mpz 27 28 def mod1(x): 29 return x-mpz(x) 30 31 def pi(): 32 x = 0 33 n = 1 34 while 1: 35 p = mpq((120*n-89)*n+16, (((512*n-1024)*n+712)*n-206)*n+21) 36 x = mod1(16*x + p) 37 n += 1 38 yield int(16*x) 39 40 def allpi(): 41 for n, p in enumerate(pi()): 42 print "%x" % p 43 if n % 1000 == 0: 44 print "\n\n%i\n\n" % n 45
