0

What is wrong with this code? It is supposed to give the value of the Nth digit of Pi which the user provides.

16th Dec 2024, 1:31 AM
Ondieki Onduso
Ondieki Onduso - avatar
1 ответ
+ 4
if you mean your code: https://sololearn.com/compiler-playground/c14uDS9vL4n4/?ref=app 22/7 is just an aproximation of pi. It is correct only up to 3.14. The rest will always be incorrect. Pi is an irrational number. You cannot use such a simple fraction to calculate the digits. a = 22 b = 7 # actual value only looks like pi for the first 3 digits print(f'{a/b = }') digits = [0] #using your formula will not give correct value of pi after 3.14. for n in range(0,50): print(f'{n = } -> {digits[-1] = }') while len(digits) < n: c = 0 d = a % b while a >= b : a = a - b c += 1 digits.append(c) a = d * 10
16th Dec 2024, 2:43 AM
Bob_Li
Bob_Li - avatar