euler/ipython/EulerProblem046.ipynb
2018-12-22 18:44:11 -05:00

157 lines
3.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Goldbach's other conjecture (Euler Problem 46)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=46](https://projecteuler.net/problem=46)\n",
"\n",
"It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.\n",
"\n",
"$9 = 7 + 2×1^2$\n",
"\n",
"$15 = 7 + 2×2^2$\n",
"\n",
"$21 = 3 + 2×3^2$\n",
"\n",
"$25 = 7 + 2×3^2$\n",
"\n",
"$27 = 19 + 2×2^2$\n",
"\n",
"$33 = 31 + 2×1^2$\n",
"\n",
"It turns out that the conjecture was false.\n",
"\n",
"What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, we reuse Fermat's test and brute force. Easy."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def expmod(base, exp, m):\n",
" if exp == 0:\n",
" return 1\n",
" if (exp % 2 == 0):\n",
" return (expmod(base, exp // 2, m) ** 2 % m)\n",
" return (base * expmod(base, exp - 1, m) % m)\n",
"\n",
"def fermat_test(n):\n",
" a = n - 3\n",
" return expmod(a, n, n) == a"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def twice_square(n):\n",
" return 2 * n * n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"n_max = 10000\n",
"twice_squares = [twice_square(n) for n in range(1, n_max + 1)]\n",
"\n",
"def test_conjecture(n):\n",
" for ts in twice_squares:\n",
" if ts > n:\n",
" return False\n",
" if fermat_test(n - ts):\n",
" return True\n",
" \n",
"assert(test_conjecture(33))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5777\n"
]
}
],
"source": [
"for n in range(3, n_max + 1, 2):\n",
" if not fermat_test(n) and test_conjecture(n) == False:\n",
" s = n\n",
"\n",
"print(s)\n",
"assert(s == 5777)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "Sat, 22 Dec 2018, 23:39",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
"name": "python3"
},
"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.6.5"
},
"tags": [
"prime",
"goldbach",
"composite",
"square",
"fermat"
]
},
"nbformat": 4,
"nbformat_minor": 2
}