Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 241 additions & 0 deletions .ipynb_checkpoints/Fibonacci-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fibonacci Numbers [[1]](https://en.wikipedia.org/wiki/Fibonacci_number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:\n",
"\n",
"$$ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \\cdots$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In mathematical terms, the sequence $F_n$ of Fibonacci numbers is defined by the recurrence relation:\n",
"\n",
"$$ F_{n}=F_{n-1}+F_{n-2} $$\n",
"\n",
"with seed values:\n",
"$$ F_0 = 0 , F_1 = 1 $$\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In matrix notation this definition is equivalent to:\n",
"\n",
"\\begin{eqnarray}\n",
" \\begin{bmatrix}\n",
" F_1 \\\\\n",
" F_0\n",
" \\end{bmatrix}\n",
" & = &\n",
" \\begin{bmatrix}\n",
" 1 \\\\\n",
" 0\n",
" \\end{bmatrix} \\\\\n",
" \\begin{bmatrix}\n",
" F_{n+1} \\\\\n",
" F_n\n",
" \\end{bmatrix}\n",
" & = &\n",
" \\begin{bmatrix}\n",
" 1 & 1 \\\\\n",
" 1 & 0\n",
" \\end{bmatrix}\n",
" \\begin{bmatrix}\n",
" F_n \\\\\n",
" F_{n-1}\n",
" \\end{bmatrix}\n",
"\\end{eqnarray}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we want to compute only the $n^{th}$ Fibonacci number, then the following identity is useful:\n",
"\n",
"\\begin{equation}\n",
" \\begin{bmatrix}\n",
" F_{n} \\\\\n",
" F_{n-1}\n",
" \\end{bmatrix} =\n",
" \\begin{bmatrix}\n",
" 1 & 1 \\\\\n",
" 1 & 0\n",
" \\end{bmatrix}^{n-1}\n",
" \\begin{bmatrix}\n",
" F_1 \\\\\n",
" F_0\n",
" \\end{bmatrix}\n",
"\\end{equation}\n",
"\n",
"By using matrix exponentiation (for instance, calculating $M^8$ as $((M^2)^2)^2$ ), $F_{n}$ can be calculated in $O(log(n))$ time complexity. This algorithm is sometimes called **fast fibonacci transform**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Follow the instructions in the next sections. Feel free to create extra cells (for instance, you can try different values for $F_1$ and $F_0$).**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Fast Fibonacci Transform Implementation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a function that returns $F_n$ as we described above (for this assignment we are not concerned about the efficiency of your implementation, i.e. you can use $M^n$ assuming octave does matrix exponentiation for you):"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function Fn = FFT (n)\n",
" initial=([1;0]);\n",
" if(n == 0 )\n",
" Fn= initial(2,1);\n",
" elseif (n == 1)\n",
" Fn= initial(1,1);\n",
" else \n",
" mm=([1,1;1,0]);\n",
" pivot=([1,1;1,0]);\n",
" for i=1:n-2\n",
" mm=mm*pivot;\n",
" endfor\n",
" Fn=(mm*initial)(1,1);\n",
" endif\n",
"endfunction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Plot $F_{n+1} / F_n$ ratio"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize $F_0 = 0$ and $F_1 = 1$, then plot the $\\dfrac{F_{n+1}}{F_{n}}$ values for $ 1 \\leq n \\leq 100$. As $n \\to \\infty$, we expect $\\dfrac{F_{n+1}}{F_{n}} \\to \\dfrac{\\sqrt{5}+1}{2}$. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for i=1:100\n",
" result= FFT(i+1)/FFT(i);\n",
" x(i)=i;\n",
" y(i)=result;\n",
"endfor\n",
"plot(x,y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Plot $F_{n+1} / F_n$ ratio starting with $F_0 = 2$ and $F_1 = 1 - \\sqrt{5}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize $F_0 = 2$ and $F_1 = F_1 = 1 - \\sqrt{5}$, then plot the $\\dfrac{F_{n+1}}{F_{n}}$ values for $ 1 \\leq n \\leq 100$. If we would represent $\\sqrt{5}$ exactly in our floating point arithmetic, then as $n \\to \\infty$, we expect $\\dfrac{F_{n+1}}{F_{n}} \\to \\dfrac{1 - \\sqrt{5}}{2}$, but for the very large values of $n$, this ratio unexpectedly converges to $\\dfrac{\\sqrt{5} + 1}{2}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function newFn = newFFT (n)\n",
" initial=([(1-sqrt(5));2]);\n",
" if(n == 0 )\n",
" newFn= initial(2,1);\n",
" elseif (n == 1)\n",
" newFn= initial(1,1);\n",
" else \n",
" mm=([1,1;1,0]);\n",
" pivot=([1,1;1,0]);\n",
" for i=1:n-2\n",
" mm=mm*pivot;\n",
" endfor\n",
" newFn=(mm*initial)(1,1);\n",
" endif\n",
"endfunction\n",
"\n",
"for i=1:100\n",
" result= newFFT(i+1)/newFFT(i);\n",
" x(i)=i;\n",
" y(i)=result;\n",
"endfor\n",
"plot(x,y)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Octave",
"language": "octave",
"name": "octave"
},
"language_info": {
"file_extension": ".m",
"help_links": [
{
"text": "GNU Octave",
"url": "https://www.gnu.org/software/octave/support.html"
},
{
"text": "Octave Kernel",
"url": "https://github.com/Calysto/octave_kernel"
},
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-octave",
"name": "octave",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
67 changes: 61 additions & 6 deletions Fibonacci.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"function Fn = FFT (n)\n",
" initial=([1;0]);\n",
" if(n == 0 )\n",
" Fn= initial(2,1);\n",
" elseif (n == 1)\n",
" Fn= initial(1,1);\n",
" else \n",
" mm=([1,1;1,0]);\n",
" pivot=([1,1;1,0]);\n",
" for i=1:n-2\n",
" mm=mm*pivot;\n",
" endfor\n",
" Fn=(mm*initial)(1,1);\n",
" endif\n",
"endfunction"
]
},
{
"cell_type": "markdown",
Expand All @@ -131,12 +147,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"for i=1:100\n",
" result= FFT(i+1)/FFT(i);\n",
" x(i)=i;\n",
" y(i)=result;\n",
"endfor\n",
"plot(x,y)"
]
},
{
"cell_type": "markdown",
Expand All @@ -159,10 +182,34 @@
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"function newFn = newFFT (n)\n",
" initial=([(1-sqrt(5));2]);\n",
" if(n == 0 )\n",
" newFn= initial(2,1);\n",
" elseif (n == 1)\n",
" newFn= initial(1,1);\n",
" else \n",
" mm=([1,1;1,0]);\n",
" pivot=([1,1;1,0]);\n",
" for i=1:n-2\n",
" mm=mm*pivot;\n",
" endfor\n",
" newFn=(mm*initial)(1,1);\n",
" endif\n",
"endfunction\n",
"\n",
"for i=1:100\n",
" result= newFFT(i+1)/newFFT(i);\n",
" x(i)=i;\n",
" y(i)=result;\n",
"endfor\n",
"plot(x,y)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Octave",
"language": "octave",
Expand All @@ -171,14 +218,22 @@
"language_info": {
"file_extension": ".m",
"help_links": [
{
"text": "GNU Octave",
"url": "https://www.gnu.org/software/octave/support.html"
},
{
"text": "Octave Kernel",
"url": "https://github.com/Calysto/octave_kernel"
},
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-octave",
"name": "octave",
"version": "0.16.1"
"version": "3.6.4"
}
},
"nbformat": 4,
Expand Down