diff --git a/notebooks/problem0049.ipynb b/notebooks/problem0049.ipynb new file mode 100644 index 0000000..5ebff28 --- /dev/null +++ b/notebooks/problem0049.ipynb @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "9c45cbb8", + "metadata": {}, + "source": [ + "# [Prime Permutations](https://projecteuler.net/problem=49)\n", + "\n", + "An easy way to approach this problem is to first find [arithmetic progressions of primes](https://en.wikipedia.org/wiki/Primes_in_arithmetic_progression), then check if any of those progressions have numbers that are permutations of each other.\n", + "\n", + "To start, we'll generate all the four-digit primes." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d278b9e4", + "metadata": {}, + "outputs": [], + "source": [ + "primes = prime_range(1000, 10000)" + ] + }, + { + "cell_type": "markdown", + "id": "97dab665", + "metadata": {}, + "source": [ + "Here's where we find arithmetic progressions. Given two primes $p < q$, we can calculate their difference $d = q - p$ and check if $q + d$ is prime - if it is, we have a three-number progression." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "0eaf16ac", + "metadata": {}, + "outputs": [], + "source": [ + "progressions = set()\n", + "for (i, p) in enumerate(sorted(primes)):\n", + " for q in primes[i+1:]:\n", + " d = q - p\n", + " r = q + d\n", + " if r < 10000 and is_prime(r):\n", + " progressions.add((p, q, r))" + ] + }, + { + "cell_type": "markdown", + "id": "bbbcab4f", + "metadata": {}, + "source": [ + "Here's how many of these progressions there are." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "8c87fea2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "42994" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(progressions)" + ] + }, + { + "cell_type": "markdown", + "id": "da3a48f3", + "metadata": {}, + "source": [ + "Now we'll check each progression to find one where the digits of the numbers are permutations of each other." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "89cb3dd0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2969, 6299, 9629)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from collections import Counter\n", + "\n", + "for (p, q, r) in progressions:\n", + " if p == 1487 and q == 4817 and r == 8147:\n", + " continue\n", + " \n", + " if Counter(p.digits()) == Counter(q.digits()) == Counter(r.digits()):\n", + " break\n", + " \n", + "p, q, r" + ] + } + ], + "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 +}