2018-02-01 16:45:03 +01:00
{
"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,
2018-02-01 18:53:44 +01:00
"metadata": {
"collapsed": true
},
2018-02-01 16:45:03 +01:00
"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,
2018-02-01 18:53:44 +01:00
"metadata": {
"collapsed": true
},
2018-02-01 16:45:03 +01:00
"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,
2018-02-01 18:53:44 +01:00
"metadata": {
"collapsed": false
},
2018-02-01 16:45:03 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4613732\n"
]
}
],
"source": [
"f = get_even_fibonaccis_smaller_or_equal_four_million()\n",
"print(sum(f))"
]
2018-02-01 18:53:44 +01:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I looked at the solutions in the forum and I kind of forgot about simple straight forward approaches. There is no need to create a list and the sum it up. Instead I can simply increment a counter which will be much faster, but less readable maybe."
]
},
{
"cell_type": "code",
2018-02-01 19:13:48 +01:00
"execution_count": 1,
2018-02-01 18:53:44 +01:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4613732\n"
]
}
],
"source": [
"r, a, b = 0, 0, 1\n",
2018-02-01 19:13:48 +01:00
"\n",
2018-02-01 18:53:44 +01:00
"while b <= 4000000:\n",
2018-02-01 19:13:48 +01:00
" if b % 2 == 0:\n",
" r += b\n",
2018-02-01 18:53:44 +01:00
" a, b = b, a + b\n",
2018-02-01 19:13:48 +01:00
" \n",
2018-02-01 18:53:44 +01:00
"print(r)"
]
2018-02-01 16:45:03 +01:00
}
],
"metadata": {
2018-02-04 16:54:28 +01:00
"completion_date": "Tue, 19 Aug 2014, 20:36",
2018-02-01 16:45:03 +01:00
"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",
2018-02-01 18:53:44 +01:00
"version": "3.5.4"
2018-02-04 15:21:29 +01:00
},
"tags": [
"fibonacci"
]
2018-02-01 16:45:03 +01:00
},
"nbformat": 4,
"nbformat_minor": 2
}