Mode of #cas

continuedFraction

The continued fraction [a0;a1,…,ak] of a rational number, with a theorem that it equals the number.

#cas [continuedFraction] q
gives the continued fraction [a₀; a₁, …, aₖ] of the rational literal q and proves q = a₀ + 1 / (a₁ + 1 / (⋯ + 1 / aₖ)) in ℚ.

I like continued fractions because they answer a concrete question: how well can a number be approximated by fractions with small denominators? Cutting the expansion off gives the convergents, and each convergent is closer to the number than every fraction with a smaller denominator. continuedFraction computes the expansion of a rational number by recording the quotients of Euclid’s algorithm. The theorem is the identity in ℚ together with the conditions on the terms that make the expansion unique, so it does not just say that the expansion has the value q. An expansion with these conditions is unique, a classical fact that the theorem does not restate, so this is the continued fraction of q.

Details and Options

  • q is an exact rational literal, built from natural and decimal literals with -, / and parentheses, such as 415/93, -7/3 or 0.25. A decimal is the exact rational it denotes, so 3.14159 is 314159100000.
  • The theorem is q = a₀ + 1 / (a₁ + 1 / (⋯ + 1 / aₖ)) in ℚ, and for k≥1 also (∀ a ∈ [a₁, …, aₖ], 1 ≤ a) ∧ 2 ≤ aₖ. These conditions make the expansion unique, since […,ak−1,1] has the same value. norm_num proves the identity and decide the conditions.
  • The terms come from Euclid’s algorithm, with a0=⌊q⌋, which may be 0 or negative. An integer n gives [n].
  • An irrational number has an infinite continued fraction and is refused. A decimal approximation from N is a rational literal, with a finite continued fraction that agrees with the infinite one in its first terms.
  • There are no options. fromContinuedFraction goes the other way.

Examples (14)#

Basic Examples (2)

The expansion of 41593, with its theorem:

In[1]:=
#cas [continuedFraction] (415/93)
Out[1]=

continued fraction

[4; 2, 6, 7]
[4; 2, 6, 7]
Domain
ℚ
Input (rational number)
415/93

Checked by Lean’s kernel: Value of the continued fraction

415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7)) ∧ (∀ a ∈ [2, 6, 7], 1 ≤ a) ∧ 2 ≤ 7
The certificate: 1 checked step
  1. Value of the continued fraction

    415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7)) ∧ (∀ a ∈ [2, 6, 7], 1 ≤ a) ∧ 2 ≤ 7

    Checked by Lean’s kernel


A decimal is the exact rational it denotes, here 314159100000:

In[2]:=
#cas [continuedFraction] (3.14159)
Out[2]=

continued fraction

[3; 7, 15, 1, 25, 1, 7, 4]
[3; 7, 15, 1, 25, 1, 7, 4]
Domain
ℚ
Input (rational number)
314159/100000

Checked by Lean’s kernel: Value of the continued fraction

314159 / 100000 = 3 + 1 / (7 + 1 / (15 + 1 / (1 + 1 / (25 + 1 / (1 + 1 / (7 + 1 / 4)))))) ∧
  (∀ a ∈ [7, 15, 1, 25, 1, 7, 4], 1 ≤ a) ∧ 2 ≤ 4
The certificate: 1 checked step
  1. Value of the continued fraction

    314159 / 100000 = 3 + 1 / (7 + 1 / (15 + 1 / (1 + 1 / (25 + 1 / (1 + 1 / (7 + 1 / 4)))))) ∧
      (∀ a ∈ [7, 15, 1, 25, 1, 7, 4], 1 ≤ a) ∧ 2 ≤ 4

    Checked by Lean’s kernel

Scope (3)

A negative number has a negative first term, −73=−3+11+12:

In[3]:=
#cas [continuedFraction] (-7/3)
Out[3]=

continued fraction

