euler/ipython/EulerProblem002.ipynb

166 lines
3.6 KiB
Plaintext

{
"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": {
"collapsed": true
},
"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": {
"collapsed": true
},
"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": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4613732\n"
]
}
],
"source": [
"f = get_even_fibonaccis_smaller_or_equal_four_million()\n",
"print(sum(f))"
]
},
{
"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",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4613732\n"
]
}
],
"source": [
"r, a, b = 0, 0, 1\n",
"\n",
"while b <= 4000000:\n",
" if b % 2 == 0:\n",
" r += b\n",
" a, b = b, a + b\n",
" \n",
"print(r)"
]
}
],
"metadata": {
"completion_date": "Tue, 19 Aug 2014, 20:36",
"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.5.4"
},
"tags": [
"fibonacci"
]
},
"nbformat": 4,
"nbformat_minor": 2
}