Back to General discussions forum
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)
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).
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...
I see you have solved task 18 (Square root), you can reuse that solution by replacing / to // and setting proper type of loop.