1000 digits of pi in python

2019-01-11 07:09发布

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.

标签: python pi
7条回答
劫难
2楼-- · 2019-01-11 07:50

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).*

查看更多
劫难
3楼-- · 2019-01-11 07:55

Does this do what you want?

i = 0;
pi_str = ""
for x in make_pi():
    pi_str += str(x)
    i += 1
    if i == 1001:
        break

print "pi= %s.%s" % (pi_str[0],pi_str[1:])
查看更多
乱世女痞
4楼-- · 2019-01-11 07:57

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


digits = make_pi()
pi_list = []
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 in Python?

Here is the answer:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337
查看更多
做个烂人
5楼-- · 2019-01-11 07:59

wallis formula can get to 3.141592661439964 but a more efficient way is needed to solve this problem.

https://www.youtube.com/watch?v=EZSiQv_G9HM

and now my code

x, y, summing = 2, 3, 4

for count in range (0,100000000):
    summing *= (x/y)
    x += 2
    summing *= (x/y)
    y += 2
    print (summing)
查看更多
爷的心禁止访问
6楼-- · 2019-01-11 08:03

I'm not familiar with your algorithm. Is it an implementation of BBP?

In any case, your make_pi is a generator. Try using it in a for loop:

for digit in make_pi():
    print digit

Note that this loop is infinite: make_pi() never throws StopIteration

查看更多
【Aperson】
7楼-- · 2019-01-11 08:14

Here you can check whether your program outputs correct 1000 digits: http://spoj.com/CONSTANT

Of course you can use diff or tc as well but you'd have to copy these 1000 digits from somewhere and there you just submit your program and check whether the score is bigger than 999.

You can try to print even more digits there and thus get more points. Perhaps you'd enjoy it.

查看更多
登录 后发表回答