Introduction

We all reach for 22/7 when we need π in a hurry, and astronomers swear by 355/113, which nails π to six decimals. These are not lucky guesses. They fall out of one of the most elegant ideas in number theory: the continued fraction.

The recipe is almost childishly simple. Take any number, write down its whole part, then flip the leftover fraction over and repeat. Do that to π and you get 3, then 7, then 15, then 1, then 292… Stop anywhere and you have a fraction — and that fraction is, in a precise sense, the best possible for its size.

This little peeling process is everywhere it counts: it is why a leap year lands every four years (but not every century), why a clockmaker chooses one gear-tooth count over another, and how mathematicians cracked some of the deepest questions about irrational numbers. Continued fractions are not an open problem or an impossibility result — they are established mathematics, with the best-approximation property proved by Joseph-Louis Lagrange in the 18th century. What makes them worth your time is how much they explain.

Try It: Watch the Convergents Close In

Choose a number below — or type your own — and the demo will peel it into a continued fraction. Each row is a convergent: the fraction you get by stopping at that term. Watch the error column shrink dramatically with every step, and notice the famous fractions appearing on their own.

<p class="hint">Pick a number or type your own, then peel it into a continued fraction.
    Each row is a <b>convergent</b> — the fraction you get by stopping there. Watch the
    <b>error</b> column collapse, and the famous fractions appear on their own.</p>
<div class="controls">
  <button type="button" data-x="3.141592653589793" data-n="π">π</button>
  <button type="button" data-x="1.618033988749895" data-n="golden ratio φ">φ (golden)</button>
  <button type="button" data-x="1.4142135623730951" data-n="√2">√2</button>
  <button type="button" data-x="2.718281828459045" data-n="e">e</button>
  <button type="button" data-x="365.2422" data-n="days in a year">365.2422 (year)</button>
</div>
<div class="custom">
  <input id="num" type="text" value="3.141592653589793" aria-label="number" />
  <button id="go" type="button">Expand</button>
