{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NUMERICAL COMPUTING IS FUN" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A guide to principles of computer science and numerical computing for all ages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As much as this series is to educate aspiring computer programmers and data scientists of all ages and all backgrounds, it is also a reminder to myself. After playing with computers and numbers for nearly 4 decades, I've also made this to keep in mind how to have fun with computers and maths." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# OVERVIEW OF PART 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this fourth part, we will continue with putting in to practice what we learned in [Part 1](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-1.ipynb), [Part 2](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-2.ipynb), and [Part 3](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-3.ipynb) of the series. \n", "\n", "We will go deeper into the meaning of algoritms, specifically in the context of the idea of automation. Most computer programs are ways to automate things so that humans can do human stuff instead. Or at least that's the idea.\n", "\n", "As a reminder from [Part 1](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-1.ipynb), prime numbers are the numbers that all other numbers are made of. This means that any number that is only divisible by 1 or itself, is a prime number. Consequently any number that is divisible by a number other than 1 or itself, is not a prime number." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember the way we defined algorithm as something like a factory? Automation has to do with all those algorithms combined, as well all of their individual components. All those tiny pieces of code together make up what we refer to as automation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# PART 4 : Process Automation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We've already created simple algorithms and were left in the third version of our algo in [Part 3](https://nbviewer.jupyter.org/github/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-3.ipynb). Before continuing to build our algo further, let's learn about generating numbers. It is a perfect example of how computers help us automated tedious processess. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.1. Generating Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before moving on to the main section of this part, which will cover loops, another flow control method, let's become familiar with the idea and have some number generation fun.\n", "\n", "Number generation will become handy soon, so we don't have to key in the many numbers we want to check in terms of if they are prime or not. To do this, we will use a `for` statement. But let's first learn about `range`, a nifty little function that comes with python." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At the simplest, range takes a number and creates a sequence of numbers from 0 to the input number. In this case, even though we can't see the numbers yet, we've created a sequence 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.\n", "\n", "You might wonder why we don't get a 10 but stop at 9 even though we input 10. In Python and most other programming languages counting always starts from 0. Now, let's access the numbers we're generating." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for i in range(10):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see what we did here. First with `for` we basically say that we want to do something for a number of items. Then with `i` we say that each time an item is picked, `i` will represent it. In other words, we can use `i` to access it inside the loop. With `range(10)` we create a sequence of numbers from 0 through 9. As you can see, the `print(i)` has leading spaces to it, which means that it's handled inside the loop. Note that `i` can be called anything you like." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for number in range(10):\n", " print(number)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`range` can be used to create any sequence of integers by defining the starting and ending positions of the sequence." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n", "12\n", "13\n", "14\n", "15\n", "16\n", "17\n" ] } ], "source": [ "for i in range(11, 18):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also add a 'step' argument, which gives us even more control over the range of numbers we want to create. For example with step argument 2, we will get every other number in a range:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n", "10\n", "12\n", "14\n", "16\n", "18\n" ] } ], "source": [ "for i in range(2, 20, 2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This way we only get the even numbers between 2 and 2. Let's try the same for odd numbers." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "5\n", "7\n", "9\n", "11\n", "13\n", "15\n", "17\n", "19\n" ] } ], "source": [ "for i in range(1, 20, 2):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You've now learned a very useful and often applicable process automation; number generation. We've learn how to write any sequence of numbers, including just even or odd numbers.\n", "\n", "There are many other ways you can use to create numbers, including random numbers, but this will be more than enough for what we want to do. Let's move on to the next section and learn about loops." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you see, loops are just as easy and intuitive to use in Python language as everything else we've learn so far. A `for` loop gives us a useful way to say that some process will go on *for* as long as something is true. Consider this example: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4.2. Storing something in a variable" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you've learn yet another fundamental building block of numerical computing, we can go back to our prime number algorithm. Let's start putting what we've learn into use in our algo and also introduce one more new concept (I promise it's the last one for a while), storing something on to a variable. Storing something in to the computer memory is nothing like storing something in to human memory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even though we talk about storing something, it'a also nothing like storing something in to a safety deposit box where things will be kept for a long time. Computer memory is a temporary storage for something we're going to use as part of our computer program. Let's see some examples." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "storage = 1" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(storage)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basically anything in Python can be stored in a variable. It's very simple. Also, we don't have to use the `print` to access the contents of a variable." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "storage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next two examples you will get a taste for how anything can be stored into a variable to access it later. " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "answer = 1 is 2\n", "answer" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print_hello = print('hello')\n", "print_hello" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This ability to store things into variables is very powerful, particularly when we want to use the same value many times. For example, we could use this approach to simplify the most recent version of our algo. Take a note below how we are using the same operation twice:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "def third_algo(left, right):\n", " \n", " # it will not rain\n", " if left % right is 0: # < -- here first time\n", " return True\n", " \n", " # it will rain a little\n", " elif left % right is 1: # < -- here second time\n", " return \n", " \n", " # it will rain heavily\n", " else: \n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's make a slight simplifcation by storing the operation we do twice in to memory first." ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def fourth_algo(left, right):\n", " \n", " stored_value = left % right\n", " \n", " # it will not rain\n", " if stored_value is 0:\n", " return True\n", " \n", " # it will rain a little\n", " elif stored_value is 1:\n", " return \n", " \n", " # it will rain heavily\n", " else: \n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before wrapping up for now, let's put all that we've learn together in one example." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.5. Putting it All Together" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this following section the length of our algoritm (function) is growing. But if you look carefully, you see that the changes we make are very small in fact. Moreover, we are only making changes that you've already learn so far. " ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "True\n", "True\n", "True\n", "True\n", "True\n", "True\n" ] } ], "source": [ "# first we create a range of numbers\n", "numbers = range(1, 10)\n", "\n", "# then we create a loop\n", "for number in numbers: \n", " \n", " # then we perform the modulus operation \n", " result = number % number\n", " \n", " # then we create a conditional statement for cases when it's true\n", " if result is 0: \n", " print(True)\n", " \n", " # and finish with else for cases when it's false \n", " else: \n", " print(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Obviously we are getting True as result everytime because we are always having both the right number and the left number the same (e.g. 1 % 1, 2 % 2...). \n", "\n", "Let's make a slight modification to take us step closer to something that will help us a great deal in finding prime numbers later. This time I'm removing the comments to keep the code neat." ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "False\n", "True\n", "True\n", "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "left = 20\n", "right_numbers = range(1, 20)\n", "\n", "for right in numbers: \n", " result = left % right\n", " \n", " if result is 0: \n", " print(True)\n", " \n", " else: \n", " print(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So what we are doing now, is fixing the left number to be 20, and then checking it against every number in the range of 1 to 20 and see if it's divisible. This makes checking if a number is prime a whole lot simpler! Let's try an example where we know it's a prime nubmer, for example 13 (it's not divisisble by any other number than 1 and itself)." ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "left = 13 # <-- changed\n", "right_numbers = range(1, 12) # <-- changed\n", "\n", "for right in numbers: \n", " result = left % right\n", " \n", " if result is 0: \n", " print(True)\n", " \n", " else: \n", " print(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because we are starting our range from 1, one get one True in the beginning, so we have to start the range from 2 instead to get the right answer. As you can see, I changed the second line so that we scan until 12 which is the last number before 13. Let's put this inside a function as our fifth algo version and make the range start from 2 instead of 1." ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "def fifth_algo(left, right): \n", "\n", " right_numbers = range(2, right) # <-- changed\n", "\n", " for right in right_numbers: # <-- changed\n", " result = left % right\n", "\n", " if result is 0: \n", " print(True)\n", "\n", " else: \n", " print(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now things are starting to look good. We could now remove 'left' variable entirely as it comes as an argument from the function, and also instead of having to modify the function for the last number of the range, we also input that as an argument." ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "fifth_algo(7, 6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's it, we're prime number checking now! :) Because the result is False for all, we know for sure that our input, in this case 7, is a prime. There is one more very small change we can do using the skill we've already learn to make a nice improvement to what we already have. Instead of requiring the user to input the end of the range, we can automatically compute it as it's always the last number before left. In other words, it's left - 1." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "def sixth_algo(left): # <-- changed\n", "\n", " right_numbers = range(2, left - 1) # <-- changed\n", "\n", " for right in right_numbers:\n", " result = left % right\n", "\n", " if result is 0: \n", " print(True)\n", "\n", " else: \n", " print(False)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "sixth_algo(9)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "sixth_algo(11)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n", "False\n" ] } ], "source": [ "sixth_algo(19)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Things are working real nicely now. But clearly we will later have a problem with larger numbers with this current approach, as if we input 1,000, we will have 1,000 True or False values printed on the screen. To overcome this, we can make a small change to our latest version." ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "def seventh_algo(left):\n", "\n", " right_numbers = range(2, left - 1)\n", " output = 0 # <-- changed\n", " \n", " for right in right_numbers:\n", " result = left % right\n", "\n", " if result is 0: \n", " output += 1 # <-- changed\n", "\n", " else: \n", " output += 0 # <-- changed\n", " \n", " return output # <-- changed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What we are doing, is first we declare a variable 'output' with starting value 0. Then instead of printing out True, we silently add 1 to output, and in case of False we add 0. Only in the end we print the value out, with the return statement that is outside of the for loop (note how it's indentation is equal to the for statement, meaning it will be processed only once the for loop has completed its job)." ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seventh_algo(19)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nice. Now we can key in much larger numbers, and just get one output." ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seventh_algo(127)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before wrapping up, let's simplify our code slightly and instead of outputting a number, output a True or False statement. True for 'it's a prime' and False for 'it's not a prime'." ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def eight_algo(left):\n", "\n", " right_numbers = range(2, left - 1)\n", " output = 0\n", " \n", " for right in right_numbers:\n", " result = left % right\n", "\n", " if result is 0: \n", " output += 1\n", " \n", " return output is 0 # <-- changed" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eight_algo(19)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eight_algo(127)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eight_algo(12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how we removed the else statements entirely. Because we are doing nothing in the cases where the left number is not divisible by the right number. In other words, whenever the product of the modulus operation is not zero, we do nothing. Therefore it's enough to just have the if statement without the else. This is quite common. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Part 4 Summary\n", "\n", "- Generally speaking computer programs are focused on process automation\n", "- Loops are a highly effective method for automation\n", "- With small changes to our code, we can make big improvements in capability\n", "- Sometimes we can get more done with less code!\n", "- It's very convinient to store values in to memory\n", "- Computer memory is nothing like human memory, and also not like a safe deposit box\n", "- Any value can be stored in to memory \n", "- Numbers can be automatically generated with `range` function\n", "- It's meaningful to learn new concepts by gradually improving things\n", "\n", "We've made great progress! Time to wrap up for now, and then in the next part we get in to the real action, looking for prime numbers! With the skills you're learn so far, you're doing a lot of the things the day-to-day of advanced programmers and data scientists is made of." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 1 }