1
00:00:00,330 --> 00:00:07,950
All right, so in the next coding exercise, we're going to be building a prime number checker. And a

2
00:00:07,950 --> 00:00:15,030
prime number, just to remind you, is a number that is only divisible by one and itself.

3
00:00:15,720 --> 00:00:22,350
So, for example, two is a prime number because it can only be divided by one and two.

4
00:00:23,010 --> 00:00:24,600
Three is also a prime number,

5
00:00:24,630 --> 00:00:29,100
similarly, it can only be split into three equal sized chunks.

6
00:00:29,550 --> 00:00:35,700
But four, however, is not a prime number because while it can be divided by one and four, it can

7
00:00:35,700 --> 00:00:37,350
also be divided by two.

8
00:00:37,620 --> 00:00:44,100
So two times two is four. Essentially a prime number is a number that can't be broken to smaller

9
00:00:44,100 --> 00:00:46,230
parts other than one and itself.

10
00:00:46,950 --> 00:00:54,240
And it's this property of prime numbers that makes them really useful to everything from cicadas to

11
00:00:54,240 --> 00:00:58,440
Bitcoin and also encryption in the computer world.

12
00:00:59,280 --> 00:01:05,820
So if you head over to day 8.2 prime number checker, you'll see the starting code including

13
00:01:05,820 --> 00:01:08,840
the number that we're going to be sending over and checking.

14
00:01:09,270 --> 00:01:16,200
And then, of course, the line eleven where we actually call this function and pass over the number. So

15
00:01:16,320 --> 00:01:19,960
this part is really important when you are creating your function.

16
00:01:20,610 --> 00:01:26,370
The idea is that you're going to be creating a function here that checks whether if the number that's

17
00:01:26,370 --> 00:01:29,930
passed in is a prime number or not.

18
00:01:30,450 --> 00:01:36,180
And if you take a look at this chart, then you'll see all the prime numbers up to 100 highlighted in

19
00:01:36,180 --> 00:01:36,640
yellow.

20
00:01:37,440 --> 00:01:39,410
There's a couple of things you need to remember.

21
00:01:39,600 --> 00:01:44,120
One is that you'll probably need to use the modulus, which we've seen before.

22
00:01:44,670 --> 00:01:49,090
And if you can't remember how to use that, then take a look at this link.

23
00:01:49,710 --> 00:01:54,900
Now, the second thing is that make sure that you name your function and parameters the same as what

24
00:01:54,900 --> 00:01:57,390
you see here where it's called.

25
00:01:58,020 --> 00:02:05,040
And finally, try to use the same wording as the example outputs, namely, if it is a prime number print

26
00:02:05,490 --> 00:02:08,430
"It's a prime number." in this exact wording.

27
00:02:08,760 --> 00:02:14,310
And if it's not a prime number, then print "It's not a prime number." with this exact wording.

28
00:02:15,360 --> 00:02:21,150
And finally, if you're still not quite sure how prime numbers work, then be sure to check out the

29
00:02:21,150 --> 00:02:22,200
Wikipedia page.

30
00:02:22,470 --> 00:02:29,340
And if your first language is not English, then be sure to switch to your native language just to know

31
00:02:29,610 --> 00:02:33,480
what it is because I know it's called different things in different languages.

32
00:02:34,920 --> 00:02:35,910
Pause the video,

33
00:02:36,180 --> 00:02:40,890
have a good think about this and see if you can solve this problem.

34
00:02:44,690 --> 00:02:48,920
All right, so let's think about how we might tackle this. The first thing I'm going to do is I'm going

35
00:02:48,920 --> 00:02:54,950
to create a new function called prime_checker to be in line with this line, because I'm calling a

36
00:02:54,950 --> 00:02:56,270
function called prime_checker

37
00:02:56,660 --> 00:03:00,800
and then I'm passing in an argument to the parameter called number.

38
00:03:01,430 --> 00:03:06,740
So now that we've created our function, the next step is to figure out, well, what do we do in this

39
00:03:06,740 --> 00:03:07,190
function?

40
00:03:07,730 --> 00:03:14,840
Now, I mentioned that every prime number can only be cleanly divided by itself and one.

41
00:03:15,290 --> 00:03:21,540
So that means that if it's divided by any other number other than these, it will have a remainder.

42
00:03:22,100 --> 00:03:28,360
So we know that we can use the modulus to check what the remainder is of a division.

43
00:03:28,910 --> 00:03:37,910
So we know that, for example, if we divide six by two, then it equals three with no remainder because

44
00:03:37,910 --> 00:03:44,750
six is divided into two three times, while seven divided by two equals one because there's a remainder

45
00:03:44,750 --> 00:03:50,320
of one, because two times three is six and then six plus one makes seven.

46
00:03:51,080 --> 00:04:00,020
So we can use the modulus to check whether if the number that we're passing in here can be divided by

47
00:04:00,050 --> 00:04:02,610
all the numbers all the way down to two.

48
00:04:03,410 --> 00:04:11,570
So, for example, if number was equal to seven, well, then what we would want to do is we would want

49
00:04:11,570 --> 00:04:20,450
to do seven divided by two and then see if there is any remainder, and then seven divided by three and

