continued_fraction, a Python code which implements some simple algorithms for dealing with simple and generalized continued fractions.

Mathematically, continued fractions are infinite sums. In general, they are treated computationally as finite sums. Here we will assume that all such sums terminate at index N.

A simple continued fraction (SCF) is a representation of a number R as:

        R = A0 + 1 / ( A1 + 1 / ( A2 + 1 / ( A3 + ... + 1 / AN )...)))

A generalized continued fraction (GCF) is a representation of a number R as:

        R = A0 + B1 / ( A1 + B2 / ( A2 + B3 / ( A3 + ... + BN / AN )...)))

For either form of continued fraction, there are two tasks:

  1. Given a value R, determine an N-term SCF or GCF which is equal to, or approximates, R.
  2. Given the coefficients of an SCF or GCF, determine the sequence of rational values represented by computing the partial sums. The final sum is the value R.


I don't care what you do with this code.


continued_fraction is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version and a Python version and.

Related Software and Data:

subset, a Python code which enumerates, generates, randomizes, ranks and unranks combinatorial objects including combinations, compositions, Gray codes, index sets, partitions, permutations, polynomials, subsets, and Young tables. Backtracking routines are included to solve some combinatorial problems. Some routines for continued fractions are included.


  1. Norman Richert,
    Strang's Strange Figures,
    American Mathematical Monthly,
    Volume 99, Number 2, February 1992, pages 101-107.

Source Code:

Last revised on 19 January 2020.