Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1000 digits of pi in Python

Tags:

I have been thinking about this issue and I can't figure it out. Perhaps you can assist me. The problem is my code isn't working to output 1000 digits of pi in the Python coding language.

Here's my code:

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    while True:
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x)
        else:
            q, r, t, k, m, x = (q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2)

digits = make_pi()
pi_list = []
my_array = []
for i in range(1000):
    my_array.append(str("hello, I'm an element in an array \n" ))
big_string = "".join(my_array)

print "here is a big string:\n %s" % big_string 

I know this code can be fixed to work, but I'm not sure what to fix... The print statement saying here is a big string and the my_array.append(str("hello, im an element in an array \n)) is just a filler for now. I know how all the code is used to work, but like I said before, I can't get it to shoot out that code.

like image 313
bobimo Avatar asked Jan 25 '12 14:01

bobimo


People also ask

How many digits of pi are stored in Python?

You can have 1000000(1 Million) digits of pi in a string. You can't calculate with it, because if you convert it to foat python will reduce it to 3.141592653589793 but you can many things; make e. a pi search(To find a number sequence in pi), create music with pi, make a 3D code drawing or even make a game!

What are the 1000 digits of pi?

3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944 5923078164 0628620899 8628034825 3421170679 ...

How do you get the full value of pi in Python?

The math. pi constant returns the value of PI: 3.141592653589793. Note: Mathematically PI is represented by π.

What are the 50 trillion digits of pi?

Conversation. The most accurate value of pi goes to 50,000,000,000,000 digits. That's 50 trillion decimal places! 😱 It would take over 178 billion tweets to write it out in full.


3 Answers

If you don't want to implement your own algorithm, you can use mpmath.

try:     # import version included with old SymPy     from sympy.mpmath import mp except ImportError:     # import newer version     from mpmath import mp  mp.dps = 1000  # set number of digits print(mp.pi)   # print pi to a thousand places 

Reference

Update: Code supports older and newer installations of SymPy (see comment).*

like image 65
Garrett Hyde Avatar answered Oct 05 '22 15:10

Garrett Hyde


Run this

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(1000):
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2


my_array = []

for i in make_pi():
    my_array.append(str(i))

my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)
print "here is a big string:\n %s" % big_string 

And read about yield operator from here: What does the "yield" keyword do?

Here is the answer:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337
like image 36
batbaatar Avatar answered Oct 05 '22 14:10

batbaatar


The accepted answer is incorrect, as noted in comments.

The OP's code appears to be based on an implementation of Spigot's algorithm copied from here.

To fix the code per the OP's question (although I renamed the variables and functions to match what they were in the original source), one solution might be:

#!/usr/bin/env python

DIGITS = 1000

def pi_digits(x):
    """Generate x digits of Pi."""
    q,r,t,k,n,l = 1,0,1,1,3,3
    while x >= 0:
        if 4*q+r-t < x*t:
            yield n
            x -= 1
            q,r,t,k,n,l = 10*q, 10*(r-n*t), t, k, (10*(3*q + r))/t-10*n, l
        else:
            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

digits = [str(n) for n in list(pi_digits(DIGITS))]
print("%s.%s\n" % (digits.pop(0), "".join(digits)))

Also, here is a much faster* implementation, also apparently based on Spigot's algorithm:

#!/usr/bin/env python

DIGITS = 1000

def pi_digits(x):
    """Generate x digits of Pi."""
    k,a,b,a1,b1 = 2,4,1,12,4
    while x > 0:
        p,q,k = k * k, 2 * k + 1, k + 1
        a,b,a1,b1 = a1, b1, p*a + q*a1, p*b + q*b1
        d,d1 = a/b, a1/b1
        while d == d1 and x > 0:
            yield int(d)
            x -= 1
            a,a1 = 10*(a % b), 10*(a1 % b1)
            d,d1 = a/b, a1/b1

digits = [str(n) for n in list(pi_digits(DIGITS))]
print("%s.%s\n" % (digits.pop(0), "".join(digits)))

I tested both a few times against this online Pi digit generator.

All credit to this Gist by deeplook.

* Based on testing 10,000 digits, where I got about 7 seconds compared to about 1 second.

like image 22
Alex Harvey Avatar answered Oct 05 '22 15:10

Alex Harvey