This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Add-ons / Standard Packages / NumberTheory  /


The decimal expansion is the most common way to represent a real number. This package supports two alternative representations: the continued fraction expansion of a real number and the arbitrary base expansion of a rational number in terms of preperiodic and periodic parts.
The continued fraction expansion of a real number

is a representation of the form

The integers are called the partial quotients. Rational numbers have a finite number of partial quotients, while irrational numbers have an infinite continued fraction expansion. Continued fractions also find application in the factorization of integers (see, for example, Chapter 10 in [Rosen]).
If the number has partial quotients , the rational number formed by considering the first partial quotients , is called the

convergent of

. The convergents of a number provide, in a certain sense, the best rational approximation with a small denominator to the given real number.

Continued fractions.

  • This loads the package.
  • In[1]:= <<NumberTheory`ContinuedFractions`

  • This gives partial quotients in the continued fraction expansion of the square root of 7. Square roots of integers always have repeating blocks in their continued fractions. Typesetting rules defined for ContinuedFractionForm make the result display as a sum of nested fractions.
  • In[2]:= ContinuedFraction[Sqrt[7], 11]


  • The golden ratio has all of its partial quotients equal to one.
  • In[3]:= ContinuedFraction[GoldenRatio, 10]


  • This gives a list of the first

    convergents of the golden ratio. These convergents have the Fibonacci numbers for numerators and denominators.
  • In[4]:= Table[
    Table[1, {n}]]], {n, 11}]


  • The convergents of a number converge to it while alternating sides.
  • In[5]:= % - N[GoldenRatio]


  • Any rational number has a finite continued fraction expansion. This gives all of the partial quotients involved.
  • In[6]:= ContinuedFraction[11/1301]


    Another useful representation of certain types of numbers is the base expansion. Not all such expansions are finite. An infinite base expansion is periodic if there are positive integers and such that for

    . It is common to express the base expansion


    The nonrepeating elements constitute the preperiodic part and the repeating elements constitute the periodic part.

    Periodic expansions.

  • This rational number has a finite decimal expansion; the preperiodic part has length two and the periodic part has length zero. The periodic part is highlighted using typesetting rules defined for PeriodicForm.
  • In[7]:= ToPeriodicForm[ 1/20 ]


  • This rational number has an infinite decimal expansion; the preperiodic part has length zero and the periodic part has length 16.
  • In[8]:= ToPeriodicForm[ 1/17 ]


  • This shows the first few decimal digits of

    and illustrates the recurring expansion explicitly.
  • In[9]:= RealDigits[ N[ 1/17, 35 ] ]


  • The decimal number has an infinite binary expansion. This is the reason that

    cannot be stored in a binary digital computer without representation error.
  • In[10]:= ToPeriodicForm[ 1/10, 2 ]


  • This reconstructs the previous rational number from its binary digit expansion.
  • In[11]:= Normal[ % ]


  • Here is a classical example with a lengthy periodic part involving pairs of digits

    in the decimal digit expansion (see [Glaisher]).
  • In[12]:= ToPeriodicForm[ 1/9801 ]



    [Rosen] K. H. Rosen, Elementary Number Theory and Its Applications, Third edition, Addison Wesley, Reading, MA, 1993.
    [Glaisher] J. W. L. Glaisher, Messenger of Math. 2, 41, 1873.