{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "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.8.3" }, "colab": { "provenance": [] } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "whjkuflGLNWu" }, "source": [ "# Fibonacci Solver\n", "\n", "The Fibonacci Sequence is the series of numbers:\n", "\n", "` 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... `\n", "\n", "The next number is found by adding up the two numbers before it:\n", "\n", "the `2` is found by adding the two numbers before it `(1+1)`,\n", "the `3` is found by adding the two numbers before it `(1+2)`,\n", "the `5` is `(2+3)`,\n", "and so on!\n", "\n", "## Coding it up in Python\n", "Just initialize to variables with the first two numbers of the Fibonacci sequence. Then add them up and print the result.\n" ] }, { "cell_type": "code", "metadata": { "id": "p58ac2HULNWv" }, "source": [], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "xSyPoS1bLNWy" }, "source": [ "You now need to do this multiple times, so you need a ***loop***\n", "To use a loop you need to know how many times you want to loop for. \n", "For now, create a loop that prints **20** numbers (from 0 to 19). To do this in python you can create a ***for loop*** using the `range` function which will run the function for every number in the range.\n", "(TIP: Python is a ***0 based*** language which means it starts counting from 0 not 1 as a human normally would.)" ] }, { "cell_type": "code", "metadata": { "id": "tTsWxVxMLNWy" }, "source": [], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "INNU9p8zLNW0" }, "source": [ "Now use the loop to compute the first 20 elements of the Fibonacci sequence." ] }, { "cell_type": "code", "metadata": { "id": "4Hf59jqgLNW1" }, "source": [], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "bScJ-6tULNW3" }, "source": [ "If you managed it till here great job!\n", "Is seems to work great so far, but you need to store these numbers so you can reference them when needed." ] }, { "cell_type": "markdown", "metadata": { "id": "1VhirrXpLNW3" }, "source": [ "Create an empty ***array*** called `fibonacci` and then append the elements of the sequence to it." ] }, { "cell_type": "code", "metadata": { "id": "thTMoVY2LNW3" }, "source": [], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "6nlc_cC7LNW5" }, "source": [ "If everything worked according to the plan, you should be able to access any of the first 20 elements of the Fibonacci sequence at will.\n", "\n", "eg `fibonacci[0]` would get us the first item in the array whilst `fibonacci[9]` would get us the tenth item (remember Python is 0 based so you always need to take 1 away from the number you need)" ] }, { "cell_type": "code", "metadata": { "id": "yEKa7pAuLNW6" }, "source": [ "fibonacci[0]" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "1wbxOVnELNW7" }, "source": [ "fibonacci[9]" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "50FpCM7CLNW9" }, "source": [ "So now you know how to get at the index, you need to know which index values are asked for by the user.\n", "Lets get some input from the user." ] }, { "cell_type": "markdown", "source": [ "Get the first index from the user" ], "metadata": { "id": "5Vp9iO2r2e2S" } }, { "cell_type": "code", "source": [], "metadata": { "id": "Zj6mnXCI2mNo" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Get the second index from the user" ], "metadata": { "id": "9LDXbzP02rFa" } }, { "cell_type": "code", "metadata": { "id": "wqbTuR2TLNXA" }, "source": [], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Print the received inputs and their types" ], "metadata": { "id": "EK9FMjJV22x9" } }, { "cell_type": "code", "metadata": { "id": "xfy81HrsLNXC" }, "source": [], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "ttb_0MejLNXE" }, "source": [ "The value received from input will be treated as a string type, you need to convert this to a number so python knows to treat it as one." ] }, { "cell_type": "code", "source": [], "metadata": { "id": "RwVoHTLn6QvB" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Print the type of the transformed variables." ], "metadata": { "id": "pxKx8gKy2-eh" } }, { "cell_type": "code", "source": [], "metadata": { "id": "dN_EzHKr3GmO" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "CRuAEA7KLNXE" }, "source": [ "Now you have all the parts you need to make this work. \n", "\n", "* The user should be prompted \"How many results would you like?\"\n", "* The program should read the users preference, e.g. 20, and then compute the list of elements up to this number.\n", "* Then the program should ask the user for 2 indexes within the list of the calculated Fibonacci sequence\n", "* Finally it should print the summation of their values.\n", "\n", "Time to put it together:" ] }, { "cell_type": "code", "metadata": { "id": "ukW0WUA7LNXE" }, "source": [], "execution_count": null, "outputs": [] } ] }