Mode of #cas

fromContinuedFraction

The rational value of a finite continued fraction [a0;a1,…,ak], with a theorem in ℚ.

#cas [fromContinuedFraction] [a₀, a₁, …, aₖ]
gives the rational number a₀ + 1 / (a₁ + 1 / (⋯ + 1 / aₖ)) in lowest terms and proves the identity in ℚ.

fromContinuedFraction takes the terms of a continued fraction and gives back its value, as Mathematica’s FromContinuedFraction does. It undoes continuedFraction. Where I use it most is for convergents. Cut an expansion off after a few terms and you get a fraction that is closer to the number than every fraction with a smaller denominator, which is why 227 and 355113 approximate π so well. The theorem is the identity in ℚ, and Mathlib’s tactic norm_num proves it.

Details and Options

  • The argument is a list of integer literals [a₀, a₁, …, aₖ] with at least one term. The first term may be zero or negative, and the others are positive.
  • The theorem is q = a₀ + 1 / (a₁ + 1 / (⋯ + 1 / aₖ)) in ℚ, for the printed q in lowest terms.
  • The last term may be 1. Since […,ak−1,1]=[…,ak−1+1], such a list has the value of a shorter one: [1, 1, 1] and [1, 2] are both 32. continuedFraction gives the shorter form, whose last term is at least 2.
  • There are no options.

Examples (14)#

Basic Examples (2)

The value of [4;2,6,7]:

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

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


Three ones:

In[2]:=
#cas [fromContinuedFraction] [1, 1, 1]
Out[2]=

rational number

32
3/2
Domain
ℚ
Input (continued fraction)
[1, 1, 1]

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

3 / 2 = 1 + 1 / (1 + 1 / 1)
The certificate: 1 checked step
  1. Value of the continued fraction

    3 / 2 = 1 + 1 / (1 + 1 / 1)

    Checked by Lean’s kernel

Scope (2)

The first term may be negative:

In[3]:=
#cas [fromContinuedFraction] [-3, 1, 2]
Out[3]=

rational number

−73
-7/3
Domain
ℚ
Input (continued fraction)
[-3, 1, 2]

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

-(7 / 3) = -3 + 1 / (1 + 1 / 2)
The certificate: 1 checked step
  1. Value of the continued fraction

    -(7 / 3) = -3 + 1 / (1 + 1 / 2)

    Checked by Lean’s kernel


A first term 0 gives a number between 0 and 1:

In[4]:=
#cas [fromContinuedFraction] [0, 1, 2]
Out[4]=

rational number

23
2/3
Domain
ℚ
Input (continued fraction)
[0, 1, 2]

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

2 / 3 = 0 + 1 / (1 + 1 / 2)
The certificate: 1 checked step
  1. Value of the continued fraction

    2 / 3 = 0 + 1 / (1 + 1 / 2)

    Checked by Lean’s kernel

Applications (3)

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

In[5]:=
theorem from_continued_fraction_ones : (3 / 2 : ℚ) = 1 + 1 / (1 + 1 / 1) := by
  cas

#print axioms from_continued_fraction_ones
Out[5]=

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


π=[3;7,15,1,292,…], and its convergents are the classical approximations. [3;7] is Archimedes’ 227, and [3;7,15,1] is Zu Chongzhi’s 355113, which is good to six decimals because the next term 292 is large:

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

rational number

227
22/7
Domain
ℚ
Input (continued fraction)
[3, 7]

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

22 / 7 = 3 + 1 / 7
The certificate: 1 checked step
  1. Value of the continued fraction

    22 / 7 = 3 + 1 / 7

    Checked by Lean’s kernel

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

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 convergents of 2=[1;2,2,2,…] solve Pell’s equation x2−2y2=±1. Six terms give 9970, and 992−2·702=1:

In[8]:=
#cas [fromContinuedFraction] [1, 2, 2, 2, 2, 2]
Out[8]=

rational number

9970
99/70
Domain
ℚ
Input (continued fraction)
[1, 2, 2, 2, 2, 2]

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

99 / 70 = 1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))))
The certificate: 1 checked step
  1. Value of the continued fraction

    99 / 70 = 1 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / (2 + 1 / 2))))

    Checked by Lean’s kernel

In[9]:=
example : (99 : ℤ) ^ 2 - 2 * 70 ^ 2 = 1 := by norm_num
Out[9]=

Lean accepts the proof.

Properties and Relations (2)

continuedFraction goes the other way and gives back the terms:

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

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


[1, 1, 1] and [1, 2] have the same value, and continuedFraction gives the one whose last term is at least 2:

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

rational number

32
3/2
Domain
ℚ
Input (continued fraction)
[1, 2]

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

3 / 2 = 1 + 1 / 2
The certificate: 1 checked step
  1. Value of the continued fraction

    3 / 2 = 1 + 1 / 2

    Checked by Lean’s kernel

In[12]:=
#cas [continuedFraction] (3/2)
Out[12]=

continued fraction

[1; 2]
[1; 2]
Domain
ℚ
Input (rational number)
3/2

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

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

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

    Checked by Lean’s kernel

Possible Issues (4)

A continued fraction has at least the term a0:

In[13]:=
#cas [fromContinuedFraction] []
Out[13]=

Error

fromContinuedFraction: the list is empty

Only the first term may be zero or negative:

In[14]:=
#cas [fromContinuedFraction] [1, 0, 2]
Out[14]=

Error

fromContinuedFraction: the terms after the first must be positive

The terms are integers:

In[15]:=
#cas [fromContinuedFraction] [1, 1/2]
Out[15]=

Error

fromContinuedFraction: expected integers

The terms go in a list, in square brackets:

In[16]:=
#cas [fromContinuedFraction] (4, 2, 6, 7)
Out[16]=

Error

fromContinuedFraction: expected a list `[a₀, a₁, …]`

Neat Examples (1)

Ten ones give the quotient 8955 of the fibonacci numbers F11 and F10, a convergent of the golden ratio φ=[1;1,1,…]:

In[17]:=
#cas [fromContinuedFraction] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Out[17]=

rational number

8955
89/55
Domain
ℚ
Input (continued fraction)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

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

89 / 55 = 1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / 1))))))))
The certificate: 1 checked step
  1. Value of the continued fraction

    89 / 55 = 1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / (1 + 1 / 1))))))))

    Checked by Lean’s kernel

See Also

continuedFraction · extendedGCD · fibonacci