1000 digits of pi in Python

前端 未结 11 1453
忘了有多久
忘了有多久 2020-11-28 10:54

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

11条回答
  •  难免孤独
    2020-11-28 11:18

    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
    

提交回复
热议问题