Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created July 21, 2021 13:49
Show Gist options
  • Save igorvanloo/b944eb5d239151d0357537016d98cb4c to your computer and use it in GitHub Desktop.
Save igorvanloo/b944eb5d239151d0357537016d98cb4c to your computer and use it in GitHub Desktop.
Continued Fraction Function
def continued_fraction(x):
m0 = 0
d0 = 1
a0 = math.floor(math.sqrt(x)) #These are the starting values
temp_list = [a0]
while True:
mn = int(d0*a0 - m0)
dn = int((x - mn**2)/d0)
an = int(math.floor((math.sqrt(x) + mn) / dn)) #new values
temp_list.append(an)
if an == 2*math.floor(math.sqrt(x)):
break
m0 = mn
d0 = dn
a0 = an #Replace values
return temp_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment