eulerbooks/notebooks/problem0049.ipynb

148 lines
3.4 KiB
Plaintext

{
"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": [
"from itertools import combinations\n",
"\n",
"progressions = set()\n",
"for (p, q) in combinations(primes, 2):\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": [
"for (p, q, r) in progressions:\n",
" if p == 1487 and q == 4817 and r == 8147:\n",
" continue\n",
" \n",
" if sorted(p.digits()) == sorted(q.digits()) == sorted(r.digits()):\n",
" break\n",
" \n",
"p, q, r"
]
},
{
"cell_type": "markdown",
"id": "929e777d",
"metadata": {},
"source": [
"#### 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
}