From 9bdbcb91f08fd7831df0ea17fe536acb202d9c05 Mon Sep 17 00:00:00 2001 From: filifa Date: Mon, 16 Jun 2025 22:31:40 -0400 Subject: [PATCH] add problem 87 --- notebooks/problem0087.ipynb | 108 ++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 notebooks/problem0087.ipynb diff --git a/notebooks/problem0087.ipynb b/notebooks/problem0087.ipynb new file mode 100644 index 0000000..faa339c --- /dev/null +++ b/notebooks/problem0087.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "a44edaf3", + "metadata": {}, + "source": [ + "# [Prime Power Triples](https://projecteuler.net/problem=87)\n", + "\n", + "First, we'll generate all the primes below $\\sqrt{50000000} \\approx 7071$ - since $7072^2 > 50000000$, we clearly don't need any larger primes." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "75ca6458", + "metadata": {}, + "outputs": [], + "source": [ + "limit = 50000000\n", + "primes = prime_range(isqrt(limit))" + ] + }, + { + "cell_type": "markdown", + "id": "6f022da4", + "metadata": {}, + "source": [ + "After this, it's just a matter of iterating through all these primes repeatedly to find triples $p^2 + q^3 + r^4 < 50000000$. For each term, we iterate from smallest to largest prime - that way, if we encounter a sum (or even just an individual power) greater than 50000000, we don't have to check any following prime (since the sum will also be greater than 50000000), so we can break early." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f0017332", + "metadata": {}, + "outputs": [], + "source": [ + "values = set()\n", + "for r in primes:\n", + " if r^4 >= limit:\n", + " break\n", + " \n", + " for q in primes:\n", + " if q^3 + r^4 >= limit:\n", + " break\n", + " \n", + " for p in primes:\n", + " n = p^2 + q^3 + r^4\n", + " if n >= limit:\n", + " break\n", + " \n", + " values.add(n)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "74114043", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1097343" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(values)" + ] + }, + { + "cell_type": "markdown", + "id": "8e625a1d", + "metadata": {}, + "source": [ + "## Relevant sequences\n", + "* Prime power triples: [A134657](https://oeis.org/A134657)" + ] + } + ], + "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 +}