1
00:00:00,480 --> 00:00:02,490
All right, let's debug exercise number three.

2
00:00:05,480 --> 00:00:10,820
So in this exercise, it's supposed to output a random number between one and 15 printed and then print

3
00:00:10,890 --> 00:00:13,100
whether that number is prime or composites.

4
00:00:13,850 --> 00:00:17,360
Now, prime means that the number is only divisible by itself and one.

5
00:00:17,630 --> 00:00:20,300
So numbers like 1117 are 43.

6
00:00:20,810 --> 00:00:21,950
But if we run the code.

7
00:00:26,570 --> 00:00:27,680
Clearly it doesn't work.

8
00:00:28,310 --> 00:00:31,640
And we're going to use breakpoints to visualize the runtime and debug the issue.

9
00:00:44,290 --> 00:00:45,550
And there's your first mistake.

10
00:00:45,550 --> 00:00:47,530
It returns 16 when it should return.

11
00:00:47,530 --> 00:00:49,750
Year enemy number between one and 15.

12
00:00:50,260 --> 00:00:51,820
But why is the code crashing?

13
00:00:52,360 --> 00:00:54,010
Let's not worry about that right now.

14
00:00:54,040 --> 00:00:57,190
There's clearly a bug right here, so let's step into the function.

15
00:01:04,010 --> 00:01:06,200
And now it's perfectly clear what the error was.

16
00:01:06,500 --> 00:01:13,430
The range that comes in is 15 plus one, plus that random decimal, which is going to be 16 point something.

17
00:01:23,050 --> 00:01:26,500
And then casting that to an integer is always going to return 16.

18
00:01:32,410 --> 00:01:33,940
So clearly this is all wrong.

19
00:01:34,540 --> 00:01:39,940
Instead, we should multiply the range by the random value that gets returned and then add one.

20
00:01:44,470 --> 00:01:45,700
All right, let's try this again.

21
00:01:45,820 --> 00:01:46,900
Replay the debugger.

22
00:01:52,930 --> 00:01:53,650
Much better.

23
00:01:55,440 --> 00:01:59,490
We can keep testing this out and it keeps returning a random number each time.

24
00:02:08,259 --> 00:02:08,580
Okay.

25
00:02:08,680 --> 00:02:12,070
That number gets stored in a variable named random and gets printed.

26
00:02:12,370 --> 00:02:12,970
Okay.

27
00:02:15,580 --> 00:02:18,460
Now the crash happens in this function, so we'll step into it.

28
00:02:21,880 --> 00:02:24,250
First, it's going to go inside the print line function.

29
00:02:24,580 --> 00:02:25,430
We don't care about that.

30
00:02:25,450 --> 00:02:26,320
So step out of it.

31
00:02:35,740 --> 00:02:37,390
And this is the last line of code.

32
00:02:37,390 --> 00:02:39,190
So I'm assuming here is where it crashes.

33
00:02:40,040 --> 00:02:40,430
Okay.

34
00:02:40,450 --> 00:02:41,230
What's going on?

35
00:02:41,830 --> 00:02:43,450
We have a random number coming in.

36
00:02:45,230 --> 00:02:46,850
And that number modulus.

37
00:02:46,850 --> 00:02:48,590
Some value is causing the crash.

38
00:02:48,760 --> 00:02:50,270
Ah, that makes sense.

39
00:02:50,780 --> 00:02:51,660
As you know by now.

40
00:02:51,680 --> 00:02:56,720
Modulus means dividing two numbers and returning the remainder, and you can never divide a number by

41
00:02:56,720 --> 00:02:57,050
zero.

42
00:02:57,050 --> 00:03:00,200
That's impossible, which causes an arithmetic exception.

43
00:03:03,880 --> 00:03:07,360
And so what we want to do is start the loop counter at one, none at zero.

44
00:03:07,870 --> 00:03:09,040
All right, let's try this out.

45
00:03:21,280 --> 00:03:21,640
Wait.

46
00:03:21,640 --> 00:03:23,320
This number is not a prime number.

47
00:03:23,440 --> 00:03:24,340
There's something wrong.

48
00:03:26,280 --> 00:03:28,260
Let's restart the debugger and go through the loop.

49
00:03:33,790 --> 00:03:34,030
Okay.

50
00:03:34,120 --> 00:03:36,130
So this number should be a composite.

51
00:03:45,270 --> 00:03:50,600
And the remainder from dividing this number by one and zero, which would make it composite.

52
00:03:50,610 --> 00:03:51,570
But wait a second.

53
00:03:51,780 --> 00:03:56,220
Every number is divisible by one, so it doesn't really make sense to start the loop at one.

54
00:03:56,940 --> 00:03:58,410
Anyways, let's keep going.

55
00:04:02,710 --> 00:04:06,580
It is divisible by two into the fact that it's divisible by two means.

56
00:04:06,580 --> 00:04:09,940
We know this number is composite, so we don't need to keep going with the loop.

57
00:04:14,930 --> 00:04:20,690
But the loop keeps going and because it's not divisible by three, it marks it back as prime, which

58
00:04:20,690 --> 00:04:21,950
doesn't make any sense.

59
00:04:28,440 --> 00:04:33,620
The first thing we need to do is break the loop as soon as we establish that the random number is composites.

