Afrikaans
Akan
Albanian
Amharic
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
Hungarian
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
I really like the problem that we're going to work on in this video, it's the fact that digit some
problem and the reason why I like it is because I think it's a perfect one to practice a lot of the
things that we've learned in the course.
So after we read this, definitely try to pause the video and try to build it yourself before we go
through the solution, because I think that you'll see that pretty much everything that we've gone through
in the course, there's nothing new in it.
So you have all the knowledge you need in order to build this.
Don't worry if you can't do it, because it's still very challenging, but I definitely recommend it
because it'll help you learn it even better.
Even if you fail doing it, you'll remember the solution better.
So what is a factorial factorial is they give you the algebraic answer up here.
Here's the example.
Factorial of ten is ten times nine times, eight times seven all the way down.
Factorial of ten is massive just by itself.
It's three million six hundred twenty eight thousand eight hundred.
So as you can see in pectorals grow very, very quickly and the sum of the digits in ten factorial is
27.
And so the way that you can count that is it's just literally the sum of the digits, three six to eight
zero and zero.
So that equals twenty seven.
So what we need to do is find the sum of the digits in the number 100 factorial.
So as you can see, we have to find the factorial, then we have to split it up, get all the digits,
atom together and that's how we'll get the sum.
So let's start this off by creating a factorial method.
So factorial value some generator.
That's a nice long name.
And we're going to pass in factorial as the argument, OK, the first thing we're going to do is to
set up an array.
And so in this array, we want it to range from one to the value of the argument passed into the function.
So in this case, I'm going to do one dot dot factorial and then want to cast this and convert it into
an array.
And now we because if we're matching what we saw over here, this needs to be in reverse.
So how do we reverse this?
We just called the reverse method on.
And so when we call the reverse method on it, if this is 10 factorial, then what it's going to do
is pass in ten and then multiply it ten times eventually when we have this part in, but is going to
do it backwards.
So if we pass in ten is factorial, it's immediately going to create an array that looks like of this.
But that won't work when we do our factorial method on it.
So we need to split this up.
So our array looks like this.
Does that make sense?
That's that's a little bit of just a tricky way of being able to get at you swap out or you reverse
the order so that you can be able to call the factorial on it.
So now that we have that where it's an array so we can call each on it and iterate over, I'm going
to create an iterator variable called I and pass End of the Block.
And now I'm going to call I'm going to bring the factorial method or argument in, because here, remember,
we want to grab this some.
So I'm going to increment it by this value.
And so this value is going to be factorial times, ie, minus one.
OK, now don't worry, if you don't get that, let's just hold on one second and go through it again.
So let's pick up where we left off right here.
So I got one through 10.
We reversed it.
So it's going to be assuming factorial.
The argument is 10, we reversed it.
So it's going to be 10 through one.
And now we're iterating over that new array.
So let's just for visual sake, say that we have our red dot all the way to one.
OK, so we're iterating over it.
And so what we're doing is we're taking the value of factorial right here.
And so we're going to say this is what starts off with.
So in this case, a B, 10, and then we're going to add the product of factorial, which is also ten
times IE minus one.
So in this case, I is going to be 10 minus one, which is going to be nine.
So this is going to be ten there.
That's going to be the sum of ten plus ten times nine, nine.
So it's going to iterate through that all the way through.
So hopefully that makes sense.
I think it's a good way of writing the factorial method.
So now that we have that, that's just a factorial now we actually have to create the the sum of the
digits.
So I'm going to say factorial to string and then split this.
And so what this is going to do is simply split it up so that each one of the digits is separated.
And now I'm going to call the map method.
So on the map method, we're going to pass it in a block and call the two integer method on it.
And then we're simply going to call inject so that we can add all of this up and that is it.
So let's now call our method pass in one 100, because that's what we need for this one.
And let's see if this works.
Ruby Project Guler, this is problem 20.
And 648 is the solution.
So great job, this is a this is one that I thought was perfect for this course because it makes you
use a lot of the things we've learned, like being able to work with blocks, being able to call methods
on arrays, chain methods together, use methods like inject and map in conjunction with each other
to be able to build a pretty, pretty simple, elegant and simplified solution to a complex problem.
So nice work.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.