1
00:00:01,110 --> 00:00:02,130
In this exercise,

2
00:00:02,130 --> 00:00:05,010
you are going to be writing a function that checks

3
00:00:05,010 --> 00:00:10,010
whether if an input is a Prime Number.

4
00:00:10,410 --> 00:00:13,740
Prime numbers are numbers that can be cleanly divided

5
00:00:13,740 --> 00:00:17,730
by themselves and 1 and no other numbers.

6
00:00:17,730 --> 00:00:20,370
There are sort of mathematical peculiarity

7
00:00:20,370 --> 00:00:21,420
and there's a real beauty

8
00:00:21,420 --> 00:00:23,850
to these numbers if you read up on it.

9
00:00:23,850 --> 00:00:24,690
Now, in the Description,

10
00:00:24,690 --> 00:00:27,510
I've linked to the Wikipedia page on prime numbers.

11
00:00:27,510 --> 00:00:29,460
So if English is not your first language

12
00:00:29,460 --> 00:00:32,070
and you're not sure what it is I'm talking about

13
00:00:32,070 --> 00:00:33,810
take a look at Wikipedia

14
00:00:33,810 --> 00:00:37,923
and change the page to your own local language.

15
00:00:38,790 --> 00:00:40,616
The way that your function is going to work is

16
00:00:40,616 --> 00:00:42,370
it's going to be called prime_checker

17
00:00:44,190 --> 00:00:48,300
and it's going to use a logic to figure

18
00:00:48,300 --> 00:00:50,820
out whether if the input number that's put

19
00:00:50,820 --> 00:00:54,963
into the Input pane is a prime number or not.

20
00:00:55,830 --> 00:00:58,080
Now we're going limit the inputs to

21
00:00:58,080 --> 00:01:02,280
up to 100 because this program can be quite intensive

22
00:01:02,280 --> 00:01:04,050
and it can take quite long

23
00:01:04,050 --> 00:01:07,530
if you go up to a large number of numbers.

24
00:01:07,530 --> 00:01:10,650
So have a think about how you might achieve this

25
00:01:10,650 --> 00:01:14,910
by writing code and how you might achieve this

26
00:01:14,910 --> 00:01:17,250
by simply writing out the logic.

27
00:01:17,250 --> 00:01:18,210
If you get stuck,

28
00:01:18,210 --> 00:01:21,780
always try and figure out if you can think about it just

29
00:01:21,780 --> 00:01:26,280
from a logical point of view and then implement it in code.

30
00:01:26,280 --> 00:01:28,740
Either way, take a look at the description,

31
00:01:28,740 --> 00:01:30,630
take a look to see if you can figure it out

32
00:01:30,630 --> 00:01:32,130
and we'll go through the solution together

33
00:01:32,130 --> 00:01:33,120
on the next slide

34
00:01:33,120 --> 00:01:34,503
once you are done.

35
00:01:40,080 --> 00:01:43,560
The first thing we're going to do is to define our function.

36
00:01:43,560 --> 00:01:47,050
So on line one, I've got the definition for our function

37
00:01:47,995 --> 00:01:48,900
which is called prime_checker(),

38
00:01:48,900 --> 00:01:51,332
and it takes just one input, the number

39
00:01:51,332 --> 00:01:54,540
that we want to check. On the next line,

40
00:01:54,540 --> 00:01:58,410
I've defined a variable which is called is is_prime

41
00:01:58,410 --> 00:01:59,730
and I'm setting it to "True"

42
00:01:59,730 --> 00:02:04,050
by default and I'm going to change it to "False"

43
00:02:04,050 --> 00:02:07,080
if at some point my code detects that it's actually

44
00:02:07,080 --> 00:02:08,913
not a prime number.

45
00:02:10,199 --> 00:02:11,670
So how am I going check whether

46
00:02:11,670 --> 00:02:13,170
if it's a prime number or not?

47
00:02:13,170 --> 00:02:16,530
Well, I'm going to create a range and I'm going to  start

48
00:02:16,530 --> 00:02:20,040
from the number 2 because we know

49
00:02:20,040 --> 00:02:24,570
that prime numbers are numbers that can only be divided

50
00:02:24,570 --> 00:02:27,780
by themselves and 1 cleanly,

51
00:02:27,780 --> 00:02:31,290
and that also means I don't need to increase the number

52
00:02:31,290 --> 00:02:34,263
by 1 at the end of the range either.

53
00:02:35,100 --> 00:02:38,880
We're going to loop through each of those numbers in this

54
00:02:38,880 --> 00:02:40,470
for loop, and we're going

55
00:02:40,470 --> 00:02:44,100
to check our potential prime number

56
00:02:44,100 --> 00:02:46,050
which is stored in the variable number

57
00:02:46,050 --> 00:02:47,970
as the input to our function,

58
00:02:47,970 --> 00:02:52,080
and we're going to check if we divide it by the current number

59
00:02:52,080 --> 00:02:55,320
in our range, does it divide cleanly?

60
00:02:55,320 --> 00:02:57,840
Because remember prime numbers can only be divided

61
00:02:57,840 --> 00:03:00,720
by 1 and itself cleanly.

62
00:03:00,720 --> 00:03:04,020
If any of the numbers in our range between 2

63
00:03:04,020 --> 00:03:08,520
and our number can substitute for i, and we end up

64
00:03:08,520 --> 00:03:09,660
with remainder 0,

65
00:03:09,660 --> 00:03:11,880
then it's clearly not a prime number.

66
00:03:11,880 --> 00:03:14,733
So we set is_prime to "False".

67
00:03:16,170 --> 00:03:18,570
Now the final part is the easiest.

68
00:03:18,570 --> 00:03:19,890
We just have an if and else statement.

69
00:03:19,890 --> 00:03:22,530
If at the end of our for loop,

70
00:03:22,530 --> 00:03:26,130
once we've checked through all of the numbers between 2

71
00:03:26,130 --> 00:03:30,210
up to the number that we're questioning and none

72
00:03:30,210 --> 00:03:34,050
of those happen to allow us to divide our number by it

73
00:03:34,050 --> 00:03:36,330
and have no remainder,

74
00:03:36,330 --> 00:03:41,330
then it's clearly a prime number and our is_prime variable

75
00:03:41,400 --> 00:03:43,830
is untouched, stays as "True".

76
00:03:43,830 --> 00:03:45,870
If anything else happens

77
00:03:45,870 --> 00:03:49,080
then it's obviously not a prime number

78
00:03:49,080 --> 00:03:52,274
and it is divisible by some number between 2

79
00:03:52,274 --> 00:03:53,643
and our number.

80
00:03:54,480 --> 00:03:57,150
The hardest part of this is to think

81
00:03:57,150 --> 00:04:00,150
and reason through the range that we create

82
00:04:00,150 --> 00:04:04,860
and using that range in our loop to check our number.

83
00:04:04,860 --> 00:04:07,770
See if you can understand how the logic works

84
00:04:07,770 --> 00:04:10,230
and if you had any issues passing the test

85
00:04:10,230 --> 00:04:11,640
in the previous lesson

86
00:04:11,640 --> 00:04:13,890
then go back there and try and fix your code.

