The calculator below represents a given rational number as a finite continued fraction. It also shows the continued fraction coefficients (first coefficient is the integer part). Read more on continued fractions just below the calculator.
The calculator below converts the continued fraction coefficients back to the rational number.
Continued (recurring) fraction
Continued or recurring fraction is a number representation kind as a sum of the number integer part and the fractional part. The fractional part numerator is always one, the denominator is the sum of the integer part and the fractional part. The fractional part denominator may again contain the sum of integer and fractional part and so on.
a0,a1,a2...an is the continued fraction coefficients.
We use the following algorithm to calculate continued fraction coefficients:
// n - the fraction numerator // d - the fraction denominator loop while d ≠ 0 r ⟵ n mod d; output ⟵ (n-r)/d; n ⟵ d; d ⟵ r; end loop
The reverse transformation algorithm:
// f - the continued fraction coefficient array with indexes 0...k-1 // k - number of the coefficients n ⟵ f[k-1]; d ⟵ 1; loop while k greater than 1 r ⟵ d; d ⟵ n; k ⟵ k-1; n ⟵ f[k-1]*n+r; end loop output ⟵ n/d;