eulerbooks/notebooks/problem0046.ipynb

98 lines
2.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "42fd0b2c",
"metadata": {},
"source": [
"# [Goldbach's Other Conjecture](https://projecteuler.net/problem=46)\n",
"\n",
"We can write a function that takes odd composites $m$ and checks successively larger values of $n$ to see if $m - 2n^2$ is prime. If we find such an $n$, $m$ satisfies the conjecture, but if $n$ gets too large, $m - 2n^2$ will become non-positive, and therefore can't be prime, so $m$ fails to satisfy the conjecture."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "eba09d52",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5777"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import count\n",
"\n",
"def satisfies_conjecture(m):\n",
" for n in count(1):\n",
" p = m - 2 * n^2\n",
" if p <= 0:\n",
" return False\n",
" \n",
" if is_prime(p):\n",
" return True\n",
"\n",
"\n",
"for k in count(2):\n",
" m = 2 * k - 1\n",
" if is_prime(m):\n",
" continue\n",
" \n",
" if not satisfies_conjecture(m):\n",
" break\n",
"\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "dd35bb73",
"metadata": {},
"source": [
"Interestingly, the only other number known not to satisfy this conjecture is 5993."
]
},
{
"cell_type": "markdown",
"id": "3412975d",
"metadata": {},
"source": [
"## Relevant sequences\n",
"* Stern numbers (includes all odd numbers, not just composites): [A060003](https://oeis.org/A060003)\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
}