pyContFrac is a library to do arithmetic with continued fractions.
A continued fraction is a way to represent real numbers
1
x = a0 + -------------
1
a1 + --------
1
a2 + ---
...
In 1972 Bill Gosper discovered an algorithm to perform arithmetic with continued fractions.
pyContFrac is a Python implementation of the Gosper algorithm, it is based on the original paper and the talk by Mark Jason Dominus
Arithmetic with Continued Fractions
Clone the repository and run
pip install -e .
>>> import math
>>> from contfrac import ContFrac
>>> r = ContFrac('254/100') # CF for a rational number
>>> print(r) # it has a finite number of coefficients
[2, 1, 1, 5, 1, 3]
127
--- = 2.54
50
>>> sqrt2 = ContFrac(math.sqrt(2)) # CF for an irrational number
>>> print(sqrt2) # its coefficient expansion goes on and on
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, ...]
9369319
------- = 1.414213562373087
6625109
>>> print((r * sqrt2 - 1) / 3)
[0, 1, 6, 2, 1, 4, 1, 1, 19, 1, ...]
6498851
------- = 0.864034149475884
7521521