60
00:04:50,620 --> 00:04:52,210
All right, relaunch the debugger.

61
00:04:59,870 --> 00:05:00,020
Okay.

62
00:05:00,120 --> 00:05:02,450
This time you get a number that's clearly prime.

63
00:05:10,520 --> 00:05:15,020
And remember the rule that you can't divide a prime number by anything except itself in one.

64
00:05:15,680 --> 00:05:20,810
But wait, our code isn't going to work because the loop starts at one and it's going to think, okay,

65
00:05:20,810 --> 00:05:23,840
this number divided by one returns a remainder of zero.

66
00:05:24,050 --> 00:05:27,350
So clearly this number is composite and it's going to break the loop.

67
00:05:27,890 --> 00:05:29,720
And here you can see that happen in real time.

68
00:05:39,600 --> 00:05:45,420
This number is not a composite number, and dividing it by one should not be a criteria to determine

69
00:05:45,420 --> 00:05:47,430
if a number is prime or composites.

70
00:05:49,420 --> 00:05:51,730
So we need to start the loop off at two.

71
00:05:53,180 --> 00:05:54,380
All right, let's try this again.

72
00:06:02,430 --> 00:06:03,870
Now we get a composite number.

73
00:06:13,130 --> 00:06:15,620
That number is indeed divisible by two.

74
00:06:15,860 --> 00:06:19,010
So it's going to return a remainder of zero, which means it's composite.

75
00:06:19,040 --> 00:06:20,540
Break the loop and we're good.

76
00:06:33,070 --> 00:06:34,510
All right, let's keep running tests.

77
00:06:35,930 --> 00:06:36,120
Ken.

78
00:06:36,170 --> 00:06:37,370
Now we've got a prime number.

79
00:06:37,370 --> 00:06:39,530
That number is not divisible by anything.

80
00:06:45,550 --> 00:06:50,620
So it's going to keep going through the entire loop and none of the numbers are going to return a non-zero

81
00:06:50,620 --> 00:06:51,310
modulus.

82
00:06:51,610 --> 00:06:55,840
And since the number is not divisible by anything, it's always going to be prime.

83
00:07:06,940 --> 00:07:07,750
And that's all.

84
00:07:14,930 --> 00:07:16,670
Now I would like to rewrite this code.

85
00:07:17,060 --> 00:07:20,660
A more efficient way to do this is to start the variable office prime.

86
00:07:28,430 --> 00:07:33,200
And if the numbers happen to be divisible by one of the numbers inside the range.

87
00:07:38,910 --> 00:07:41,640
Then we're going to label it as composite and break the loop.

88
00:07:57,630 --> 00:07:58,880
To me, that makes a lot more sense.

89
00:07:58,890 --> 00:08:00,930
So let's rerun and visualize the runtime.

90
00:08:06,020 --> 00:08:06,290
Okay.

91
00:08:06,350 --> 00:08:07,790
Now we're going to composite number.

92
00:08:13,160 --> 00:08:14,750
First we assume that it's prime.

93
00:08:18,240 --> 00:08:20,580
That number is indeed divisible by two.

94
00:08:22,420 --> 00:08:25,240
So this condition is going to be true, making it composite.

95
00:08:32,520 --> 00:08:34,710
Breaking the loop and returning the results.

96
00:08:51,590 --> 00:08:51,970
All right.

97
00:08:51,980 --> 00:08:53,420
This time you get a prime number.

98
00:09:01,160 --> 00:09:02,630
First we assume it's prime.

99
00:09:02,990 --> 00:09:04,730
It's not going to divide by anything.

100
00:09:04,730 --> 00:09:05,930
So it will stay prime.

101
00:09:17,140 --> 00:09:20,370
Eventually the loop is going to break and return a prime result.

102
00:09:25,910 --> 00:09:28,820
And the one last thing is that the number one is not a prime number.

103
00:09:30,870 --> 00:09:36,480
So in the event that the random number returned is one, this function is actually going to return prime,

104
00:09:37,020 --> 00:09:38,190
which is incorrect.

105
00:10:09,490 --> 00:10:14,410
So here what I can do is use the ternary operator to say if random value equals one.

106
00:10:32,950 --> 00:10:38,380
Otherwise we'll pass that number into the is prime function and then it returned the correct results.

107
00:10:40,960 --> 00:10:41,290
All right.

108
00:10:41,290 --> 00:10:41,980
One more time.

109
00:10:48,510 --> 00:10:49,320
Number is one.

110
00:10:49,500 --> 00:10:50,520
What a coincidence.

111
00:10:53,630 --> 00:10:54,920
This evaluates the true.

112
00:11:10,450 --> 00:11:11,080
This time.

113
00:11:13,700 --> 00:11:18,350
This evaluates the files and it's going to run the function which determines whether the value is prime

114
00:11:18,350 --> 00:11:19,130
or composite.

115
00:11:26,970 --> 00:11:29,220
In this case, it ends up being composites.

116
00:11:37,310 --> 00:11:41,120
By the way, you don't have to use the ternary operator if you're not comfortable with it.

117
00:11:41,330 --> 00:11:43,100
You can use standard default if you want.

118
00:11:43,130 --> 00:11:44,030
It's the same thing.

119
00:11:44,870 --> 00:11:45,330
All right.

120
00:11:45,370 --> 00:11:45,830
That's all.

