The Continued Fraction, Explained: How [a0;a1,a2,...] Finds the Best Fraction for Any Number
How a continued fraction expands any number into [a0;a1,a2,...], why its convergents give the best rational approximations, and where 22/7 for pi comes from.
The Continued Fraction, Explained: How [a0;a1,a2,...] Finds the Best Fraction for Any Number
Decimals are good at telling you a number's size. They are terrible at telling you the simplest fraction near it. Ask a decimal "what clean fraction is close to 3.14159265?" and it shrugs. A continued fraction answers immediately: 22/7, then 333/106, then 355/113. Each one is the tightest fraction you can write for its size of denominator, and they fall out of a single mechanical procedure you can run by hand or with the Continued Fraction Calculator.
This post walks through what the notation means, the one rule that generates the whole expansion, the worked example of pi, and why the fractions you read off are provably the best.
What a Continued Fraction Actually Is
A continued fraction writes a number as an integer plus one over an integer plus one over another integer, nested downward. Written out in full it stacks into an ugly tower, so we list the integers in a bracket instead. The value 3.245 becomes [3;4,1,9,...], which is shorthand for:
3 + 1/(4 + 1/(1 + 1/(9 + ...)))
The number before the semicolon is the whole part. The comma-separated numbers after it are the reciprocal layers. So [3;7,15] means 3 + 1/(7 + 1/15), which works out to 3 + 1/(106/15) = 3 + 15/106 = 333/106. The semicolon is just a convention that flags the integer part — that is why number theory references write [3;7,15] rather than [3,7,15].
The payoff of this base-free way of describing a number is that it exposes the number's best rational approximations directly. Decimals hide them; continued fractions hand them over.
The One Rule That Generates the Whole Expansion
Here is the entire algorithm, and it is worth memorizing because it is so short: take the integer part, then recurse on the reciprocal of what is left over.
Start with your number x. Write down its floor — that is your first coefficient a0. Subtract it, and you are left with a fractional part between 0 and 1. Flip it (take the reciprocal), and you have a new number bigger than 1. Take its floor for a1, subtract, flip again, and keep going. Each floor you write down is the next term in the bracket. The leftover keeps shrinking and flipping until either it hits zero (you are done) or you decide you have enough terms.
That is the whole engine. Floor, subtract, reciprocate, repeat.
For a plain fraction num/den, this loop is exactly the Euclidean algorithm. Expanding 415/93 is the same quotient sequence as computing gcd(415, 93): 415 ÷ 93 is 4 remainder 43, 93 ÷ 43 is 2 remainder 7, 43 ÷ 7 is 6 remainder 1, 7 ÷ 1 is 7 remainder 0. The quotients 4, 2, 6, 7 are the coefficients [4;2,6,7]. Because the remainders shrink to zero, every fraction terminates in finitely many steps. If you want to see that quotient sequence on its own, the GCD and LCM Calculator runs the same division ladder.
Worked Example: Where 22/7 Comes From
Let's expand pi and watch the schoolbook approximation appear.
Take x = 3.14159265. The floor is 3, so a0 = 3. Subtract: 0.14159265. Flip it: 1/0.14159265 ≈ 7.0625. The floor is 7, so a1 = 7. Subtract: 0.0625. Flip: 1/0.0625 ≈ 15.99, floor 15, so a2 = 15. One more flip gives roughly 1.003, floor 1, so a3 = 1. So far pi reads [3;7,15,1,...].
Now build the convergents — the fractions you get by truncating after each term:
- Truncate after a0: just 3.
- Truncate after a1: 3 + 1/7 = 22/7 = 3.142857, correct to two decimals.
- Truncate after a2:
[3;7,15]= 333/106 = 3.141509. - Truncate after a3:
[3;7,15,1]= 355/113 = 3.1415929, correct to six decimals.
There it is. The familiar 22/7 is simply pi's continued fraction cut after the second term, and 355/113 — the freakishly accurate one a Chinese astronomer found in the fifth century — is the next cut. The reason 355/113 is so good is sitting in the expansion: the term right after it is 292, a large coefficient. A big coefficient means the previous convergent was already nearly perfect, because the correction it adds, 1/292, is tiny.
The calculator prints each convergent as p/q alongside its decimal value and the signed error against your input, so you literally watch the error column shrink from about 1e-3 at 22/7 to 1e-7 at 355/113. The convergent recurrence runs on BigInt, so even a deep expansion stays exact rather than drifting on float rounding.
Why the Convergents Are the Best Approximations
A convergent is not just a good fraction near your number — it is the best one for its denominator. A classic theorem says each convergent p/q is the closest fraction to the target among all fractions whose denominator is no larger than q. Nothing with a denominator of 7 or smaller beats 22/7 as an approximation to pi. Nothing up to denominator 113 beats 355/113.
That is exactly why this matters in practice. The first time I needed it, I was cutting a gear pair and had a target ratio with seven digits no gear could hold. I expanded it, scanned the convergent column for a denominator my tooth count could actually reach, and picked it. I traded a microscopic accuracy loss for a buildable pair of integers — instead of chasing a ratio no physical gear can represent. The convergent table does the optimization for you: it hands you the tightest fraction at every level of precision, in order, and you stop at the one you can use.
Two Famous Patterns Worth Knowing
Two numbers have continued fractions so clean they are worth memorizing.
The square root of 2 is [1;2,2,2,...] — a 1 followed by an endless run of 2s. The golden ratio is even simpler: [1;1,1,1,...], all ones forever. The all-ones pattern is not a coincidence. The golden ratio satisfies φ = 1 + 1/φ, which is the continued fraction equation written directly. And because every coefficient is the smallest it can be (1), each convergent corrects the previous one by the largest possible amount, which makes the golden ratio the slowest-converging continued fraction there is — the hardest number to approximate well with small fractions. Its convergents are the ratios of consecutive Fibonacci numbers: 1/1, 2/1, 3/2, 5/3, 8/5, 13/8.
Decimals, Fractions, and the One Gotcha
You can feed the tool an integer, a decimal, or num/den. For a fraction it runs the exact Euclidean expansion; for a decimal it runs the floor-and-reciprocal loop and lets you cap the number of terms. If you start from a decimal and want to recover the underlying fraction first, the Decimal to Fraction Converter pairs naturally with this one.
The one gotcha: a decimal like 3.14159265 is a rational truncation of pi, not pi itself. Its continued fraction is finite, and the deep tail coefficients reflect your digits, not pi. So read the early convergents as genuine pi approximations, but do not treat the tail of a truncated decimal as the true continued fraction of the irrational. The famous terms — 3, 7, 15, 1, 292 — are real; the noise after them is just your rounding.
Continued fractions are one of those ideas that look like a curiosity and turn out to be the right tool whenever you need the simplest fraction near a number — gear ratios, calendar cycles, lattice problems, or just understanding why 22/7 was good enough for two thousand years. Expand a number, read the convergents, and stop where the denominator suits you.
Made by Toolora · Updated 2026-06-13