Download code

Jump to: navigation, search

Back to Pi_with_the_BBP_formula_(Python)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

sequential_pi.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?oldid=19187
14 
15 from gmpy import mpq, mpz
16 
17 def mod1(x):
18     return x-mpz(x)
19 
20 def pi():
21     x = 0
22     n = 1
23     while 1:
24         p = mpq((120*n-89)*n+16, (((512*n-1024)*n+712)*n-206)*n+21)
25         x = mod1(16*x + p)
26         n += 1
27         yield int(16*x)
28 def allpi():
29     from sys import stdout
30     p = pi()
31     n = 1
32     while 1:
33         stdout.write("%x" % p.next())
34         if n % 1000 == 0:
35             print "\n\n%i\n\n" % n
36         n += 1


hijacker
hijacker
hijacker
hijacker

pibbp.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?oldid=19187
14 
15 def S(j, n):
16     # Left sum
17     s = 0.0
18     k = 0
19     while k <= n:
20         r = 8*k+j
21         s = (s + pow(16, n-k, r) / r) % 1.0
22         k += 1
23     # Right sum
24     t = 0.0
25     k = n + 1
26     while 1:
27         newt = t + pow(16, n-k) / (8*k+j)
28         # Iterate until t no longer changes
29         if t == newt:
30             break
31         else:
32             t = newt
33         k += 1
34     return s + t
35 
36 def pi(n):
37     n -= 1
38     x = (4*S(1, n) - 2*S(4, n) - S(5, n) - S(6, n)) % 1.0
39     return "%014x" % int(x * 16**14)


hijacker
hijacker
hijacker
hijacker

pibbp2.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Pi_with_the_BBP_formula_(Python)?oldid=19187
14 
15 D = 14        # number of digits of working precision
16 M = 16 ** D
17 SHIFT = (4 * D)
18 MASK = M - 1
19 
20 def S(j, n):
21     # Left sum
22     s = 0
23     k = 0
24     while k <= n:
25         r = 8*k+j
26         s = (s + (pow(16,n-k,r)<<SHIFT)//r) & MASK
27         k += 1
28     # Right sum
29     t = 0
30     k = n + 1
31     while 1:
32         xp = int(16**(n-k) * M)
33         newt = t + xp // (8*k+j)
34         # Iterate until t no longer changes
35         if t == newt:
36             break
37         else:
38             t = newt
39         k += 1
40     return s + t
41 
42 def pi(n):
43     n -= 1
44     x = (4*S(1, n) - 2*S(4, n) - S(5, n) - S(6, n)) & MASK
45     return "%014x" % x


hijacker
hijacker
hijacker
hijacker