Created
January 29, 2019 19:12
-
-
Save EliseAv/ccd98081f5cb3c6c88411058c20a70e8 to your computer and use it in GitHub Desktop.
Continued Fraction
This file contains 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
from fractions import Fraction as F | |
from math import floor, pi | |
def to_cf(value): | |
head = floor(value) | |
value = F(value - head) | |
parts = [head] | |
while value: | |
value = F(1, value) | |
part = int(value) | |
parts.append(part) | |
value -= part | |
return tuple(parts) | |
def from_cf(parts): | |
iterator = reversed(parts) | |
value = F(next(iterator)) | |
for part in iterator: | |
value = part + F(1, value) | |
return value | |
PICF = ( | |
3, 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 2, 1, 84, 2, | |
1, 1, 15, 3, 13, 1, 4, 2, 6, 6, 99, 1, 2, 2, 6, 3, 5, 1, 1, 6, 8, 1, 7, 1, | |
2, 3, 7, 1, 2, 1, 1, 12, 1, 1, 1, 3, 1, 1, 8, 1, 1, 2, 1, 6, 1, 1, 5, 2, 2, | |
3, 1, 2, 4, 4, 16, 1, 161, 45, 1, 22, 1, 2, 2, 1, 4, 1, 2, 24, 1, 2, 1, 3, | |
1, 2, 1, 1, 10, 2, 5, 4, 1, 2, 2, 8, 1, 5, 2, 2, 26, 1, 4, 1, 1, 8, 2, 42, | |
2, 1, 7, 3, 3, 1, 1, 7, 2, 4, 9, 7, 2, 3, 1, 57, 1, 18, 1, 9, 19, 1, 2, 18, | |
1, 3, 7, 30, 1, 1, 1, 3, 3, 3, 1, 2, 8, 1, 1, 2, 1, 15, 1, 2, 13, 1, 2, 1, | |
4, 1, 12, 1, 1, 3, 3, 28, 1, 10, 3, 2, 20, 1, 1, 1, 1, 4, 1, 1, 1, 5, 3, 2, | |
1, 6, 1, 4, 1, 120, 2, 1, 1, 3, 1, 23, 1, 15, 1, 3, 7, 1, 16, 1, 2, 1, 21, | |
2, 1, 1, 2, 9, 1, 6, 4, 127, 14, 5, 1, 3, 13, 7, 9, 1, 1, 1, 1, 1, 5, 4, 1, | |
1, 3, 1, 1, 29, 3, 1, 1, 2, 2, 1, 3, 1, 1, 1, 3, 1, 1, 10, 3, 1, 3, 1, 2, | |
1, 12, 1, 4, 1, 1, 1, 1, 7, 1, 1, 2, 1, 11, 3, 1, 7, 1, 4, 1, 48, 16, 1, 4, | |
5, 2, 1, 1, 4, 3, 1, 2, 3, 1, 2, 2, 1, 2, 5, 20, 1, 1, 5, 4, 1, 436, 8, 1, | |
2, 2, 1, 1, 1, 1, 1, 5, 1, 2, 1, 3, 6, 11, 4, 3, 1, 1, 1, 2, 5, 4, 6, 9, 1, | |
5, 1, 5, 15, 1, 11, 24, 4, 4, 5, 2, 1, 4, 1, 6, 1, 1, 1, 4, 3, 2, 2, 1, 1, | |
2, 1, 58, 5, 1, 2, 1, 2, 1, 1, 2, 2, 7, 1, 15, 1, 4, 8, 1, 1, 4, 2, 1, 1, | |
1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 1, 9, 1, 4, 3, 15, 1, 2, 1, 13, 1, 1, 1, 3, | |
24, 1, 2, 4, 10, 5, 12, 3, 3, 21, 1, 2, 1, 34, 1, 1, 1, 4, 15, 1, 4, 44, 1, | |
4, 20776, 1, 1, 1, 1, 1, 1, 1, 23, 1, 7, 2, 1, 94, 55, 1, 1, 2, 1, 1, 3, 1, | |
1, 32, 5, 1, 14, 1, 1, 1, 1, 1, 3, 50, 2, 16, 5, 1, 2, 1, 4, 6, 3, 1, 3, 3, | |
) | |
if __name__ == '__main__': | |
print(PICF[:20]) | |
print(to_cf(pi)) | |
print(from_cf(PICF)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment