All language subtitles for 12. Print Sum of Values Divisible by 3 AND 5 - Trivial & Optimized Solutions - Solut

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

All right, so let's talk about the solution for this exercise, so we are looking for let's first of

all, just receive the value of an arm.

So in between Jeff and her and her, OK?

And basically what we are going to do is to read this value into storage inside variable naoum.

Nothing special so far.

Right.

And now what do we have to do is simply to let's start with the trivial solution.

So let's create additional variable AI and use a for loop for for that.

OK, so this will be, let's call it a trivial solution.

OK, the basic one and probably not the most optimal.

So let's use here for AI equals to one as long as I use less than or equal to AI plus plus.

So inside of these for a loop we are going to test two conditions.

So we are looking for a numbers that can be divided both by three and by five.

So this condition will look like this.

If I can be divided by three without a remainder and I can be divided by five without the remainder,

then in this case what we will do is just not to bring the result.

We should also sum up all the variables.

So let's create additional variable sum instead of value of zero.

So some will be equal to the previous sum which we had so far, plus the value of AI.

And finally, we should print all the sum like this.

So sum equals to percentage and of the value of some awesome.

So some awesome, amazing.

Let's just build and run it and see what happens.

So enter it's 300.

So basically the sum will be three hundred and fifteen.

And if we want to know also what are the values that are included in the sum.

So let's simply copy that here.

So and also preened the value that we found, let's say percentage D can be divided by both three and

five.

OK.

Let's listen in here.

Instead of the percentage of pool is the value of AI awesome.

So let's build and run, it's just, you know, what is part of our of our final sound system, so international,

it's a hundred.

So 15 can be divided by three and five.

Thirty, forty five.

Sixty, seventy five and ninety, which seemed pretty legit.

And that's the final sum.

OK, so the solution for this exercise actually works and that's OK.

But that's, as we said, the trivial solution and not the optimal one.

What we would like to do is let's take another look at this program.

Let's say we had, I know, up to a thousand.

So we can see here a lot of numbers that can be divided by both three and five.

Fifteen, thirty, forty five.

Sixty seventy five.

Ninety one hundred and five 120.

And what I want you to stoop to, to do is to take a close look, a close look at the numbers that you

can see here, which are part of the sum.

And there is some rule, some similarity between them.

And the similarity is very simple.

You can see that we started from 15, right.

Because 15 is the first number from one up to 15 that can be divided by both three and five.

And then we increment it again by 15.

So 30 is 15 and 15, multiplied by two, multiplied by three, multiplied by four by five.

So every, let's say, iteration that we can use it.

Every time that we take a number, we simply take the previous one and add 15 to to it in order to get

its value that can be printed by both three and five.

So in this case, if we are smart programmers.

Right.

And we are, I suggest to do the following so we know every time we take the value of 15, we can simply

added to the sum.

So this condition of checking all the numbers from one up to number and if you have now, I don't know,

like a million, OK, what is it, a million.

So this will be a lot of numbers.

Right.

And that's OK.

But the problem is that why do we need so much time to check the result?

OK, well, why do we need it?

So what I want to do us for us, first of all, let's say this is the trivial solution and let's go

with the optimized solution.

So optimized solution and not what I want us to do is just to remove these conditions because it's not

necessary and also remove this one and also remove that one, leave just the sum and the values that

can be divided by both three and five and simply start this little bit from I equals to 15 as long as

I is less than or equal to NUM.

And what we will do here is not increment the value of I by one, but rather we know that every time

that we will incremented by 15, the result will be divided by both three and five.

So what we will do is I equals to I lost fifteen.

OK, so that's basically what we will do at the end of every iteration.

All right.

So this will be the optimized solution.

Let's go like this.

Let's leave the sum here.

Uncommon.

OK, uncommon.

It will come and eat out and.

Yeah, OK, so let's print pre-emptive trivial solution.

This will be the trivial solution numbers.

And I will also use here once we simply print it out and found out all the numbers which are part of

the trivial solution, but simply print the optimal optimal solution.

OK, numbers.

So let's build an and on it.

Let's go up to up to hundred.

OK, shall we.

So let's see.

Come here.

So one hundred.

So fifteen thirty.

OK, so these are the numbers under the trivial solution and the optimal solution gives you the same

numbers.

OK, so the sum is going also to be the same.

But what we've done here and what we've added to this kind of solution is simply optimizing it and removing

the check of the need to check both of these conditions.

So every time for every I you you use this condition.

And then if this condition was true, you would also check this condition.

So that's something we do not do here.

We also do not we eliminate the need to check all the numbers between one up to 15, OK, not included.

And then from sixteen up to thirty.

So that's a hell.

Lot of less of.

Operation that we do so basically here from Wynnum equals to a hundred, we just use the instrumentation

about six or seven times.

OK, so I changes just six or seven times while here he changes about a hundred times.

Right.

I equals to one I plus plus up until a hundred or a hundred one.

Hey.

Yeah.

So that's the optimal solution.

The optimal way.

I hope you succeeded to solve this exercise on your own.

If not, please close this exercise right.

The notes down, open up your idea and solve this exercise on your own from scratch and make sure that

everything works exactly as you expected for you.

Because once again, guys, it's great that I'm solving this exercise for you.

But if you want to practice and become a better programmer and to like to have memorized all the answers

and all the solutions and all the nuances that we talk about here, that's very important to solve these

exercises on your own.

So thank you so much for watching.

My name is Vlad and I will see you in the next video.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.