[−3; 1, 2]
[-3; 1, 2]
Domain
ℚ
Input (rational number)
-7/3

Checked by Lean’s kernel: Value of the continued fraction

-(7 / 3) = -3 + 1 / (1 + 1 / 2) ∧ (∀ a ∈ [1, 2], 1 ≤ a) ∧ 2 ≤ 2
The certificate: 1 checked step
  1. Value of the continued fraction

    -(7 / 3) = -3 + 1 / (1 + 1 / 2) ∧ (∀ a ∈ [1, 2], 1 ≤ a) ∧ 2 ≤ 2

    Checked by Lean’s kernel


An integer is its own continued fraction:

In[4]:=
#cas [continuedFraction] (5)
Out[4]=

continued fraction

[5]
[5]
Domain
ℚ
Input (rational number)
5

Checked by Lean’s kernel: Value of the continued fraction

5 = 5
The certificate: 1 checked step
  1. Value of the continued fraction

    5 = 5

    Checked by Lean’s kernel


A number between 0 and 1 starts with 0:

In[5]:=
#cas [continuedFraction] (0.25)
Out[5]=

continued fraction

[0; 4]
[0; 4]
Domain
ℚ
Input (rational number)
1/4

Checked by Lean’s kernel: Value of the continued fraction

1 / 4 = 0 + 1 / 4 ∧ (∀ a ∈ [4], 1 ≤ a) ∧ 2 ≤ 4
The certificate: 1 checked step
  1. Value of the continued fraction

    1 / 4 = 0 + 1 / 4 ∧ (∀ a ∈ [4], 1 ≤ a) ∧ 2 ≤ 4

    Checked by Lean’s kernel

Applications (2)

The theorem as a statement in ℚ. Only the three standard axioms are used:

In[6]:=
theorem continued_fraction_415_93 : (415 / 93 : ℚ) = 4 + 1 / (2 + 1 / (6 + 1 / 7)) := by
  cas

#print axioms continued_fraction_415_93
Out[6]=

continued_fraction_415_93 depends on the axioms propext, Classical.choice, Quot.sound.


A tropical year is about 365.24219 days. The convergents 14, 729 and 833 of its fractional part are leap year rules: one leap day in 4 years for the Julian calendar, and 8 in 33 for Omar Khayyam’s:

In[7]:=
#cas [continuedFraction] (365.24219)
Out[7]=

continued fraction

[365; 4, 7, 1, 3, 24, 6, 2, 2]
[365; 4, 7, 1, 3, 24, 6, 2, 2]
Domain
ℚ
Input (rational number)
36524219/100000

Checked by Lean’s kernel: Value of the continued fraction

36524219 / 100000 =
    365 + 1 / (4 + 1 / (7 + 1 / (1 + 1 / (3 + 1 / (24 + 1 / (6 + 1 / (2 + 1 / 2))))))) ∧
  (∀ a ∈ [4, 7, 1, 3, 24, 6, 2, 2], 1 ≤ a) ∧ 2 ≤ 2
The certificate: 1 checked step
  1. Value of the continued fraction

    36524219 / 100000 =
        365 + 1 / (4 + 1 / (7 + 1 / (1 + 1 / (3 + 1 / (24 + 1 / (6 + 1 / (2 + 1 / 2))))))) ∧
      (∀ a ∈ [4, 7, 1, 3, 24, 6, 2, 2], 1 ≤ a) ∧ 2 ≤ 2

    Checked by Lean’s kernel

Properties and Relations (3)

fromContinuedFraction goes back from the terms to the number:

In[8]:=
#cas [fromContinuedFraction] [4, 2, 6, 7]
Out[8]=

rational number

41593
415/93
Domain
ℚ
Input (continued fraction)
[4, 2, 6, 7]

Checked by Lean’s kernel: Value of the continued fraction

415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7))
The certificate: 1 checked step
  1. Value of the continued fraction

    415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7))

    Checked by Lean’s kernel


