In this exercise,
you are going to be writing a function that checks
whether if an input is a Prime Number.
Prime numbers are numbers that can be cleanly divided
by themselves and 1 and no other numbers.
There are sort of mathematical peculiarity
and there's a real beauty
to these numbers if you read up on it.
Now, in the Description,
I've linked to the Wikipedia page on prime numbers.
So if English is not your first language
and you're not sure what it is I'm talking about
take a look at Wikipedia
and change the page to your own local language.
The way that your function is going to work is
it's going to be called prime_checker
and it's going to use a logic to figure
out whether if the input number that's put
into the Input pane is a prime number or not.
Now we're going limit the inputs to
up to 100 because this program can be quite intensive
and it can take quite long
if you go up to a large number of numbers.
So have a think about how you might achieve this
by writing code and how you might achieve this
by simply writing out the logic.
If you get stuck,
always try and figure out if you can think about it just
from a logical point of view and then implement it in code.
Either way, take a look at the description,
take a look to see if you can figure it out
and we'll go through the solution together
on the next slide
once you are done.
The first thing we're going to do is to define our function.
So on line one, I've got the definition for our function
which is called prime_checker(),
and it takes just one input, the number
that we want to check. On the next line,
I've defined a variable which is called is is_prime
and I'm setting it to "True"
by default and I'm going to change it to "False"
if at some point my code detects that it's actually
not a prime number.
So how am I going check whether
if it's a prime number or not?
Well, I'm going to create a range and I'm going to start
from the number 2 because we know
that prime numbers are numbers that can only be divided
by themselves and 1 cleanly,
and that also means I don't need to increase the number
by 1 at the end of the range either.
We're going to loop through each of those numbers in this
for loop, and we're going
to check our potential prime number
which is stored in the variable number
as the input to our function,
and we're going to check if we divide it by the current number
in our range, does it divide cleanly?
Because remember prime numbers can only be divided
by 1 and itself cleanly.
If any of the numbers in our range between 2
and our number can substitute for i, and we end up
with remainder 0,
then it's clearly not a prime number.
So we set is_prime to "False".
Now the final part is the easiest.
We just have an if and else statement.
If at the end of our for loop,
once we've checked through all of the numbers between 2
up to the number that we're questioning and none
of those happen to allow us to divide our number by it
and have no remainder,
then it's clearly a prime number and our is_prime variable
is untouched, stays as "True".
If anything else happens
then it's obviously not a prime number
and it is divisible by some number between 2
and our number.
The hardest part of this is to think
and reason through the range that we create
and using that range in our loop to check our number.
See if you can understand how the logic works
and if you had any issues passing the test
in the previous lesson
then go back there and try and fix your code.