Square Root Convergents¶

Stop me if you've heard this one before: easy with SageMath.

In [1]:
convergents = continued_fraction(sqrt(2)).convergents()

cs = []
for c in convergents[1:1001]:
    n, d = c.as_integer_ratio()
    if len(n.digits()) > len(d.digits()):
        cs.append(c)
    
len(cs)
Out[1]:
153

Here's how to work this yourself.

If you were to look up the square root of 2, you would discover that the denominators of successive convergents of $\sqrt{2}$ form a sequence called the Pell numbers. The numerators are half of a related sequence called the Pell-Lucas numbers. We can easily make generators for these sequences from their definitions.

In [2]:
def pell_numbers():
    a, b = 0, 1
    while True:
        yield a
        a, b = 2*a + b, a


def pell_lucas_numbers():
    a, b = 2, 2
    yield a
    while True:
        yield a
        a, b = 2*a + b, a

With these generators, we can make a generator of the convergents of $\sqrt{2}$. We'll skip the first generated value since the first Pell number is 0.

In [3]:
convergents = ((p//2, q) for (p, q) in zip(pell_lucas_numbers(), pell_numbers()))
next(convergents)
Out[3]:
(1, 0)

Now we just iterate over the convergents and check the digits.

In [4]:
digits = lambda n: floor(1 + log(n, 10))

cs = []
for (i, (p, q)) in enumerate(convergents):
    if i >= 1000:
        break
        
    if digits(p) > digits(q):
        cs.append((p, q))
        
len(cs)
Out[4]:
153

Relevant sequences¶

  • Numerators of convergents of $\sqrt{2}$: A001333
  • Pell numbers (denominators of convergents of $\sqrt{2}$): A000129

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.