Cutting off the expansion of 3.14159 gives its convergents, among them 333106 and Zu Chongzhi’s 355113:

In[9]:=
#cas [fromContinuedFraction] [3, 7, 15]
Out[9]=

rational number

333106
333/106
Domain
ℚ
Input (continued fraction)
[3, 7, 15]

Checked by Lean’s kernel: Value of the continued fraction

333 / 106 = 3 + 1 / (7 + 1 / 15)
The certificate: 1 checked step
  1. Value of the continued fraction

    333 / 106 = 3 + 1 / (7 + 1 / 15)

    Checked by Lean’s kernel

In[10]:=
#cas [fromContinuedFraction] [3, 7, 15, 1]
Out[10]=

rational number

355113
355/113
Domain
ℚ
Input (continued fraction)
[3, 7, 15, 1]

Checked by Lean’s kernel: Value of the continued fraction

355 / 113 = 3 + 1 / (7 + 1 / (15 + 1 / 1))
The certificate: 1 checked step
  1. Value of the continued fraction

    355 / 113 = 3 + 1 / (7 + 1 / (15 + 1 / 1))

    Checked by Lean’s kernel


The terms 4, 2, 6 and 7 are the quotients of Euclid’s algorithm on 415 and 93. The convergent before the last, [4;2,6]=5813, gives the Bézout coefficients of extendedGCD, 415·13−93·58=1:

In[11]:=
#cas [fromContinuedFraction] [4, 2, 6]
Out[11]=

rational number

5813
58/13
Domain
ℚ
Input (continued fraction)
[4, 2, 6]

Checked by Lean’s kernel: Value of the continued fraction

58 / 13 = 4 + 1 / (2 + 1 / 6)
The certificate: 1 checked step
  1. Value of the continued fraction

    58 / 13 = 4 + 1 / (2 + 1 / 6)

    Checked by Lean’s kernel

In[12]:=
#cas [extendedGCD] (415, 93)
Out[12]=

gcd and Bézout coefficients

1,[13, −58]
1, [13, -58]
Domain
ℕ
Input (natural numbers)
(415, 93)

Checked by Lean’s kernel: gcd and Bézout coefficients

Nat.gcd 415 93 = 1 ∧ 415 * 13 + 93 * -58 = 1
The certificate: 1 checked step
  1. gcd and Bézout coefficients

    Nat.gcd 415 93 = 1 ∧ 415 * 13 + 93 * -58 = 1

    Checked by Lean’s kernel

Possible Issues (3)

An irrational number has an infinite continued fraction, and the mode refuses it:

In[13]:=
#cas [continuedFraction] (sqrt 2)
Out[13]=

Error

continuedFraction: expected an exact rational literal, such as 415/93, -7/3 or 3.14159

Powers are not literals, so I write 210 out as 1024:

In[14]:=
#cas [continuedFraction] (1/2^10)
Out[14]=

Error

continuedFraction: expected an exact rational literal, such as 415/93, -7/3 or 3.14159
In[15]:=
#cas [continuedFraction] (1/1024)
Out[15]=

continued fraction

[0; 1024]
[0; 1024]
Domain
ℚ
Input (rational number)
1/1024

Checked by Lean’s kernel: Value of the continued fraction

1 / 1024 = 0 + 1 / 1024 ∧ (∀ a ∈ [1024], 1 ≤ a) ∧ 2 ≤ 1024
The certificate: 1 checked step
  1. Value of the continued fraction

    1 / 1024 = 0 + 1 / 1024 ∧ (∀ a ∈ [1024], 1 ≤ a) ∧ 2 ≤ 1024

    Checked by Lean’s kernel


A decimal is exact, so its expansion follows the number it rounds only for a while. π begins [3;7,15,1,292,1,1,1,2,1,3,1,14,…], and its decimal to 15 digits has 12 where π has 14. The expansion is long, and in the printed theorem Lean writes ⋯ for the innermost levels of the nested fraction:

