Skip to content

lucamarx/pyContFrac

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyContFrac

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

Installation

Clone the repository and run

pip install -e .

Quick Start

>>> 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

Similar Libraries

Releases

No releases published

Packages

No packages published

Languages