</div>
<div class="cf" id="cf"></div>
<table class="conv"><thead><tr><th>term</th><th>convergent</th><th>value</th><th>error</th></tr></thead>
<tbody id="rows"></tbody></table>
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; color: #222; margin: 0; }
.hint { font-size: .9rem; color: #444; margin: 0 0 .7rem; line-height: 1.45; }
.controls { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: .5rem; }
.controls button { font: 600 13px system-ui, sans-serif; padding: .4rem .7rem; border: 1px solid #1d3557;
  background: #eef3f8; color: #1d3557; border-radius: 8px; cursor: pointer; }
.controls button:hover { background: #dce6f0; }
.custom { display: flex; gap: .4rem; margin-bottom: .8rem; }
.custom input { flex: 1; font: 600 14px ui-monospace, monospace; padding: .4rem .6rem;
  border: 1px solid #cdd9e3; border-radius: 8px; min-width: 0; }
.custom button { font: 600 14px system-ui, sans-serif; padding: .4rem .9rem; border: 1px solid #1d3557;
  background: #1d3557; color: #fff; border-radius: 8px; cursor: pointer; }
.cf { font: 600 15px ui-monospace, monospace; color: #1d3557; background: #f3f6fa;
  border: 1px solid #dde6ee; border-radius: 8px; padding: .6rem .7rem; margin-bottom: .8rem;
  word-break: break-word; }
table.conv { border-collapse: collapse; width: 100%; font-size: .9rem; }
table.conv th, table.conv td { text-align: left; padding: .35rem .5rem; border-bottom: 1px solid #e6ecf2; }
table.conv th { color: #5a7088; font-weight: 700; }
table.conv td { font-family: ui-monospace, monospace; }
td.frac { font-weight: 700; color: #1d3557; }
td.err { color: #0a7d33; }
tr.famous td.frac { background: #fff3cd; border-radius: 6px; }
// Continued fraction expansion: write the whole part, take the reciprocal of the
// leftover, repeat. Convergents h_n/k_n use the recurrence
//   h_n = a_n*h_{n-1} + h_{n-2},  k_n = a_n*k_{n-1} + k_{n-2}.
// Lagrange's theorem (18th c.): each convergent is the BEST rational approximation
// among all fractions with denominator <= its own. Classical, solved mathematics.
const FAMOUS = new Set(['22/7','355/113','3/2','7/5','17/12','1/4','365/1']);

function expand(x){
  const terms=[]; const conv=[];
  let v=x, hm1=1,hm2=0,km1=0,km2=1;
  for(let i=0;i<12;i++){
    const a=Math.floor(v); terms.push(a);
    const h=a*hm1+hm2, k=a*km1+km2;
    conv.push([h,k]);
    hm2=hm1; hm1=h; km2=km1; km1=k;
    const frac=v-a;
    if(frac < 1e-10) break;   // rational / numerical floor reached
    v=1/frac;
    if(k > 1e9) break;        // keep denominators readable
  }
  return {terms, conv};
}

function fmtCF(terms){
  if(terms.length===1) return '[' + terms[0] + ']';
  return '[' + terms[0] + '; ' + terms.slice(1).join(', ') + ']';
}

function show(x, name){
  const {terms, conv} = expand(x);
  document.getElementById('cf').textContent =
    (name?name+'  =  ':'') + fmtCF(terms);
  const tb=document.getElementById('rows'); tb.innerHTML='';
  conv.forEach(([h,k],i)=>{
    const val=h/k, err=Math.abs(x-val);
    const tr=document.createElement('tr');
    const key=h+'/'+k;
    if(FAMOUS.has(key)) tr.className='famous';
    tr.innerHTML =
      '<td>'+terms[i]+'</td>'+
      '<td class="frac">'+key+'</td>'+
      '<td>'+val.toFixed(8)+'</td>'+
      '<td class="err">'+(err===0?'exact':err.toExponential(2))+'</td>';
    tb.appendChild(tr);
  });
  console.log(name||x, '->', fmtCF(terms),
    '| best convergent', conv[conv.length-1].join('/'));
}

document.querySelectorAll('.controls button').forEach(b=>{
  b.addEventListener('click',()=>{
    document.getElementById('num').value=b.dataset.x;
    show(parseFloat(b.dataset.x), b.dataset.n);
  });
});
document.getElementById('go').addEventListener('click',()=>{
  const x=parseFloat(document.getElementById('num').value);
  if(isFinite(x)) show(x, '');
  else document.getElementById('cf').textContent='Enter a valid number.';
});
show(Math.PI, 'π');
Result
Console

Try π and watch 22/7 and then 355/113 pop out. Try the golden ratio (the hardest number to approximate) and see only 1s — its convergents are ratios of consecutive Fibonacci numbers, and they close in as slowly as any number possibly can. The key fact: no fraction with a smaller denominator ever beats one of these convergents.

The Real Math

What exactly makes these fractions best? Here is the precise picture.

  • The algorithm is just division. Write the whole part, take the reciprocal of the remainder, repeat. For a rational number it stops; for an irrational it runs forever.
  • Convergents come from a clean recurrence. If the terms are a0a_{0}, a1a_{1}, a2a_{2}, … then numerators and denominators each follow hn=anhn−1+hn−2h_n = a_n h_{n-1} + h_{n-2} and kn=ankn−1+kn−2k_n = a_n k_{n-1} + k_{n-2}. Cheap to compute, exact in integer arithmetic.
  • They are provably the best approximations. This is Lagrange's theorem: each convergent p/qp/q is closer to the number than any other fraction with denominator ≤q\le q. You cannot do better without using bigger numbers.
  • A big term means a great shortcut. The 292 in π's expansion is why 355/113 is so spectacularly accurate — a large term means the previous convergent was already almost perfect.
  • All 1s is the worst case. The golden ratio φ=[1; 1,1,1,…]\varphi = [1;\, 1, 1, 1, \ldots] is the hardest number to approximate by rationals; that is exactly what makes it the "most irrational" number.

None of this is conjectural. The status is solved, classical mathematics — Euler and Lagrange settled it in the 1700s. Continued fractions sit on the easy side of computation: expanding a number and reading off its best fractions is fast and exact, a refreshing contrast to the hard problems like factoring elsewhere on KipuHub.

Where It Matters

"Find the simplest fraction that is close enough" is a question engineers and scientists ask constantly, and continued fractions answer it optimally:

  • Calendars. The year is about 365.2422 days. Its convergents give 365 + 1/4 (the Julian rule) and refinements close to the modern 97-leap-days-per-400-years Gregorian rule. Leap years are a continued fraction.
  • Gears and clocks. To get an exact ratio with a buildable number of teeth, gearmakers pick a convergent — the best ratio achievable with small wheels.
  • Music. Twelve equal semitones approximate a stack of perfect fifths because 7/12 is a great convergent of log⁡2(3/2)\log_{2}(3/2).
  • Cryptanalysis. The Wiener attack breaks RSA keys with a small secret exponent by reading it off a continued fraction — the same best-approximation property turned into a weapon, related to the hardness of factoring.

The thread is always the same: the most efficient rational stand-in for an awkward real number. For the contrast with problems that have no efficient answer, see P vs NP.

Conclusion

Continued fractions take a messy real number and hand back a ladder of fractions, each one the best you could possibly write at that size. It is why 22/7 and 355/113 feel magical, why the calendar skips a leap year three times every four centuries, and why the golden ratio earns its title as the most irrational number.

Best of all, this is a corner of mathematics that is completely solved — no asterisks, no open conjectures, just a beautiful, exact, fast algorithm. After all the impossibility results and hard problems on KipuHub, like P vs NP, it is worth savoring one idea that simply works, and works perfectly.

Share this article

Pick a channel — or use your device's native share sheet.

Comments

Loading comments...

https://www.kipuhub.com/en/article/continued-fractions/Content licensed under CC BY-NC 4.0.