Skip to content

Instantly share code, notes, and snippets.

@telliott99
Created August 25, 2021 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telliott99/bf740d6ca5fe6ac8ff32e217569a6b8f to your computer and use it in GitHub Desktop.
Save telliott99/bf740d6ca5fe6ac8ff32e217569a6b8f to your computer and use it in GitHub Desktop.
import sys
from fractions import Fraction
prog, name, reps, lead = sys.argv[:4]
lead, reps = int(lead), int(reps)
L = [Fraction(s) for s in sys.argv[4:]]
L = L * reps
pL = []
def add_invert(n,d):
p = 1/d
q = n + p
pL.append((str(d), str(p), str(n), str(q)))
return q
def evaluate(L):
d = L.pop()
while L:
n = L.pop()
d = add_invert(n,d)
return add_invert(lead,d)
x = evaluate(L)
print(name)
for t in pL:
print('%10s %10s %4s %10s' % t)
print(x)
if name.startswith('sqrt'):
print('%3.12f' % float(x**2))
else:
print('%3.12f' % float(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment