Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,390 --> 00:00:06,630
All right, so let's talk about the solution for this exercise, so we are looking for let's first of
2
00:00:06,630 --> 00:00:08,490
all, just receive the value of an arm.
3
00:00:08,490 --> 00:00:14,930
So in between Jeff and her and her, OK?
4
00:00:15,240 --> 00:00:20,410
And basically what we are going to do is to read this value into storage inside variable naoum.
5
00:00:20,430 --> 00:00:22,080
Nothing special so far.
6
00:00:22,710 --> 00:00:23,240
Right.
7
00:00:24,390 --> 00:00:29,090
And now what do we have to do is simply to let's start with the trivial solution.
8
00:00:29,100 --> 00:00:33,900
So let's create additional variable AI and use a for loop for for that.
9
00:00:34,070 --> 00:00:38,940
OK, so this will be, let's call it a trivial solution.
10
00:00:39,210 --> 00:00:42,520
OK, the basic one and probably not the most optimal.
11
00:00:43,260 --> 00:00:50,850
So let's use here for AI equals to one as long as I use less than or equal to AI plus plus.
12
00:00:52,020 --> 00:00:55,550
So inside of these for a loop we are going to test two conditions.
13
00:00:55,920 --> 00:01:00,950
So we are looking for a numbers that can be divided both by three and by five.
14
00:01:01,320 --> 00:01:03,090
So this condition will look like this.
15
00:01:03,090 --> 00:01:13,680
If I can be divided by three without a remainder and I can be divided by five without the remainder,
16
00:01:14,370 --> 00:01:19,070
then in this case what we will do is just not to bring the result.
17
00:01:19,080 --> 00:01:21,190
We should also sum up all the variables.
18
00:01:21,210 --> 00:01:25,640
So let's create additional variable sum instead of value of zero.
19
00:01:26,250 --> 00:01:33,990
So some will be equal to the previous sum which we had so far, plus the value of AI.
20
00:01:34,410 --> 00:01:38,010
And finally, we should print all the sum like this.
21
00:01:38,010 --> 00:01:46,760
So sum equals to percentage and of the value of some awesome.
22
00:01:47,340 --> 00:01:49,740
So some awesome, amazing.
23
00:01:49,740 --> 00:01:52,090
Let's just build and run it and see what happens.
24
00:01:52,500 --> 00:01:54,870
So enter it's 300.
25
00:01:55,230 --> 00:01:58,620
So basically the sum will be three hundred and fifteen.
26
00:01:58,980 --> 00:02:05,340
And if we want to know also what are the values that are included in the sum.
27
00:02:05,490 --> 00:02:08,770
So let's simply copy that here.
28
00:02:09,630 --> 00:02:21,450
So and also preened the value that we found, let's say percentage D can be divided by both three and
29
00:02:21,600 --> 00:02:22,010
five.
30
00:02:22,410 --> 00:02:22,770
OK.
31
00:02:23,260 --> 00:02:24,720
Let's listen in here.
32
00:02:24,720 --> 00:02:28,320
Instead of the percentage of pool is the value of AI awesome.
33
00:02:29,400 --> 00:02:38,520
So let's build and run, it's just, you know, what is part of our of our final sound system, so international,
34
00:02:38,520 --> 00:02:39,700
it's a hundred.
35
00:02:40,110 --> 00:02:42,780
So 15 can be divided by three and five.
36
00:02:42,780 --> 00:02:44,130
Thirty, forty five.
37
00:02:44,130 --> 00:02:47,610
Sixty, seventy five and ninety, which seemed pretty legit.
38
00:02:47,910 --> 00:02:49,710
And that's the final sum.
39
00:02:50,130 --> 00:02:55,380
OK, so the solution for this exercise actually works and that's OK.
40
00:02:55,710 --> 00:03:01,290
But that's, as we said, the trivial solution and not the optimal one.
41
00:03:01,650 --> 00:03:06,030
What we would like to do is let's take another look at this program.
42
00:03:06,030 --> 00:03:08,610
Let's say we had, I know, up to a thousand.
43
00:03:09,330 --> 00:03:14,220
So we can see here a lot of numbers that can be divided by both three and five.
44
00:03:14,640 --> 00:03:16,530
Fifteen, thirty, forty five.
45
00:03:16,530 --> 00:03:17,610
Sixty seventy five.
46
00:03:17,610 --> 00:03:19,830
Ninety one hundred and five 120.
47
00:03:20,670 --> 00:03:28,440
And what I want you to stoop to, to do is to take a close look, a close look at the numbers that you
48
00:03:28,440 --> 00:03:30,720
can see here, which are part of the sum.
49
00:03:31,560 --> 00:03:35,760
And there is some rule, some similarity between them.
50
00:03:35,910 --> 00:03:38,040
And the similarity is very simple.
51
00:03:38,040 --> 00:03:41,340
You can see that we started from 15, right.
52
00:03:41,340 --> 00:03:46,860
Because 15 is the first number from one up to 15 that can be divided by both three and five.
53
00:03:47,580 --> 00:03:50,610
And then we increment it again by 15.
54
00:03:50,610 --> 00:03:57,660
So 30 is 15 and 15, multiplied by two, multiplied by three, multiplied by four by five.
55
00:03:57,670 --> 00:04:02,490
So every, let's say, iteration that we can use it.
56
00:04:02,490 --> 00:04:10,710
Every time that we take a number, we simply take the previous one and add 15 to to it in order to get
57
00:04:10,710 --> 00:04:13,920
its value that can be printed by both three and five.
58
00:04:14,760 --> 00:04:18,330
So in this case, if we are smart programmers.
59
00:04:18,330 --> 00:04:18,780
Right.
60
00:04:18,780 --> 00:04:27,390
And we are, I suggest to do the following so we know every time we take the value of 15, we can simply
61
00:04:27,390 --> 00:04:28,360
added to the sum.
62
00:04:28,890 --> 00:04:34,410
So this condition of checking all the numbers from one up to number and if you have now, I don't know,
63
00:04:34,410 --> 00:04:38,040
like a million, OK, what is it, a million.
64
00:04:38,490 --> 00:04:41,130
So this will be a lot of numbers.
65
00:04:41,130 --> 00:04:41,480
Right.
66
00:04:41,490 --> 00:04:42,270
And that's OK.
67
00:04:42,450 --> 00:04:47,810
But the problem is that why do we need so much time to check the result?
68
00:04:47,860 --> 00:04:49,750
OK, well, why do we need it?
69
00:04:50,550 --> 00:04:57,240
So what I want to do us for us, first of all, let's say this is the trivial solution and let's go
70
00:04:57,240 --> 00:05:00,060
with the optimized solution.
71
00:05:00,070 --> 00:05:09,450
So optimized solution and not what I want us to do is just to remove these conditions because it's not
72
00:05:09,450 --> 00:05:16,650
necessary and also remove this one and also remove that one, leave just the sum and the values that
73
00:05:16,650 --> 00:05:24,600
can be divided by both three and five and simply start this little bit from I equals to 15 as long as
74
00:05:24,600 --> 00:05:26,400
I is less than or equal to NUM.
75
00:05:26,880 --> 00:05:33,090
And what we will do here is not increment the value of I by one, but rather we know that every time
76
00:05:33,090 --> 00:05:39,880
that we will incremented by 15, the result will be divided by both three and five.
77
00:05:39,900 --> 00:05:44,730
So what we will do is I equals to I lost fifteen.
78
00:05:45,210 --> 00:05:50,310
OK, so that's basically what we will do at the end of every iteration.
79
00:05:51,090 --> 00:05:51,690
All right.
80
00:05:51,720 --> 00:05:53,760
So this will be the optimized solution.
81
00:05:54,270 --> 00:05:55,170
Let's go like this.
82
00:05:55,170 --> 00:05:56,790
Let's leave the sum here.
83
00:05:57,300 --> 00:05:57,990
Uncommon.
84
00:05:59,340 --> 00:06:00,610
OK, uncommon.
85
00:06:00,690 --> 00:06:02,340
It will come and eat out and.
86
00:06:03,570 --> 00:06:10,080
Yeah, OK, so let's print pre-emptive trivial solution.
87
00:06:10,920 --> 00:06:13,170
This will be the trivial solution numbers.
88
00:06:13,590 --> 00:06:20,310
And I will also use here once we simply print it out and found out all the numbers which are part of
89
00:06:20,310 --> 00:06:25,650
the trivial solution, but simply print the optimal optimal solution.
90
00:06:25,920 --> 00:06:27,420
OK, numbers.
91
00:06:28,380 --> 00:06:29,880
So let's build an and on it.
92
00:06:30,420 --> 00:06:33,090
Let's go up to up to hundred.
93
00:06:33,330 --> 00:06:34,080
OK, shall we.
94
00:06:34,920 --> 00:06:36,030
So let's see.
95
00:06:36,620 --> 00:06:37,440
Come here.
96
00:06:37,510 --> 00:06:38,840
So one hundred.
97
00:06:39,360 --> 00:06:40,920
So fifteen thirty.
98
00:06:40,920 --> 00:06:47,580
OK, so these are the numbers under the trivial solution and the optimal solution gives you the same
99
00:06:47,580 --> 00:06:48,130
numbers.
100
00:06:48,150 --> 00:06:50,910
OK, so the sum is going also to be the same.
101
00:06:51,420 --> 00:06:58,100
But what we've done here and what we've added to this kind of solution is simply optimizing it and removing
102
00:06:58,130 --> 00:07:02,760
the check of the need to check both of these conditions.
103
00:07:02,760 --> 00:07:06,480
So every time for every I you you use this condition.
104
00:07:06,630 --> 00:07:10,820
And then if this condition was true, you would also check this condition.
105
00:07:11,400 --> 00:07:13,770
So that's something we do not do here.
106
00:07:13,770 --> 00:07:23,680
We also do not we eliminate the need to check all the numbers between one up to 15, OK, not included.
107
00:07:23,820 --> 00:07:26,040
And then from sixteen up to thirty.
108
00:07:26,250 --> 00:07:27,660
So that's a hell.
109
00:07:27,660 --> 00:07:28,860
Lot of less of.
110
00:07:28,910 --> 00:07:40,130
Operation that we do so basically here from Wynnum equals to a hundred, we just use the instrumentation
111
00:07:40,460 --> 00:07:42,360
about six or seven times.
112
00:07:42,380 --> 00:07:48,950
OK, so I changes just six or seven times while here he changes about a hundred times.
113
00:07:48,950 --> 00:07:49,300
Right.
114
00:07:49,310 --> 00:07:53,900
I equals to one I plus plus up until a hundred or a hundred one.
115
00:07:54,720 --> 00:07:55,070
Hey.
116
00:07:55,550 --> 00:07:55,960
Yeah.
117
00:07:56,120 --> 00:07:58,010
So that's the optimal solution.
118
00:07:58,010 --> 00:07:59,060
The optimal way.
119
00:07:59,090 --> 00:08:02,720
I hope you succeeded to solve this exercise on your own.
120
00:08:02,920 --> 00:08:05,920
If not, please close this exercise right.
121
00:08:05,930 --> 00:08:12,740
The notes down, open up your idea and solve this exercise on your own from scratch and make sure that
122
00:08:12,740 --> 00:08:15,710
everything works exactly as you expected for you.
123
00:08:16,070 --> 00:08:21,020
Because once again, guys, it's great that I'm solving this exercise for you.
124
00:08:21,410 --> 00:08:30,500
But if you want to practice and become a better programmer and to like to have memorized all the answers
125
00:08:30,500 --> 00:08:37,160
and all the solutions and all the nuances that we talk about here, that's very important to solve these
126
00:08:37,160 --> 00:08:39,630
exercises on your own.
127
00:08:40,370 --> 00:08:41,990
So thank you so much for watching.
128
00:08:42,020 --> 00:08:44,870
My name is Vlad and I will see you in the next video.
11508
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.