In[16]:=
#cas [continuedFraction] (3.14159265358979)
Out[16]=

continued fraction

[3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 12, 2, 4, 1, 1, 3, 2, 2, 1, 18, 1, 2, 2, 1, 7, 2, 2]
[3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 12, 2, 4, 1, 1, 3, 2, 2, 1, 18, 1, 2, 2, 1, 7, 2, 2]
Domain
ℚ
Input (rational number)
314159265358979/100000000000000

Checked by Lean’s kernel: Value of the continued fraction

314159265358979 / 100000000000000 =
    3 +
      1 /
        (7 +
          1 /
            (15 +
              1 /
                (1 +
                  1 /
                    (292 +
                      1 /
                        (1 +
                          1 /
                            (1 +
                              1 /
                                (1 +
                                  1 /
                                    (2 +
                                      1 /
                                        (1 +
                                          1 /
                                            (3 +
                                              1 /
                                                (1 +
                                                  1 /
                                                    (12 +
                                                      1 /
                                                        (2 +
                                                          1 /
                                                            (4 +
                                                              1 /
                                                                (1 +
                                                                  1 /
                                                                    (1 +
                                                                      1 /
                                                                        (3 +
                                                                          1 /
                                                                            (2 +
                                                                              1 /
                                                                                (2 +
                                                                                  1 /
                                                                                    (1 +
                                                                                      1 /
                                                                                        (18 +
                                                                                          1 /
                                                                                            (1 +
                                                                                              1 /
                                                                                                (2 +
                                                                                                  1 /
                                                                                                    (2 +
                                                                                                      ⋯)))))))))))))))))))))))) ∧
  (∀ a ∈ [7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 12, 2, 4, 1, 1, 3, 2, 2, 1, 18, 1, 2, 2, 1, 7, 2, 2], 1 ≤ a) ∧
    2 ≤ 2
The certificate: 1 checked step
  1. Value of the continued fraction

    314159265358979 / 100000000000000 =
        3 +
          1 /
            (7 +
              1 /
                (15 +
                  1 /
                    (1 +
                      1 /
                        (292 +
                          1 /
                            (1 +
                              1 /
                                (1 +
                                  1 /
                                    (1 +
                                      1 /
                                        (2 +
                                          1 /
                                            (1 +
                                              1 /
                                                (3 +
                                                  1 /
                                                    (1 +
                                                      1 /
                                                        (12 +
                                                          1 /
                                                            (2 +
                                                              1 /
                                                                (4 +
                                                                  1 /
                                                                    (1 +
                                                                      1 /
                                                                        (1 +
                                                                          1 /
                                                                            (3 +
                                                                              1 /
                                                                                (2 +
                                                                                  1 /
                                                                                    (2 +
                                                                                      1 /
                                                                                        (1 +
                                                                                          1 /
                                                                                            (18 +
                                                                                              1 /
                                                                                                (1 +
                                                                                                  1 /
                                                                                                    (2 +
                                                                                                      1 /
                                                                                                        (2 +
                                                                                                          ⋯)))))))))))))))))))))))) ∧
      (∀ a ∈ [7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 12, 2, 4, 1, 1, 3, 2, 2, 1, 18, 1, 2, 2, 1, 7, 2, 2], 1 ≤ a) ∧
        2 ≤ 2

    Checked by Lean’s kernel

Neat Examples (1)

The continued fraction of e is [2;1,2,1,1,4,1,1,6,…], with the even numbers in every third place. Its decimal to 16 digits keeps the pattern up to the term 12:

In[17]:=
#cas [continuedFraction] (2.718281828459045)
Out[17]=

continued fraction

[2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 10, 2, 1, 3, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 7, 4, 1, 8]
[2; 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10, 1, 1, 12, 1, 1, 10, 2, 1, 3, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 7, 4, 1, 8]
Domain
ℚ
Input (rational number)
543656365691809/200000000000000

