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.
In this second part, we will focus on putting in to practice what we learned in Part 1 of this series. Also we will learn more about prime numbers, and why it's not only a fascinating problem, but a great way to learn about computers, programming and maths.
As a reminder from Part 1, 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.
Python language is very powerful tool for creating any kind of computer program, but today we're going to focus solely on how to perform some very basic techniques and ideas found in all computer programming languages.
Divisibility means that a given number is divisible by another number in a way that there is no remnant. For example:
Because the definition of prime number is related with divisibility, we will now go through some examples where we just divide numbers with other numbers, and see what we can learn in the process...
Let's get started with something very simple. Let's take a number, for example 8 and see if we can divide it with other numbers than 1 and itself (8). I think you know the answer already, so I could just ask you if 8 is a prime and you could answer easily. But computers are not like that, we can't ask anything from a computer directly. In this case, we could do something like this:
8 / 2
We start by dividing the number with the smallest possible number first, and in this case immediately find that the number eight is not a prime. Let's try another example, number 9. Again we start by dividing it with zero.
9 / 2
Because we don't get a whole number as a result, we can say that 9 is not divisible by two and based on the information we have so far, might be a prime.
9 / 3
Because we get a whole number as a product, in this case 3, we know for sure that 9 is not a prime number. If it was a prime number, we could not divide it with 3, or any other number than itself and number 1. That's all there is to remember about prime numbers for now.
At this point you might wonder why are we using such obvious examples? This is actually one of the key concepts to understand regarding computers; they can't understand anything and nothing at all is obvious to them.
Let's try one more, this time number 7.
7 / 2.0
7 / 3.0
7 / 4.0
7 / 5.0
7 / 6.0
Finally! We found our first prime number! :)
Actually, Python provides us a very nice tool to do this same thing in a more useful way, it's a mathmetical operator called 'modulus'. Let's see first a few examples before explaining it more.
8 % 2
9 % 2
7 % 2
**Modulus tells us if a given number is divisible by another number!**
If the result is 0, we know the answer is yes, the number on the left is divisible by the number on the right.
If the result is anything but 0, we know the number on the left is not divisible by the number on the right.
Modulus is very useful and is widely used even by advanced programmers, and often in context that have nothing to do with mathematics. Soon you will learn more about this.
Sometimes you hear the word 'binary' related with computers. Binary just means that there are two options, 0 and 1. For example we may have a case where the statement "it's raining", is
This has to do with the way computers work; the processing unit (CPU) of the computer is made of billions of tiny gates. These tiny gates can only peform one action, they can open or close. Related with this, there is a system of mathematics called boolean logic. Boolean means the same thing as binary, that there are only two possible states. Instead of talking about gates or 0 and 1, in Boolean logic we talk about
False. Either something is true, or it's false. When you hear about Boolean Logic for the first time, it sounds confusing, but actually it is really as simple as that.
Understanding this clearly, you take a giant leap towards understanding computers. Boolean logic, and the statements we use to instruct the computer based on that type of simplistic logic, is the language of computers. Python gives us humans an easy to learn and understand way to use boolean logic to tell computers what we want them to do. Let's see a couple of examples.
1 is 1
2 is 2
2 is 3
True is True
True is False
False is False
The basic principle is that simple. Boolean logic also leverage 'and' and 'or' statements.
1 is 1 and 2 is 2
1 is 1 and 2 is 3
1 is 1 or 2 is 3
True is True and True is False
or statement we are saying that one of the options is true, and with
and statement we are saying that both are true. We can also do the reverse and say that something
is not. In other words, we have two options, statements that are saying something is true, and if it is true in fact, we get the respone
True, and statements that are saying something is not true, and if it is not true, we get the response
True (and if it's true, we get
1 is not 1
2 is not 3
2 is not 3 or 5 is not 5
2 is not 3 and 5 is not 5
We can also combine statements that have
is not and
is in them...
1 is 1 or 2 is not 3
Actually, we can join together as many such statements as we like using
5 % 2 is 0 and 5 % 3 is 0 and 5 % 4 is 0
Do you see what we just did? We confirmed that 5 is a prime number. Take a moment to appreciate how we did it.
To clarify, we confirm that number 5 is not divisible by any other number than 1 and itself. Actually all numbers are divisible by 1 and itself, so this is to say it's not divisible. Also numbers can't be divisible by numbers that are larger than the number itself, so it's enough to check all the numbers between 1 and the number itself to get the answer if a given number is prime or not. This sounds fine as long as we deal with small numbers, but how about when we want to answer the same question for a very large number?
Because it's so easy to say if small numbers are prime or not, mathematicians are generally only interested in finding really big primes. In the next section we'll learn just how big, and then come back to learn how big numbers can be verified using the techniques we've learn already.
The biggest prime number is a really big number. It's a number with 22,338,618 digits. To illustrate this more clearly, to appreciate how big that number in fact is, somebody printed it out for us and it turned out bigger than a very big book. Just one number number bigger than a book!
One of the most powerful computers in the world was able to verify that this indeed is a prime number, after working years just to do that. Actually it's not just one computer, but a network of almost 200,000 computers that are dedicating their available resources to looking for the next big prime number. It's called GIMPS. That's pretty cool stuff right?
Let's be realistic, we're not going to find such a big number with our small lonely computer. What we can do, is create a simple program that more or less does what the gigantic network of computers in GIMPS do. For now, let's wrap up here and continue in the next part, where we will learn about putting into use the building blocks we already have, and create our very first algoritm. A prime number solving algorithm to be more precise!
While it may seem that we are dealing with very simple things, actually we've already covered some of the most important concepts of computer programming. You're on your way to knowing how to tell computers what you want them to do!