Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, so let's take a look at the previous solution of our previous exercise, where we had to
sum up all the numbers that can be divided by three and by five.
And we said that we have two options here, the trivial solution and the optimal solution.
And let's see what we can do right now.
So basically, just a quick reminder of what we are doing, summing up, summing up all the numbers
from one to two to that can be divided.
That can be divided by three, four by five.
So that's what we basically do in this exercise.
OK, so let's simply take a look at the program.
So in time that will be used for the for loop and ngom in some equals to zero.
Awesome.
So reading the value of Naameh and storing it inside the variable, you now will be talking about the
trivial solution.
So for I equals to one, ok, as long as I is less than or equal to now I plus plus.
So simply iterating over all the elements, all the numbers from one up to number and asking the following
question, if I can be divided by three or OK, I can be divided by five without a remainder.
So if either of these conditions happen to be true or even both of them are true, then in this case
the final result of these aph will be also true.
And we will execute the following line of code.
For example, summing up, OK, the eye itself.
OK, so that's pretty much a good option.
So let's simply coming out the optimised option because we haven't spoken yet.
So where is it?
Sorry, so that's the optimal.
Okay, so let's go like this, let's comment out.
It's optimal and also it's coming down out for a loop, so it will look like this.
So that's just something I'm doing for for now.
So let's just execute the trivial solution and see what happens.
So build and run it and what we'll do.
I will not use a hundred here because it's a lot of numbers.
Let's use just something like 15.
So enter now I'm 50 and yeah, here we have the message of the message because percentage they can be
divided by either instead of both are either three or five.
OK, so let's use here 50.
Come here.
Come on.
Console.
So 50.
So what we will see here is that three can be divided by either three or five.
Five can be divided by either three or five.
Right.
Because and six can be divided by three.
Nine can be divided by three.
Ten can be divided by five.
That's OK.
So these are all the numbers that we take into consideration in into our son.
Awesome.
So that works perfectly.
And how how many iterations that we have inside of this loop, inside of this solution.
So basically we had here about num iterations, right.
So we had num iterations in case of fifty.
So we had executed the body of the slope fifty times.
OK, and if now would be a hundred, we executed a hundred times and so on.
So the question comes are whether can we do you think that we can find some way that we can at least
optimize it a little bit so that these we will have less operations and less calculations and even removing
these condition at all?
And the answer is probably yes, because once again, my suggestion is to take a look at this trivial
solution and the result printed on the screen.
So let's take a look and take a look at all the numbers, OK?
That, first of all, can be divided by three without the remainder.
OK, so let's take a look at them.
So we have three, we have six, nine, 12, 15, six, 15, 18, 21, 20.
What is it.
Twenty four.
Twenty seven.
Thirty, thirty three and so on.
OK, what's special about this sequence that I've just shown you?
The special about it is that every time you increment the previous value by three, so three gives you
six plus three plus three, it gives you six plus three gives you nines plus three gives you twelve
and so on and so forth.
OK, so these will be all the numbers that can be divided by three.
And the same approach can be said also about numbers that can be divided by five.
So you have five and then you have ten like five plus five.
If ten is ten plus five is fifteen plus five is twenty and so on.
Right.
So you have a lot of numbers that can be divided by five.
And you know that these numbers are simply the previous, the last number that we divided by five without
the remainder plus five.
OK, so do you feel me, did you start getting the idea of what we are going to do right now if.
Yes, that's awesome.
If not, don't worry, we'll cover everything up in this optimal solution.
So let's start with the optimal solution, shall we.
So printf optimal solution and this will be the optimal solution and let's just remove it here will
come into town.
So what do we want to do?
I want to reduce at least a little bit the number of of iterations we are doing.
And for that, what I want us to do is, first of all, let's go over all the numbers that can be divided
by what?
That can be divided by three.
OK, so four I equals two and start with three.
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
simply execute the following line I equals to plus three.
So this way will sum all of the the numbers from let's say one of two num that can be divided by three.
So this will be some equal to previous sum.
Plus I will write and that means that.
Well how many.
Times of the body of these will be executed, that's very simple, since we increment I every time by
three, then we can say that the body of this will be executed, divided by three times.
So if we have 100 nomy equals 200, then this loop will be executed.
Let's say now equals two hundred, but then the body of this loop will be executed just about thirty
three times.
Awesome.
So that's OK because here we have like if num equals to 100, I mean in this case we said that we will
execute the body of this loop 100 times.
So that's OK.
And now what we also have to do is to find out all in some all the values that can be divided by five.
So for a little use for I equals to five times less than or equal to now I equals to I plus five.
OK, and in this case we will do the following thing.
So one option is to say some equals to some Placide.
But there is a problem here.
What do you think the problem will be in search of a solution.
OK, because yeah, we'll sum up all the numbers from one up to number that can be divided by five and
there is no question about it.
But the problem is, is that we are going to have duplicates here, duplicate some.
What do I mean?
For example, let's say you have the value of 15.
OK, so I equals to 15.
So you summed it up here because 15 is also a multiplication of three.
So you took into account 15 here.
You took it into account as part of the sum.
And later on, you also came here and you found out that 15 can be also divided by five and you took
it once again into account.
So that's a duplication of taking into account a given number.
So what you have to do in order to fix that is simply to ask the following question, if I can be divided
by three without a remainder.
OK.
So it will be like this.
So if basically if I cannot be divided by three.
OK, without the remainder.
So that's OK.
That means that we we didn't take it into.
We haven't, we haven't take it into account yet.
Right into the sum.
So this way we will use the sum equals to sum plus.
OK, so that way you eliminate your use in your duplications of the numbers.
So awesome.
And yeah.
So finally what I want to ask you is how many times this loop is going to be executed.
And the answer is very simple.
NUM divided by five.
Right.
Because every time we simply go in steps of five, so divided by five and if we had num like equal to
100 like we said, then it will be a total of 20, 20 commands, 20 durations of the loop.
So you can see we optimized it, although not very much, but at least a little bit.
We optimized it so that this way we have fifty three commands while fifty three entries of these little
buddy and here we have a hundred of four iterations.
So if you will increase num you will see more of difference and more and more as you just increase the
number.
So yeah guys these are basically the two solutions, the trivial one and the optimal solution.
So just to let you know that you can once again build and run it and see what happens.
And yeah, that's not the main point here to find the numbers, but just to understand the logic and
to understand how everything works behind the scenes and how to make it optimal and optimized.
So thank you guys for watching.
Keep on practicing and we will meet in the next videos.
Adios.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.