50
00:04:20,450 --> 00:04:25,480
see if there was any remainder, and then seven divided by four...

51
00:04:25,520 --> 00:04:30,740
and then we just go up two, three, four, five, and six.

52
00:04:31,040 --> 00:04:37,740
And now we've basically divided seven by all the possible values other than one and seven.

53
00:04:38,510 --> 00:04:45,470
So if any of these has zero as the remainder, then it means that it does cleanly divide by one of these

54
00:04:45,470 --> 00:04:48,440
numbers, which means that it's actually not a prime number.

55
00:04:49,550 --> 00:04:57,230
But if all of these have a remainder, then that means seven is a prime number, which we already know

56
00:04:57,230 --> 00:04:59,260
it is because we can see it on this graph.

57
00:04:59,720 --> 00:05:07,220
So how can we go through this process of getting this number divided by numbers starting from two

58
00:05:07,220 --> 00:05:11,030
all the way going up to the number minus one?

59
00:05:11,270 --> 00:05:14,380
Well, we could simply just use a for loop, right?

60
00:05:14,780 --> 00:05:22,700
So we create a for loop and say for i which is going to be this number that we're going to divide

61
00:05:22,700 --> 00:05:24,440
by, in,

62
00:05:24,890 --> 00:05:28,760
and that number is going to be a range of numbers.

63
00:05:28,910 --> 00:05:33,470
And the range starts from two because we're not going to divide it by one.

64
00:05:33,470 --> 00:05:39,680
We already know that all numbers can be divided by one. And we're going to put the range all the way

65
00:05:39,680 --> 00:05:41,520
up to the number.

66
00:05:42,350 --> 00:05:48,380
So now inside this for loop, we can start doing our divisions. So we could take the number and we can

67
00:05:48,380 --> 00:05:51,740
use the modulo operator to divide it by i.

68
00:05:52,220 --> 00:06:00,580
So it's exactly the same as each of these lines of division. If number divided by i is equal to zero,

69
00:06:00,590 --> 00:06:06,350
so if it divides cleanly, well, then in this case, it's clearly not a prime,

70
00:06:06,350 --> 00:06:14,720
right? Because if that number that we get passed in can be divided by any of the numbers between two

71
00:06:14,720 --> 00:06:21,600
and number minus one cleanly with no remainder, then that means it's not a prime number.

72
00:06:22,220 --> 00:06:30,950
However, if we get to the very end of our for loop and this if statement was never triggered, well,

73
00:06:30,950 --> 00:06:33,650
then that means this is a prime number.

74
00:06:34,910 --> 00:06:37,820
So how can we represent this with our code?

75
00:06:38,420 --> 00:06:43,900
Well, we could create a variable called is_prime and set it to true.

76
00:06:44,540 --> 00:06:48,510
If this if statement gets triggered, then we switch

77
00:06:48,530 --> 00:06:51,000
this is_prime to false.

78
00:06:51,560 --> 00:06:55,980
And finally, once we get to the end of the for loop, we can go ahead and check

79
00:06:56,000 --> 00:07:00,400
well, is is_prime true or is it false?

80
00:07:00,920 --> 00:07:07,370
And this expression is basically the same as just typing this because this is going to be true or false.

81
00:07:07,550 --> 00:07:15,020
And if it's true, then we get to step into this block and we're going to print from this example output

82
00:07:15,020 --> 00:07:23,090
and say that it is a prime number. But else or otherwise, then we're going to print the opposite

83
00:07:23,280 --> 00:07:26,360
where  we say it's not a prime number.

84
00:07:27,870 --> 00:07:28,630
There you have it.

85
00:07:28,650 --> 00:07:38,040
This is the simplest way of checking whether if a number is a prime number. And all it does is it checks

86
00:07:38,040 --> 00:07:41,790
through all of the numbers between two and the number.

87
00:07:42,120 --> 00:07:47,220
And if it's divisible by any of those numbers, well, then it's not a prime.

88
00:07:48,000 --> 00:07:54,420
Now, out there in the real world, there's a lot of other ways that you could implement this function.

89
00:07:54,900 --> 00:08:00,570
And there's more efficient ways, there's faster ways and there's lots of algorithms that mathematicians

90
00:08:00,570 --> 00:08:04,350
have come up with in the past. Now for our case,

91
00:08:04,530 --> 00:08:12,030
what I'm really interested in is that you understand how inputs work and how you can use the argument

92
00:08:12,030 --> 00:08:18,420
that's passed in to the parameter inside your function in order to do something with it.

93
00:08:18,960 --> 00:08:24,780
As long as that's clear to you, then that means we can continue in the course and I can rely on the

94
00:08:24,780 --> 00:08:31,050
fact that this code or this code will not throw you off and you'll know exactly what's going on when

95
00:08:31,050 --> 00:08:38,100
it's being used inside the block of code and when it's being passed over into a block of code.

96
00:08:38,700 --> 00:08:43,980
In the next lesson, we're going to be putting all of our knowledge that we learned into practice and

97
00:08:43,980 --> 00:08:48,100
we're going to get started building our final project, the Caesar cipher.

98
00:08:48,390 --> 00:08:50,550
So for all of that and more,

99
00:08:50,580 --> 00:08:51,270
I'll see you there.

