Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,510 --> 00:00:08,010
All right, so let's take a look at the previous solution of our previous exercise, where we had to
2
00:00:08,460 --> 00:00:12,560
sum up all the numbers that can be divided by three and by five.
3
00:00:13,140 --> 00:00:18,990
And we said that we have two options here, the trivial solution and the optimal solution.
4
00:00:19,380 --> 00:00:20,940
And let's see what we can do right now.
5
00:00:20,970 --> 00:00:29,670
So basically, just a quick reminder of what we are doing, summing up, summing up all the numbers
6
00:00:30,720 --> 00:00:36,900
from one to two to that can be divided.
7
00:00:37,200 --> 00:00:44,600
That can be divided by three, four by five.
8
00:00:44,850 --> 00:00:48,630
So that's what we basically do in this exercise.
9
00:00:49,510 --> 00:00:52,710
OK, so let's simply take a look at the program.
10
00:00:52,710 --> 00:00:57,840
So in time that will be used for the for loop and ngom in some equals to zero.
11
00:00:57,840 --> 00:00:58,470
Awesome.
12
00:00:58,480 --> 00:01:05,040
So reading the value of Naameh and storing it inside the variable, you now will be talking about the
13
00:01:05,040 --> 00:01:06,120
trivial solution.
14
00:01:06,150 --> 00:01:12,850
So for I equals to one, ok, as long as I is less than or equal to now I plus plus.
15
00:01:12,870 --> 00:01:21,030
So simply iterating over all the elements, all the numbers from one up to number and asking the following
16
00:01:21,030 --> 00:01:30,140
question, if I can be divided by three or OK, I can be divided by five without a remainder.
17
00:01:30,180 --> 00:01:37,620
So if either of these conditions happen to be true or even both of them are true, then in this case
18
00:01:37,650 --> 00:01:41,370
the final result of these aph will be also true.
19
00:01:41,370 --> 00:01:43,620
And we will execute the following line of code.
20
00:01:43,810 --> 00:01:47,940
For example, summing up, OK, the eye itself.
21
00:01:49,350 --> 00:01:53,060
OK, so that's pretty much a good option.
22
00:01:53,070 --> 00:01:59,820
So let's simply coming out the optimised option because we haven't spoken yet.
23
00:02:00,850 --> 00:02:02,120
So where is it?
24
00:02:03,040 --> 00:02:06,680
Sorry, so that's the optimal.
25
00:02:06,700 --> 00:02:09,640
Okay, so let's go like this, let's comment out.
26
00:02:10,000 --> 00:02:16,830
It's optimal and also it's coming down out for a loop, so it will look like this.
27
00:02:16,860 --> 00:02:19,140
So that's just something I'm doing for for now.
28
00:02:19,150 --> 00:02:23,480
So let's just execute the trivial solution and see what happens.
29
00:02:24,370 --> 00:02:27,310
So build and run it and what we'll do.
30
00:02:27,310 --> 00:02:30,560
I will not use a hundred here because it's a lot of numbers.
31
00:02:30,580 --> 00:02:32,420
Let's use just something like 15.
32
00:02:32,890 --> 00:02:40,480
So enter now I'm 50 and yeah, here we have the message of the message because percentage they can be
33
00:02:40,480 --> 00:02:46,780
divided by either instead of both are either three or five.
34
00:02:47,060 --> 00:02:51,490
OK, so let's use here 50.
35
00:02:51,490 --> 00:02:52,300
Come here.
36
00:02:52,300 --> 00:02:52,600
Come on.
37
00:02:52,600 --> 00:02:53,060
Console.
38
00:02:53,800 --> 00:02:54,570
So 50.
39
00:02:54,580 --> 00:03:00,430
So what we will see here is that three can be divided by either three or five.
40
00:03:00,610 --> 00:03:03,670
Five can be divided by either three or five.
41
00:03:03,670 --> 00:03:03,970
Right.
42
00:03:03,970 --> 00:03:06,220
Because and six can be divided by three.
43
00:03:06,220 --> 00:03:07,780
Nine can be divided by three.
44
00:03:07,780 --> 00:03:09,390
Ten can be divided by five.
45
00:03:09,760 --> 00:03:10,290
That's OK.
46
00:03:10,300 --> 00:03:15,980
So these are all the numbers that we take into consideration in into our son.
47
00:03:17,140 --> 00:03:17,670
Awesome.
48
00:03:18,190 --> 00:03:20,190
So that works perfectly.
49
00:03:20,230 --> 00:03:27,240
And how how many iterations that we have inside of this loop, inside of this solution.
50
00:03:28,030 --> 00:03:32,800
So basically we had here about num iterations, right.
51
00:03:32,800 --> 00:03:36,430
So we had num iterations in case of fifty.
52
00:03:36,430 --> 00:03:40,840
So we had executed the body of the slope fifty times.
53
00:03:40,840 --> 00:03:46,670
OK, and if now would be a hundred, we executed a hundred times and so on.
54
00:03:47,680 --> 00:03:56,140
So the question comes are whether can we do you think that we can find some way that we can at least
55
00:03:56,140 --> 00:04:04,360
optimize it a little bit so that these we will have less operations and less calculations and even removing
56
00:04:04,360 --> 00:04:06,520
these condition at all?
57
00:04:07,360 --> 00:04:16,060
And the answer is probably yes, because once again, my suggestion is to take a look at this trivial
58
00:04:16,060 --> 00:04:19,080
solution and the result printed on the screen.
59
00:04:20,020 --> 00:04:24,980
So let's take a look and take a look at all the numbers, OK?
60
00:04:25,000 --> 00:04:29,170
That, first of all, can be divided by three without the remainder.
61
00:04:29,350 --> 00:04:31,030
OK, so let's take a look at them.
62
00:04:31,480 --> 00:04:38,770
So we have three, we have six, nine, 12, 15, six, 15, 18, 21, 20.
63
00:04:39,970 --> 00:04:40,420
What is it.
64
00:04:40,420 --> 00:04:41,350
Twenty four.
65
00:04:41,470 --> 00:04:42,370
Twenty seven.
66
00:04:42,370 --> 00:04:44,230
Thirty, thirty three and so on.
67
00:04:44,530 --> 00:04:48,850
OK, what's special about this sequence that I've just shown you?
68
00:04:49,510 --> 00:04:57,100
The special about it is that every time you increment the previous value by three, so three gives you
69
00:04:57,100 --> 00:05:03,460
six plus three plus three, it gives you six plus three gives you nines plus three gives you twelve
70
00:05:03,460 --> 00:05:04,720
and so on and so forth.
71
00:05:04,720 --> 00:05:08,620
OK, so these will be all the numbers that can be divided by three.
72
00:05:09,400 --> 00:05:14,770
And the same approach can be said also about numbers that can be divided by five.
73
00:05:14,770 --> 00:05:18,290
So you have five and then you have ten like five plus five.
74
00:05:18,290 --> 00:05:23,230
If ten is ten plus five is fifteen plus five is twenty and so on.
75
00:05:24,010 --> 00:05:24,400
Right.
76
00:05:24,400 --> 00:05:28,570
So you have a lot of numbers that can be divided by five.
77
00:05:28,930 --> 00:05:36,340
And you know that these numbers are simply the previous, the last number that we divided by five without
78
00:05:36,340 --> 00:05:37,740
the remainder plus five.
79
00:05:38,230 --> 00:05:44,860
OK, so do you feel me, did you start getting the idea of what we are going to do right now if.
80
00:05:44,860 --> 00:05:46,060
Yes, that's awesome.
81
00:05:46,060 --> 00:05:50,200
If not, don't worry, we'll cover everything up in this optimal solution.
82
00:05:50,200 --> 00:05:53,040
So let's start with the optimal solution, shall we.
83
00:05:53,890 --> 00:06:00,040
So printf optimal solution and this will be the optimal solution and let's just remove it here will
84
00:06:00,040 --> 00:06:01,070
come into town.
85
00:06:01,990 --> 00:06:04,480
So what do we want to do?
86
00:06:04,510 --> 00:06:10,720
I want to reduce at least a little bit the number of of iterations we are doing.
87
00:06:11,500 --> 00:06:17,710
And for that, what I want us to do is, first of all, let's go over all the numbers that can be divided
88
00:06:17,710 --> 00:06:18,370
by what?
89
00:06:19,270 --> 00:06:21,460
That can be divided by three.
90
00:06:21,490 --> 00:06:25,780
OK, so four I equals two and start with three.
91
00:06:26,110 --> 00:06:36,130
As long as I is less than or equal to num i plus plus i plus plus or I plus equals the three which will
92
00:06:36,130 --> 00:06:41,220
simply execute the following line I equals to plus three.
93
00:06:42,160 --> 00:06:50,650
So this way will sum all of the the numbers from let's say one of two num that can be divided by three.
94
00:06:50,650 --> 00:06:54,490
So this will be some equal to previous sum.
95
00:06:54,640 --> 00:06:59,320
Plus I will write and that means that.
96
00:06:59,650 --> 00:07:00,250
Well how many.
97
00:07:00,290 --> 00:07:07,610
Times of the body of these will be executed, that's very simple, since we increment I every time by
98
00:07:07,610 --> 00:07:13,550
three, then we can say that the body of this will be executed, divided by three times.
99
00:07:13,910 --> 00:07:18,920
So if we have 100 nomy equals 200, then this loop will be executed.
100
00:07:18,920 --> 00:07:25,220
Let's say now equals two hundred, but then the body of this loop will be executed just about thirty
101
00:07:25,220 --> 00:07:26,060
three times.
102
00:07:26,810 --> 00:07:27,380
Awesome.
103
00:07:27,380 --> 00:07:35,810
So that's OK because here we have like if num equals to 100, I mean in this case we said that we will
104
00:07:35,810 --> 00:07:38,810
execute the body of this loop 100 times.
105
00:07:39,920 --> 00:07:40,870
So that's OK.
106
00:07:41,330 --> 00:07:48,640
And now what we also have to do is to find out all in some all the values that can be divided by five.
107
00:07:49,130 --> 00:07:57,170
So for a little use for I equals to five times less than or equal to now I equals to I plus five.
108
00:07:57,440 --> 00:08:00,970
OK, and in this case we will do the following thing.
109
00:08:01,310 --> 00:08:04,510
So one option is to say some equals to some Placide.
110
00:08:06,020 --> 00:08:08,270
But there is a problem here.
111
00:08:08,300 --> 00:08:11,240
What do you think the problem will be in search of a solution.
112
00:08:11,540 --> 00:08:17,870
OK, because yeah, we'll sum up all the numbers from one up to number that can be divided by five and
113
00:08:17,870 --> 00:08:19,720
there is no question about it.
114
00:08:20,150 --> 00:08:24,950
But the problem is, is that we are going to have duplicates here, duplicate some.
115
00:08:25,800 --> 00:08:26,810
What do I mean?
116
00:08:26,810 --> 00:08:30,440
For example, let's say you have the value of 15.
117
00:08:30,680 --> 00:08:32,910
OK, so I equals to 15.
118
00:08:33,320 --> 00:08:38,780
So you summed it up here because 15 is also a multiplication of three.
119
00:08:38,870 --> 00:08:41,000
So you took into account 15 here.
120
00:08:41,510 --> 00:08:44,570
You took it into account as part of the sum.
121
00:08:44,970 --> 00:08:51,130
And later on, you also came here and you found out that 15 can be also divided by five and you took
122
00:08:51,140 --> 00:08:52,680
it once again into account.
123
00:08:53,030 --> 00:08:58,380
So that's a duplication of taking into account a given number.
124
00:08:58,850 --> 00:09:04,970
So what you have to do in order to fix that is simply to ask the following question, if I can be divided
125
00:09:04,970 --> 00:09:06,680
by three without a remainder.
126
00:09:06,920 --> 00:09:07,430
OK.
127
00:09:08,210 --> 00:09:09,610
So it will be like this.
128
00:09:09,890 --> 00:09:14,340
So if basically if I cannot be divided by three.
129
00:09:14,540 --> 00:09:16,340
OK, without the remainder.
130
00:09:16,350 --> 00:09:17,250
So that's OK.
131
00:09:17,270 --> 00:09:23,000
That means that we we didn't take it into.
132
00:09:23,000 --> 00:09:29,840
We haven't, we haven't take it into account yet.
133
00:09:29,930 --> 00:09:31,580
Right into the sum.
134
00:09:32,990 --> 00:09:37,010
So this way we will use the sum equals to sum plus.
135
00:09:37,430 --> 00:09:45,440
OK, so that way you eliminate your use in your duplications of the numbers.
136
00:09:46,310 --> 00:09:47,410
So awesome.
137
00:09:47,420 --> 00:09:48,830
And yeah.
138
00:09:48,830 --> 00:09:54,430
So finally what I want to ask you is how many times this loop is going to be executed.
139
00:09:54,680 --> 00:09:56,180
And the answer is very simple.
140
00:09:56,180 --> 00:09:57,780
NUM divided by five.
141
00:09:57,800 --> 00:09:58,080
Right.
142
00:09:58,100 --> 00:10:06,140
Because every time we simply go in steps of five, so divided by five and if we had num like equal to
143
00:10:06,140 --> 00:10:13,460
100 like we said, then it will be a total of 20, 20 commands, 20 durations of the loop.
144
00:10:13,940 --> 00:10:19,070
So you can see we optimized it, although not very much, but at least a little bit.
145
00:10:19,490 --> 00:10:27,920
We optimized it so that this way we have fifty three commands while fifty three entries of these little
146
00:10:27,920 --> 00:10:33,160
buddy and here we have a hundred of four iterations.
147
00:10:33,650 --> 00:10:39,350
So if you will increase num you will see more of difference and more and more as you just increase the
148
00:10:39,350 --> 00:10:39,600
number.
149
00:10:40,580 --> 00:10:47,460
So yeah guys these are basically the two solutions, the trivial one and the optimal solution.
150
00:10:48,320 --> 00:10:53,290
So just to let you know that you can once again build and run it and see what happens.
151
00:10:53,300 --> 00:10:58,850
And yeah, that's not the main point here to find the numbers, but just to understand the logic and
152
00:10:58,850 --> 00:11:06,070
to understand how everything works behind the scenes and how to make it optimal and optimized.
153
00:11:07,190 --> 00:11:08,540
So thank you guys for watching.
154
00:11:08,690 --> 00:11:13,390
Keep on practicing and we will meet in the next videos.
155
00:11:14,530 --> 00:11:15,890
Adios.
14372
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.