for i in range(1, 100):
num = 2 ** i
p2 = 0
for k in range(1, num + 1):
p2 += (1 / num + 1) * ( (1 - (k / num + 1) ** 2) ** .5 ) * 4
print(p2)
This is the code, but the value should be close to the approximation of the pie value, but it keeps printing complex values.
for i in range(1, 100):
num = 2 ** i
p2 = 0
for k in range(1, num):
p2 += (1 / num) * ( (1 - (k / num) ** 2) ** .5 ) * 4
print(p2)
On the other hand, this code works well.
Where did the problem come from?
python
(1 - (k / num + 1) ** 2) ** .5
where (1 - (k/num + 1) ** 2)
is negative.
© 2024 OneMinuteCode. All rights reserved.