Continued fractions are a way of representing a number as a sum of an integer and a fraction.
Mathematically, a continued fraction is a representation
a0+a1+a2+⋱b2b1b0
ai,biare complex numbers. The continued fraction with bi=1∀i is called a simple continued fraction and continued fractions with finite number of ai are called finite continued fractions.
SageMath provides functions continued_fraction and continued_fraction_list to work with continued fractions. Below is presented a simple implementation of continued_fractions.
defcontinued_fraction_list(xi): ai =floor(xi)if xi == ai:# last coefficientreturn [ai]return [ai] +continued_fraction_list(1/(x - ai))
Convergents of continued fraction
The kthconvergent of a continued fractionx=[a0;a1,a2,a3,a4,…]is the numerical value or approximation calculated using the firstk−1coefficients of the continued fraction. The firstkconvergents are
One of the immediate applications of the convergents is that they give rational approximations given the continued fraction of a number. This allows finding rational approximations to irrational numbers.
Convergents of continued fractions can be calculated in sage
Continued fractions have many other applications. One such applicable in cryptology is based on Legendre's theorem in diophantine approximations.
Theorem: if∣x−ba∣<b21, thenbais a convergent ofx.
Wiener's attack on the RSA cryptosystem works by proving that under certain conditions, an equation of the form∣x−ba∣could be derived wherexis entirely made up of public information andbais made up of private information. Under assumed conditions, the inequality∣x−ba∣<b21is statisfied, and the valueba(private information) is calculated from convergents ofx(public information), consequently breaking the RSA cryptosystem.