Calculation of pi

Back to General discussions forum

namorenogi     2020-07-06 02:03:37

I've got a problem solving this problem. I guess it is about precission but I don't know how to fix it. Can anyone take a look at my code?:

import math

k,n = [int(x) for x in "37 11".split()]

r = 10**k
l= r

for i in range (n):

    d = int(l/2)
    h = int(math.sqrt((r**2)-(d**2)))
    l = int((math.sqrt(((r-h)**2) + (d**2))))

pi = int((l*(6*(2**n)))/2)

print (pi)
qwerty     2020-07-06 04:04:49

When we calculating square root of some Z, we should use as a result the greatest integer M such that when squared it does not exceed Z (i. e. M^2 <= Z).

namorenogi     2020-07-06 05:27:26

How can I find that integer? I tried substracting one integer for the result from sqrt(Z) until finding that integer such that its square is not greater than Z, but that's very slow...

qwerty     2020-07-06 08:48:02

I see you have solved task 18 (Square root), you can reuse that solution by replacing / to // and setting proper type of loop.

Please login and solve 5 problems to be able to post at forum