diff --git a/Fibonacci.ipynb b/Fibonacci.ipynb
index 7cabb0e..f9c14e0 100644
--- a/Fibonacci.ipynb
+++ b/Fibonacci.ipynb
@@ -2,14 +2,20 @@
"cells": [
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"# Fibonacci Numbers [[1]](https://en.wikipedia.org/wiki/Fibonacci_number)"
]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"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",
@@ -18,7 +24,10 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"In mathematical terms, the sequence $F_n$ of Fibonacci numbers is defined by the recurrence relation:\n",
"\n",
@@ -31,7 +40,10 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"In matrix notation this definition is equivalent to:\n",
"\n",
@@ -63,7 +75,10 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"If we want to compute only the $n^{th}$ Fibonacci number, then the following identity is useful:\n",
"\n",
@@ -87,79 +102,1644 @@
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"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": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"### 1. Fast Fibonacci Transform Implementation"
]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"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": null,
+ "execution_count": 67,
"metadata": {
- "collapsed": true
+ "collapsed": false,
+ "deletable": true,
+ "editable": true
},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "ans = 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "function result = fast_fibonacci(seedValueZero, seedValueOne, iterationNumber)\n",
+ " seedValuesVector = [seedValueOne;seedValueZero];\n",
+ " transformMatrix = [1,1;1,0];\n",
+ " resultVector = transformMatrix ^ (iterationNumber-1) * seedValuesVector;\n",
+ " result = resultVector(1,1);\n",
+ "endfunction\n",
+ "\n",
+ "#example result for seed values F0=0, F1=1 and n=4\n",
+ "fast_fibonacci(0, 1, 2)"
+ ]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"### 2. Plot $F_{n+1} / F_n$ ratio"
]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"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": null,
+ "execution_count": 72,
"metadata": {
- "collapsed": true
+ "collapsed": false,
+ "deletable": true,
+ "editable": true
},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "x=1:100;\n",
+ "result_list = [];\n",
+ "for n = x\n",
+ " result_list(n) = fast_fibonacci(0,1,n+1) / fast_fibonacci(0,1,n);\n",
+ "endfor\n",
+ "\n",
+ "referance = (sqrt(5)+1)/2;\n",
+ "referance_list = ones(100)*referance;\n",
+ "\n",
+ "plot(x,result_list,x,referance_list,\":\")"
+ ]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"source": [
"### 3. Plot $F_{n+1} / F_n$ ratio starting with $F_0 = 2$ and $F_1 = 1 - \\sqrt{5}$"
]
},
{
"cell_type": "markdown",
- "metadata": {},
+ "metadata": {
+ "deletable": true,
+ "editable": true
+ },
"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,
+ "execution_count": 71,
"metadata": {
- "collapsed": true
+ "collapsed": false,
+ "deletable": true,
+ "editable": true
},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ ""
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "x=1:100;\n",
+ "result_list = [];\n",
+ "values = [2,1-sqrt(5)];\n",
+ "for n = x\n",
+ " values(n+2) = values(n) + values(n+1);\n",
+ " result_list(n) = values(n+1)/values(n);\n",
+ "endfor\n",
+ "\n",
+ "referance1 = (sqrt(5)+1)/2;\n",
+ "referance_list1 = ones(100)*referance;\n",
+ "\n",
+ "referance2 = (1-sqrt(5))/2;\n",
+ "referance_list2 = ones(100)*referance2;\n",
+ "\n",
+ "plot(x,result_list,x,referance_list1,\":\",x,referance_list2,\":\")"
+ ]
}
],
"metadata": {
@@ -171,6 +1751,14 @@
"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"
@@ -178,7 +1766,7 @@
],
"mimetype": "text/x-octave",
"name": "octave",
- "version": "0.16.1"
+ "version": "4.2.0"
}
},
"nbformat": 4,