Added ipython notebooks and html reports. This allows to solve the exercises documented in the future.
This commit is contained in:
11858
html/EulerProblem001.html
Executable file
11858
html/EulerProblem001.html
Executable file
File diff suppressed because it is too large
Load Diff
11906
html/EulerProblem002.html
Executable file
11906
html/EulerProblem002.html
Executable file
File diff suppressed because it is too large
Load Diff
81
ipython/EulerProblem001.ipynb
Executable file
81
ipython/EulerProblem001.ipynb
Executable file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 1\n",
|
||||
"\n",
|
||||
"If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n",
|
||||
"\n",
|
||||
"Find the sum of all the multiples of 3 or 5 below 1000."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_sum_of_natural_dividable_by_3_and_5_below(m):\n",
|
||||
" return sum([x for x in range(m) if x % 3 == 0 or x % 5 == 0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Test example provided in problem statement:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assert(get_sum_of_natural_dividable_by_3_and_5_below(10) == 23)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"233168\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(get_sum_of_natural_dividable_by_3_and_5_below(1000))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
122
ipython/EulerProblem002.ipynb
Executable file
122
ipython/EulerProblem002.ipynb
Executable file
@@ -0,0 +1,122 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 2\n",
|
||||
"\n",
|
||||
"Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:\n",
|
||||
"\n",
|
||||
"1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\n",
|
||||
"\n",
|
||||
"By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def fibonacci_generator():\n",
|
||||
" a = 0\n",
|
||||
" b = 1\n",
|
||||
" while True:\n",
|
||||
" yield a + b\n",
|
||||
" a, b = b, (a + b)\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Test fibonacci sequence generator by comparing the result to the example in the task statement."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"i = fibonacci_generator()\n",
|
||||
"fib_10 = [next(i) for _ in range(10)]\n",
|
||||
"assert(fib_10 == [1, 2, 3, 5, 8, 13, 21, 34, 55, 89,])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Greate a function which returns all even-valued fibonacci values smaller or equal to four million."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_even_fibonaccis_smaller_or_equal_four_million():\n",
|
||||
" r = []\n",
|
||||
" i = fibonacci_generator()\n",
|
||||
" current_value = next(i)\n",
|
||||
" while current_value <= 4000000:\n",
|
||||
" if current_value % 2 == 0:\n",
|
||||
" r.append(current_value)\n",
|
||||
" current_value = next(i)\n",
|
||||
" return r"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Calculate the solution."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4613732\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"f = get_even_fibonaccis_smaller_or_equal_four_million()\n",
|
||||
"print(sum(f))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user