Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:09,240 --> 00:00:16,290
I really like the problem that we're going to work on in this video, it's the fact that digit some
2
00:00:16,290 --> 00:00:22,950
problem and the reason why I like it is because I think it's a perfect one to practice a lot of the
3
00:00:22,950 --> 00:00:24,650
things that we've learned in the course.
4
00:00:24,650 --> 00:00:32,400
So after we read this, definitely try to pause the video and try to build it yourself before we go
5
00:00:32,400 --> 00:00:39,900
through the solution, because I think that you'll see that pretty much everything that we've gone through
6
00:00:39,900 --> 00:00:42,800
in the course, there's nothing new in it.
7
00:00:42,810 --> 00:00:45,680
So you have all the knowledge you need in order to build this.
8
00:00:45,690 --> 00:00:51,330
Don't worry if you can't do it, because it's still very challenging, but I definitely recommend it
9
00:00:51,330 --> 00:00:54,030
because it'll help you learn it even better.
10
00:00:54,030 --> 00:00:57,730
Even if you fail doing it, you'll remember the solution better.
11
00:00:57,780 --> 00:01:06,270
So what is a factorial factorial is they give you the algebraic answer up here.
12
00:01:06,270 --> 00:01:07,380
Here's the example.
13
00:01:07,980 --> 00:01:13,800
Factorial of ten is ten times nine times, eight times seven all the way down.
14
00:01:14,010 --> 00:01:17,580
Factorial of ten is massive just by itself.
15
00:01:17,580 --> 00:01:21,280
It's three million six hundred twenty eight thousand eight hundred.
16
00:01:21,610 --> 00:01:32,910
So as you can see in pectorals grow very, very quickly and the sum of the digits in ten factorial is
17
00:01:32,910 --> 00:01:34,020
27.
18
00:01:34,290 --> 00:01:42,870
And so the way that you can count that is it's just literally the sum of the digits, three six to eight
19
00:01:42,870 --> 00:01:43,950
zero and zero.
20
00:01:43,950 --> 00:01:45,300
So that equals twenty seven.
21
00:01:45,660 --> 00:01:52,830
So what we need to do is find the sum of the digits in the number 100 factorial.
22
00:01:52,860 --> 00:02:01,500
So as you can see, we have to find the factorial, then we have to split it up, get all the digits,
23
00:02:01,770 --> 00:02:04,190
atom together and that's how we'll get the sum.
24
00:02:04,200 --> 00:02:10,020
So let's start this off by creating a factorial method.
25
00:02:10,020 --> 00:02:16,390
So factorial value some generator.
26
00:02:16,420 --> 00:02:17,490
That's a nice long name.
27
00:02:17,790 --> 00:02:25,890
And we're going to pass in factorial as the argument, OK, the first thing we're going to do is to
28
00:02:25,890 --> 00:02:27,270
set up an array.
29
00:02:27,300 --> 00:02:35,890
And so in this array, we want it to range from one to the value of the argument passed into the function.
30
00:02:35,910 --> 00:02:44,430
So in this case, I'm going to do one dot dot factorial and then want to cast this and convert it into
31
00:02:44,430 --> 00:02:44,970
an array.
32
00:02:45,330 --> 00:02:52,960
And now we because if we're matching what we saw over here, this needs to be in reverse.
33
00:02:52,980 --> 00:02:54,610
So how do we reverse this?
34
00:02:54,630 --> 00:02:56,390
We just called the reverse method on.
35
00:02:56,400 --> 00:03:04,020
And so when we call the reverse method on it, if this is 10 factorial, then what it's going to do
36
00:03:04,830 --> 00:03:11,490
is pass in ten and then multiply it ten times eventually when we have this part in, but is going to
37
00:03:11,490 --> 00:03:12,450
do it backwards.
38
00:03:12,450 --> 00:03:20,400
So if we pass in ten is factorial, it's immediately going to create an array that looks like of this.
39
00:03:20,820 --> 00:03:25,860
But that won't work when we do our factorial method on it.
40
00:03:25,860 --> 00:03:27,810
So we need to split this up.
41
00:03:28,080 --> 00:03:32,280
So our array looks like this.
42
00:03:34,660 --> 00:03:35,710
Does that make sense?
43
00:03:35,740 --> 00:03:42,460
That's that's a little bit of just a tricky way of being able to get at you swap out or you reverse
44
00:03:42,460 --> 00:03:47,550
the order so that you can be able to call the factorial on it.
45
00:03:47,560 --> 00:03:53,080
So now that we have that where it's an array so we can call each on it and iterate over, I'm going
46
00:03:53,080 --> 00:03:58,060
to create an iterator variable called I and pass End of the Block.
47
00:03:58,330 --> 00:04:07,300
And now I'm going to call I'm going to bring the factorial method or argument in, because here, remember,
48
00:04:07,300 --> 00:04:09,820
we want to grab this some.
49
00:04:10,120 --> 00:04:13,610
So I'm going to increment it by this value.
50
00:04:13,630 --> 00:04:23,470
And so this value is going to be factorial times, ie, minus one.
51
00:04:24,760 --> 00:04:32,190
OK, now don't worry, if you don't get that, let's just hold on one second and go through it again.
52
00:04:32,200 --> 00:04:35,580
So let's pick up where we left off right here.
53
00:04:35,830 --> 00:04:38,980
So I got one through 10.
54
00:04:38,980 --> 00:04:39,880
We reversed it.
55
00:04:39,880 --> 00:04:42,220
So it's going to be assuming factorial.
56
00:04:42,220 --> 00:04:45,210
The argument is 10, we reversed it.
57
00:04:45,220 --> 00:04:47,320
So it's going to be 10 through one.
58
00:04:47,620 --> 00:04:50,540
And now we're iterating over that new array.
59
00:04:50,560 --> 00:04:57,850
So let's just for visual sake, say that we have our red dot all the way to one.
60
00:04:58,150 --> 00:05:00,150
OK, so we're iterating over it.
61
00:05:00,160 --> 00:05:04,530
And so what we're doing is we're taking the value of factorial right here.
62
00:05:04,810 --> 00:05:07,490
And so we're going to say this is what starts off with.
63
00:05:07,510 --> 00:05:14,050
So in this case, a B, 10, and then we're going to add the product of factorial, which is also ten
64
00:05:14,260 --> 00:05:17,350
times IE minus one.
65
00:05:17,380 --> 00:05:24,120
So in this case, I is going to be 10 minus one, which is going to be nine.
66
00:05:24,130 --> 00:05:26,720
So this is going to be ten there.
67
00:05:26,740 --> 00:05:32,840
That's going to be the sum of ten plus ten times nine, nine.
68
00:05:32,890 --> 00:05:36,890
So it's going to iterate through that all the way through.
69
00:05:36,940 --> 00:05:38,020
So hopefully that makes sense.
70
00:05:38,020 --> 00:05:42,600
I think it's a good way of writing the factorial method.
71
00:05:42,850 --> 00:05:50,800
So now that we have that, that's just a factorial now we actually have to create the the sum of the
72
00:05:50,800 --> 00:05:51,350
digits.
73
00:05:51,370 --> 00:05:57,120
So I'm going to say factorial to string and then split this.
74
00:05:57,520 --> 00:06:04,020
And so what this is going to do is simply split it up so that each one of the digits is separated.
75
00:06:04,360 --> 00:06:06,750
And now I'm going to call the map method.
76
00:06:06,910 --> 00:06:15,520
So on the map method, we're going to pass it in a block and call the two integer method on it.
77
00:06:16,030 --> 00:06:23,200
And then we're simply going to call inject so that we can add all of this up and that is it.
78
00:06:23,320 --> 00:06:30,730
So let's now call our method pass in one 100, because that's what we need for this one.
79
00:06:30,980 --> 00:06:33,220
And let's see if this works.
80
00:06:33,430 --> 00:06:37,270
Ruby Project Guler, this is problem 20.
81
00:06:38,190 --> 00:06:41,010
And 648 is the solution.
82
00:06:41,040 --> 00:06:49,320
So great job, this is a this is one that I thought was perfect for this course because it makes you
83
00:06:49,320 --> 00:06:55,710
use a lot of the things we've learned, like being able to work with blocks, being able to call methods
84
00:06:55,710 --> 00:07:03,240
on arrays, chain methods together, use methods like inject and map in conjunction with each other
85
00:07:03,240 --> 00:07:10,980
to be able to build a pretty, pretty simple, elegant and simplified solution to a complex problem.
86
00:07:11,010 --> 00:07:11,760
So nice work.
8793
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.