Checked by Lean’s kernel: Value of the continued fraction

543656365691809 / 200000000000000 =
    2 +
      1 /
        (1 +
          1 /
            (2 +
              1 /
                (1 +
                  1 /
                    (1 +
                      1 /
                        (4 +
                          1 /
                            (1 +
                              1 /
                                (1 +
                                  1 /
                                    (6 +
                                      1 /
                                        (1 +
                                          1 /
                                            (1 +
                                              1 /
                                                (8 +
                                                  1 /
                                                    (1 +
                                                      1 /
                                                        (1 +
                                                          1 /
                                                            (10 +
                                                              1 /
                                                                (1 +
                                                                  1 /
                                                                    (1 +
                                                                      1 /
                                                                        (12 +
                                                                          1 /
                                                                            (1 +
                                                                              1 /
                                                                                (1 +
                                                                                  1 /
                                                                                    (10 +
                                                                                      1 /
                                                                                        (2 +
                                                                                          1 /
                                                                                            (1 +
                                                                                              1 /
                                                                                                (3 +
                                                                                                  1 /
                                                                                                    (1 +
                                                                                                      ⋯)))))))))))))))))))))))) ∧
  (∀
      a ∈
        let y :=
          let y :=
            let y := [1, 7, 4, 1, 8];
            2 :: 1 :: 1 :: 2 :: 1 :: y;
          let y := 3 :: 1 :: 2 :: 2 :: 1 :: y;
          1 :: 10 :: 2 :: 1 :: y;
        let y :=
          let y := 10 :: 1 :: 1 :: 12 :: 1 :: y;
          1 :: 8 :: 1 :: 1 :: y;
        let y := 4 :: 1 :: 1 :: 6 :: 1 :: y;
        1 :: 2 :: 1 :: 1 :: y,
      1 ≤ a) ∧
    2 ≤ 8
The certificate: 1 checked step
  1. Value of the continued fraction

    543656365691809 / 200000000000000 =
        2 +
          1 /
            (1 +
              1 /
                (2 +
                  1 /
                    (1 +
                      1 /
                        (1 +
                          1 /
                            (4 +
                              1 /
                                (1 +
                                  1 /
                                    (1 +
                                      1 /
                                        (6 +
                                          1 /
                                            (1 +
                                              1 /
                                                (1 +
                                                  1 /
                                                    (8 +
                                                      1 /
                                                        (1 +
                                                          1 /
                                                            (1 +
                                                              1 /
                                                                (10 +
                                                                  1 /
                                                                    (1 +
                                                                      1 /
                                                                        (1 +
                                                                          1 /
                                                                            (12 +
                                                                              1 /
                                                                                (1 +
                                                                                  1 /
                                                                                    (1 +
                                                                                      1 /
                                                                                        (10 +
                                                                                          1 /
                                                                                            (2 +
                                                                                              1 /
                                                                                                (1 +
                                                                                                  1 /
                                                                                                    (3 +
                                                                                                      1 /
                                                                                                        (1 +
                                                                                                          ⋯)))))))))))))))))))))))) ∧
      (∀
          a ∈
            let y :=
              let y :=
                let y := [1, 7, 4, 1, 8];
                2 :: 1 :: 1 :: 2 :: 1 :: y;
              let y := 3 :: 1 :: 2 :: 2 :: 1 :: y;
              1 :: 10 :: 2 :: 1 :: y;
            let y :=
              let y := 10 :: 1 :: 1 :: 12 :: 1 :: y;
              1 :: 8 :: 1 :: 1 :: y;
            let y := 4 :: 1 :: 1 :: 6 :: 1 :: y;
            1 :: 2 :: 1 :: 1 :: y,
          1 ≤ a) ∧
        2 ≤ 8

    Checked by Lean’s kernel

See Also

fromContinuedFraction · extendedGCD · N · integerDigits