Mode of #cas
continuedFraction
The continued fraction of a rational number, with a theorem that it equals the number.
#cas [continuedFraction] qI 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.
Examples (14)#
Basic Examples (2)
The expansion of , with its theorem:
#cas [continuedFraction] (415/93)
continued fraction
[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
Value of the continued fraction
415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7)) ∧ (∀ a ∈ [2, 6, 7], 1 ≤ a) ∧ 2 ≤ 7
A decimal is the exact rational it denotes, here :
#cas [continuedFraction] (3.14159)
continued fraction
[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
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
Scope (3)
A negative number has a negative first term, :
#cas [continuedFraction] (-7/3)
continued fraction
[-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
Value of the continued fraction
-(7 / 3) = -3 + 1 / (1 + 1 / 2) ∧ (∀ a ∈ [1, 2], 1 ≤ a) ∧ 2 ≤ 2
An integer is its own continued fraction:
#cas [continuedFraction] (5)
continued fraction
[5]- Domain
ℚ- Input (rational number)
5
Checked by Lean’s kernel: Value of the continued fraction
5 = 5
The certificate: 1 checked step
Value of the continued fraction
5 = 5
A number between 0 and 1 starts with 0:
#cas [continuedFraction] (0.25)
continued fraction
[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
Value of the continued fraction
1 / 4 = 0 + 1 / 4 ∧ (∀ a ∈ [4], 1 ≤ a) ∧ 2 ≤ 4
Applications (2)
The theorem as a statement in ℚ. Only the three standard axioms are used:
theorem continued_fraction_415_93 : (415 / 93 : ℚ) = 4 + 1 / (2 + 1 / (6 + 1 / 7)) := by cas #print axioms continued_fraction_415_93
continued_fraction_415_93 depends on the axioms propext, Classical.choice, Quot.sound.
A tropical year is about 365.24219 days. The convergents , and 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:
#cas [continuedFraction] (365.24219)
continued fraction
[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
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
Properties and Relations (3)
fromContinuedFraction goes back from the terms to the number:
#cas [fromContinuedFraction] [4, 2, 6, 7]
rational number
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
Value of the continued fraction
415 / 93 = 4 + 1 / (2 + 1 / (6 + 1 / 7))
Cutting off the expansion of 3.14159 gives its convergents, among them and Zu Chongzhi’s :
#cas [fromContinuedFraction] [3, 7, 15]
rational number
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
Value of the continued fraction
333 / 106 = 3 + 1 / (7 + 1 / 15)
#cas [fromContinuedFraction] [3, 7, 15, 1]
rational number
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
Value of the continued fraction
355 / 113 = 3 + 1 / (7 + 1 / (15 + 1 / 1))
The terms 4, 2, 6 and 7 are the quotients of Euclid’s algorithm on 415 and 93. The convergent before the last, , gives the Bézout coefficients of extendedGCD, :
#cas [fromContinuedFraction] [4, 2, 6]
rational number
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
Value of the continued fraction
58 / 13 = 4 + 1 / (2 + 1 / 6)
#cas [extendedGCD] (415, 93)
gcd and Bézout coefficients
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
gcd and Bézout coefficients
Nat.gcd 415 93 = 1 ∧ 415 * 13 + 93 * -58 = 1
Possible Issues (3)
An irrational number has an infinite continued fraction, and the mode refuses it:
#cas [continuedFraction] (sqrt 2)
Error
continuedFraction: expected an exact rational literal, such as 415/93, -7/3 or 3.14159
Powers are not literals, so I write out as 1024:
#cas [continuedFraction] (1/2^10)
Error
continuedFraction: expected an exact rational literal, such as 415/93, -7/3 or 3.14159
#cas [continuedFraction] (1/1024)
continued fraction
[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
Value of the continued fraction
1 / 1024 = 0 + 1 / 1024 ∧ (∀ a ∈ [1024], 1 ≤ a) ∧ 2 ≤ 1024
A decimal is exact, so its expansion follows the number it rounds only for a while. begins , 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:
#cas [continuedFraction] (3.14159265358979)
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]- 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
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
Neat Examples (1)
The continued fraction of is , with the even numbers in every third place. Its decimal to 16 digits keeps the pattern up to the term 12:
#cas [continuedFraction] (2.718281828459045)
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]- 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
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
