44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
if __name__ == '__main__':
|
|
|
|
target = '3.14159263'
|
|
# target = '3.14159263589'
|
|
digits = len(target) - 2
|
|
power = pow(10, digits)
|
|
target = int(float(target) * power)
|
|
|
|
result = 0
|
|
divisor = -1
|
|
step = 0
|
|
count = -1
|
|
count2 = 0
|
|
subtract = False
|
|
|
|
close = False
|
|
r4 = 0
|
|
|
|
while (not close):
|
|
count = count + 1
|
|
divisor = divisor + 2
|
|
|
|
step = 1 / divisor
|
|
if subtract == True:
|
|
result = result - step
|
|
subtract = False
|
|
else:
|
|
result = result + step
|
|
subtract = True
|
|
|
|
r4 = result * 4
|
|
|
|
if count2 == 100000:
|
|
print("Step: %10d | Fraction: 1/%10d = %1.12f | Result: %1.12f | Result * 4: %1.12f" % (count, divisor, step, result, r4))
|
|
count2 = 0
|
|
|
|
if int(r4 * power) == target:
|
|
close = True
|
|
|
|
count2 = count2 + 1
|
|
|
|
print("Step: %10d | Fraction: 1/%10d = %1.12f | Result: %1.12f | Result * 4: %1.12f" % (count , divisor, step, result, r4))
|