Solved 46.

main
Felix Martin 2018-12-22 18:44:11 -05:00
parent 08b1403bb8
commit 21716b5b1a
2 changed files with 163 additions and 3 deletions

View File

@ -31,7 +31,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def T(n):\n",
@ -53,7 +55,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"n_max = 100000\n",
@ -119,7 +123,7 @@
},
"tags": [
"triangular",
"pentagona",
"pentagonal",
"hexagonal"
]
},

View File

@ -0,0 +1,156 @@
{
"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
}