101 lines
2.0 KiB
Plaintext
101 lines
2.0 KiB
Plaintext
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# Euler Problem 25\n",
|
|||
|
"\n",
|
|||
|
"The Fibonacci sequence is defined by the recurrence relation:\n",
|
|||
|
"\n",
|
|||
|
"$F_n = F_{n−1} + F_{n−2}$, where $F_1 = 1$ and $F_2 = 1$.\n",
|
|||
|
"Hence the first 12 terms will be:\n",
|
|||
|
"\n",
|
|||
|
"~~~\n",
|
|||
|
"F1 = 1\n",
|
|||
|
"F2 = 1\n",
|
|||
|
"F3 = 2\n",
|
|||
|
"F4 = 3\n",
|
|||
|
"F5 = 5\n",
|
|||
|
"F6 = 8\n",
|
|||
|
"F7 = 13\n",
|
|||
|
"F8 = 21\n",
|
|||
|
"F9 = 34\n",
|
|||
|
"F10 = 55\n",
|
|||
|
"F11 = 89\n",
|
|||
|
"F12 = 144\n",
|
|||
|
"~~~\n",
|
|||
|
"\n",
|
|||
|
"The 12th term, $F_{12}$, is the first term to contain three digits.\n",
|
|||
|
"\n",
|
|||
|
"What is the index of the first term in the Fibonacci sequence to contain 1000 digits?"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Reuse fibonacci generator and then straight forward."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"4782\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"def fibonacci_generator():\n",
|
|||
|
" a = 0\n",
|
|||
|
" b = 1\n",
|
|||
|
" yield 1\n",
|
|||
|
" while True:\n",
|
|||
|
" yield a + b\n",
|
|||
|
" a, b = b, (a + b)\n",
|
|||
|
" \n",
|
|||
|
"for i, f in enumerate(fibonacci_generator()):\n",
|
|||
|
" if len(str(f)) >= 1000:\n",
|
|||
|
" break\n",
|
|||
|
"\n",
|
|||
|
"s = i + 1\n",
|
|||
|
"assert(s == 4782)\n",
|
|||
|
"print(s)"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"completion_date": "Fri, 5 Sep 2014, 14:57",
|
|||
|
"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": 0
|
|||
|
}
|