{ "cells": [ { "cell_type": "markdown", "id": "552c0f62-f482-4262-8860-32dfc4dcd60c", "metadata": {}, "source": [ "# Fibonacci numbers" ] }, { "cell_type": "markdown", "id": "2dd0ca2e-e59a-4deb-a033-433656546fa1", "metadata": {}, "source": [ "\n", "In mathematics, the _Fibonacci sequence_ is a sequence in which each element is the sum of the two elements that precede it. \n", "Numbers that are part of the Fibonacci sequence are known as **Fibonacci numbers**, commonly denoted $F_n$ " ] }, { "cell_type": "markdown", "id": "76d1f24a-cfd8-4377-9a6b-996bab91872d", "metadata": {}, "source": [ "\n", "The Fibonacci numbers may be defined by the *recurrence relation*\n", "$F_1 = 1$, $F_2 = 1$, and $F_{n} = F_{n-1} + F_{n-2}$ for $n > 2$." ] }, { "cell_type": "code", "execution_count": null, "id": "3818ad2b-0ed6-46da-a618-93fbe9fe34c2", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "\n", "\"\"\"\n", " myfib(n)\n", "\n", "Calculate Fibonacci number F_n using the recurrence relation\n", "F_1 = 1, F_2 = 1, and F_n = F_n-1 + F_n-2 for n > 2.\n", "\"\"\"\n", "function myfib(n)\n", " fp = 1\n", " if n == 1\n", " return fp\n", " end\n", "\n", " fc = 1\n", " if n == 2\n", " return fc\n", " end\n", "\n", " for i = 3:n\n", " fc, fp = fc + fp, fc\n", " end\n", "\n", " return fc\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "2f25f061-5178-42a0-8bba-f5696e273e8b", "metadata": {}, "outputs": [], "source": [ "\n", "?myfib" ] }, { "cell_type": "code", "execution_count": null, "id": "6f8e8ed5-09ef-4e61-9534-f9ef1141338a", "metadata": {}, "outputs": [], "source": [ "\n", "myfib.(1:9)" ] }, { "cell_type": "markdown", "id": "284e2246-b9b0-4709-a877-004efd0d751c", "metadata": {}, "source": [ "\n", "A *recursive function* is a programming technique where a function calls itself to solve a problem, breaking it down into simpler, smaller instances of the same problem. \n", "It requires a base case, which is a condition that stops the function from calling itself indefinitely, and a recursive step, where the function calls itself with modified \n", "input to move closer to the base case. " ] }, { "cell_type": "code", "execution_count": null, "id": "eb6ca272-6628-4caf-8499-5bda294d8b28", "metadata": {}, "outputs": [], "source": [ "\n", "\"\"\"\n", " myfib_recursive(n)\n", "\n", "Calculate Fibonacci number F_n using the _recurrence relation_\n", "F_1 = 1, F_2 = 1, and F_n = F_n-1 + F_n-2 for n > 2, \n", "and recursive function calls. \n", "\"\"\"\n", "function myfib_recursive(n)\n", "\n", " # Base case\n", " if n == 1\n", " return 1\n", " elseif n == 2\n", " return 1\n", " end\n", "\n", " # Recursive step\n", " return myfib_recursive(n-1) + myfib_recursive(n-2)\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "af60f5a6-b4c4-47a0-90c7-145681b188d1", "metadata": {}, "outputs": [], "source": [ "\n", "myfib_recursive.(1:9)" ] }, { "cell_type": "markdown", "id": "d563be2b-fdc4-4d6a-a65a-db80ec1916a4", "metadata": {}, "source": [ "### Performance" ] }, { "cell_type": "code", "execution_count": null, "id": "851b5c85-0210-4946-9ae9-080a06bbb0cb", "metadata": {}, "outputs": [], "source": [ "\n", "@time myfib(50)" ] }, { "cell_type": "code", "execution_count": null, "id": "432614d7-c919-4844-97da-8e0781c06a50", "metadata": {}, "outputs": [], "source": [ "\n", "@time myfib_recursive(50)" ] }, { "cell_type": "markdown", "id": "6954104b-a8b3-415b-be0a-9b55561f25d3", "metadata": {}, "source": [ "### Testing" ] }, { "cell_type": "markdown", "id": "d8e2fc9b-05d1-46c8-a08d-be269f486b50", "metadata": {}, "source": [ "Let's use the identity $\\sum_{i=1}^n F_i^2 = F_n F_{n+1}$ to test `myfib`." ] }, { "cell_type": "code", "execution_count": null, "id": "82689315-6f47-40d3-884b-c082383955d8", "metadata": {}, "outputs": [], "source": [ "\n", "for n = (10, 15, 20)\n", " s = 0\n", " for i = 1:n\n", " s += (myfib(i))^2\n", " end\n", " @show s == myfib(n) * myfib(n+1)\n", "end" ] }, { "cell_type": "code", "execution_count": null, "id": "c7520625-3982-4887-bdd0-a678499f2744", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.11.6", "language": "julia", "name": "julia-1.11" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }