Created
July 21, 2021 13:49
-
-
Save igorvanloo/b944eb5d239151d0357537016d98cb4c to your computer and use it in GitHub Desktop.
Continued Fraction Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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