Started with 28/29 in ipython.

This commit is contained in:
2018-02-09 13:50:48 +01:00
parent 3f6995eabe
commit 2e825d4f78
2 changed files with 153 additions and 0 deletions

94
ipython/EulerProblem028.ipynb Executable file
View File

@@ -0,0 +1,94 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 28\n",
"\n",
"Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:\n",
"\n",
"~~~\n",
"21 22 23 24 25\n",
"20 7 8 9 10\n",
"19 6 1 2 11\n",
"18 5 4 3 12\n",
"17 16 15 14 13\n",
"~~~\n",
"\n",
"$1 + 3 + 5 + 7 + 9 + 13 + 17 + 21 + 25 = 101$\n",
"\n",
"It can be verified that the sum of the numbers on the diagonals is 101.\n",
"\n",
"What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I would try to create a function $f(n)$ which yields the sum of the outmost ring of a n by n spiral.\n",
"\n",
"For example:\n",
"\n",
"$f(1) = 1$\n",
"\n",
"$f(3) = 3 + 5 + 7 + 9 = 24$\n",
"\n",
"$f(5) = 13 + 17 + 21 + 25 = 76$\n",
"\n",
"When we have this function we calculate the solution simply by\n",
"\n",
"~~~\n",
"s = sum([f(n) for n in range(1, 1002, 2)])\n",
"~~~\n",
"\n",
"For each outer ring there is an initial corner value c ($c_3 = 3, c_5 = 76$). Once we have this value we can caluclate f like $f(n) = c_{n} + (c_n + n - 1) + (c_n + 2(n-1)) + (c_n + 3(n-1)) = 4c_n + 6 (n-1)$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f(n):\n",
" if n == 1:\n",
" return 1\n",
" return 0\n",
"\n",
"s = sum([f(n) for n in range(1, 1002, 2)])\n",
"assert(s == 669171001)\n",
"s"
]
}
],
"metadata": {
"completion_date": "Wed, 23 Aug 2017, 15:54",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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.3"
},
"tags": [
"spiral",
"diagonals"
]
},
"nbformat": 4,
"nbformat_minor": 2
}

59
ipython/EulerProblem029.ipynb Executable file
View File

@@ -0,0 +1,59 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 29\n",
"\n",
"Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:\n",
"\n",
"$2^2=4, 2^3=8, 2^4=16, 2^5=32$\n",
"\n",
"32=9, 33=27, 34=81, 35=243\n",
"\n",
"42=16, 43=64, 44=256, 45=1024\n",
"\n",
"52=25, 53=125, 54=625, 55=3125\n",
"\n",
"If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:\n",
"\n",
"4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125\n",
"\n",
"How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"comopletion_date": "",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"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.3"
},
"tags": []
},
"nbformat": 4,
"nbformat_minor": 2
}