{ "cells": [ { "cell_type": "markdown", "id": "a22e7878", "metadata": {}, "source": [ "# [Square Root Convergents](https://projecteuler.net/problem=57)\n", "\n", "Stop me if you've heard this one before: easy with SageMath." ] }, { "cell_type": "code", "execution_count": 1, "id": "27ac9cdb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "153" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convergents = continued_fraction(sqrt(2)).convergents()\n", "\n", "cs = []\n", "for c in convergents[1:1001]:\n", " n, d = c.as_integer_ratio()\n", " if len(n.digits()) > len(d.digits()):\n", " cs.append(c)\n", " \n", "len(cs)" ] }, { "cell_type": "markdown", "id": "8cc002c4", "metadata": {}, "source": [ "Here's how to work this yourself.\n", "\n", "If you were to look up the [square root of 2](https://en.wikipedia.org/wiki/Square_root_of_2), you would discover that the denominators of successive convergents of $\\sqrt{2}$ form a sequence called the [Pell numbers](https://en.wikipedia.org/wiki/Pell_number). The numerators are half of a related sequence called the Pell-Lucas numbers. We can easily make generators for these sequences from their definitions." ] }, { "cell_type": "code", "execution_count": 2, "id": "58c359a1", "metadata": {}, "outputs": [], "source": [ "def pell_numbers():\n", " a, b = 0, 1\n", " while True:\n", " yield a\n", " a, b = 2*a + b, a\n", "\n", "\n", "def pell_lucas_numbers():\n", " a, b = 2, 2\n", " yield a\n", " while True:\n", " yield a\n", " a, b = 2*a + b, a" ] }, { "cell_type": "markdown", "id": "c8588bfa", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": 3, "id": "308eaa91", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 0)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convergents = ((p//2, q) for (p, q) in zip(pell_lucas_numbers(), pell_numbers()))\n", "next(convergents)" ] }, { "cell_type": "markdown", "id": "9f7817c1", "metadata": {}, "source": [ "Now we just iterate over the convergents and check the digits." ] }, { "cell_type": "code", "execution_count": 4, "id": "ee53511d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "153" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits = lambda n: floor(1 + log(n, 10))\n", "\n", "cs = []\n", "for (i, (p, q)) in enumerate(convergents):\n", " if i >= 1000:\n", " break\n", " \n", " if digits(p) > digits(q):\n", " cs.append((p, q))\n", " \n", "len(cs)" ] }, { "cell_type": "markdown", "id": "88596f4c", "metadata": {}, "source": [ "## Relevant sequences\n", "* Numerators of convergents of $\\sqrt{2}$: [A001333](https://oeis.org/A001333)\n", "* Pell numbers (denominators of convergents of $\\sqrt{2}$): [A000129](https://oeis.org/A000129)\n", "\n", "#### Copyright (C) 2025 filifa\n", "\n", "This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)." ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 9.5", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }