From f1e56cfe934892f1ef7ca2d5b7708f29cf85810b Mon Sep 17 00:00:00 2001 From: filifa Date: Wed, 14 May 2025 21:20:21 -0400 Subject: [PATCH] add problem 57 --- notebooks/problem0057.ipynb | 175 ++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 notebooks/problem0057.ipynb diff --git a/notebooks/problem0057.ipynb b/notebooks/problem0057.ipynb new file mode 100644 index 0000000..737071f --- /dev/null +++ b/notebooks/problem0057.ipynb @@ -0,0 +1,175 @@ +{ + "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)" + ] + } + ], + "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 +}