Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:05,850
All right, so now what do we have to do is to write the recursive function that we'll be able to find
2
00:00:05,850 --> 00:00:09,240
out the sum of all digits in a given number.
3
00:00:09,360 --> 00:00:14,850
And we know that he can be easily done by using some iterative approach.
4
00:00:15,150 --> 00:00:18,330
You simply take a number, let's say two, one, three.
5
00:00:18,660 --> 00:00:24,720
And you know how to sum all of its digits by simply every time taking the right digit.
6
00:00:24,870 --> 00:00:25,970
For example, three.
7
00:00:26,010 --> 00:00:27,390
By using a model.
8
00:00:27,530 --> 00:00:27,800
Right.
9
00:00:28,110 --> 00:00:28,790
And model loop.
10
00:00:29,220 --> 00:00:33,660
And you take these three and you submit to these one.
11
00:00:33,660 --> 00:00:37,890
And then you take these two and you sum everything up and you get your six.
12
00:00:37,950 --> 00:00:38,470
At the end.
13
00:00:38,520 --> 00:00:38,850
OK.
14
00:00:39,360 --> 00:00:44,040
So that will be an Darah iterative approach where you take this number.
15
00:00:44,070 --> 00:00:46,560
Take the model divided by 10.
16
00:00:46,830 --> 00:00:48,450
Take the right digit.
17
00:00:48,720 --> 00:00:50,840
And then divide the whole number by 10.
18
00:00:50,850 --> 00:00:53,860
And you just remain in the second phase with 21.
19
00:00:54,270 --> 00:00:55,350
Then you stay with two.
20
00:00:55,350 --> 00:01:00,450
And every time you take the right digit and you are basically OK.
21
00:01:01,020 --> 00:01:06,690
So now in the recursive approach, we are going to use something very similar.
22
00:01:06,750 --> 00:01:07,030
OK.
23
00:01:07,230 --> 00:01:15,180
So we know that to find out on the sum of digits, let's call it a sum of digits, some of digits of,
24
00:01:15,960 --> 00:01:17,610
let's say two, one, three.
25
00:01:18,240 --> 00:01:19,750
It is simply it.
26
00:01:20,010 --> 00:01:21,630
That's our main problem.
27
00:01:21,660 --> 00:01:21,960
OK.
28
00:01:22,140 --> 00:01:24,960
And we are looking because we are using recursion.
29
00:01:24,960 --> 00:01:28,710
We are looking to find some relation to a sub problem.
30
00:01:28,740 --> 00:01:34,730
So we know that some of digits, four, two, one, three equals two, three.
31
00:01:34,860 --> 00:01:35,240
OK.
32
00:01:35,410 --> 00:01:41,310
Plus some of digits, some of digits for 21.
33
00:01:41,880 --> 00:01:43,800
And the reason for that is very simple.
34
00:01:43,920 --> 00:01:47,240
We are going to take off the right digit.
35
00:01:47,370 --> 00:01:48,330
Win this case.
36
00:01:48,360 --> 00:01:49,410
It's three.
37
00:01:49,710 --> 00:01:52,630
We know that it's three plus some digits for it.
38
00:01:52,650 --> 00:01:53,490
Two and one.
39
00:01:53,490 --> 00:01:59,970
And remove this digit because we also are already took it into our considerations.
40
00:02:00,510 --> 00:02:04,020
We will remove it and call this function in a recursive manner.
41
00:02:04,410 --> 00:02:10,010
And we know that this problem, this sub problem, can be divided even further.
42
00:02:10,050 --> 00:02:12,170
So we will keep these three.
43
00:02:12,200 --> 00:02:15,120
So it's three plus some of digits.
44
00:02:15,230 --> 00:02:17,970
Four twenty one will be equal to one.
45
00:02:18,060 --> 00:02:18,360
Right.
46
00:02:18,450 --> 00:02:21,210
The right digit plus some of these.
47
00:02:21,210 --> 00:02:28,950
Just some of digits for two because two is what remains if we divide twenty one by ten.
48
00:02:29,100 --> 00:02:36,030
And we know that ah that's kind of a trivial solution because some of digits when you have just one
49
00:02:36,030 --> 00:02:42,090
digit is just the digit itself, whether it's one, two, three, four or five or whatever, it will
50
00:02:42,090 --> 00:02:43,710
be the digit itself.
51
00:02:43,890 --> 00:02:48,030
So that will be used for our stopping condition, for our base case.
52
00:02:48,270 --> 00:02:55,380
And we can see that we were able to take a bigger problem, a bigger problem, and to find some rules.
53
00:02:55,440 --> 00:03:00,420
Some are regular things that we can do to divide it into sub problems.
54
00:03:00,450 --> 00:03:08,790
So we just extract, extract the right digit and then we call these function recursive call of.
55
00:03:09,180 --> 00:03:11,730
These given num divided by 10.
56
00:03:11,790 --> 00:03:15,600
So two one two one three divided by ten is just two one.
57
00:03:15,720 --> 00:03:18,980
OK, so two hundred and thirteen and we get twenty one.
58
00:03:19,680 --> 00:03:20,700
So let's break.
59
00:03:20,730 --> 00:03:22,900
Now let's see how we create a function for it.
60
00:03:23,670 --> 00:03:27,840
And these function is going to be aimed some of some of digits.
61
00:03:27,890 --> 00:03:30,030
And it will return some number.
62
00:03:30,450 --> 00:03:30,780
OK.
63
00:03:31,290 --> 00:03:36,670
And what we are going to do here is, first of all, we need to specify the stopping condition.
64
00:03:36,900 --> 00:03:42,510
So in this example, we said that the stuffing condition, the base case is going to be when we have
65
00:03:42,510 --> 00:03:44,280
just one digit.
66
00:03:44,400 --> 00:03:48,380
And since we are working with integers, it's going to be very simple.
67
00:03:48,390 --> 00:03:54,600
We just need to specify as long as if NUM is less or equal to nine.
68
00:03:55,350 --> 00:03:58,650
OK, meaning it's less or equal to nine.
69
00:03:58,710 --> 00:04:00,840
It can be nine, eight, seven.
70
00:04:01,200 --> 00:04:02,340
It can be zero.
71
00:04:02,880 --> 00:04:07,440
So if that's the case, we are going to return to return the number itself.
72
00:04:07,710 --> 00:04:08,000
OK.
73
00:04:08,370 --> 00:04:09,870
So it's trivial.
74
00:04:09,930 --> 00:04:11,790
We can find the solution right away.
75
00:04:11,790 --> 00:04:16,020
We don't need to make any division to solve problems and so on.
76
00:04:16,200 --> 00:04:16,620
OK.
77
00:04:16,680 --> 00:04:19,920
So what will be what will be the recursive call?
78
00:04:19,950 --> 00:04:26,840
What will be the recursive call we've seen here in this example that we first of all extract the right
79
00:04:26,850 --> 00:04:27,750
digit right.
80
00:04:27,750 --> 00:04:31,950
We extract the right digit of a given number.
81
00:04:32,160 --> 00:04:35,280
So that can be done by using a model of 10.
82
00:04:35,610 --> 00:04:39,470
So return num modulo 10.
83
00:04:39,900 --> 00:04:40,210
OK.
84
00:04:40,380 --> 00:04:42,030
It's the right digit.
85
00:04:42,120 --> 00:04:42,960
The right one.
86
00:04:43,290 --> 00:04:44,430
Three in this case.
87
00:04:44,790 --> 00:04:46,380
And one in this case.
88
00:04:46,740 --> 00:04:47,340
Plus.
89
00:04:47,430 --> 00:04:48,210
Plus what.
90
00:04:48,600 --> 00:04:50,070
Last sum of digits.
91
00:04:50,130 --> 00:04:51,180
Sum of digits.
92
00:04:51,360 --> 00:04:55,230
Four for these twenty one which is.
93
00:04:55,620 --> 00:04:58,740
Are these given number divided by ten.
94
00:04:58,770 --> 00:04:59,820
Because we don't.
95
00:04:59,890 --> 00:05:03,490
Want to include these three in the freezer?
96
00:05:03,670 --> 00:05:10,220
Further south problem, because we already took it into account here, so it will look like it is numb,
97
00:05:10,330 --> 00:05:11,770
divided by 10.
98
00:05:11,940 --> 00:05:12,330
OK.
99
00:05:12,460 --> 00:05:16,660
And basically, basically, we can say that that that's it.
100
00:05:17,230 --> 00:05:20,410
So let's write some simple program to test it out.
101
00:05:21,160 --> 00:05:32,740
So ain't no arm and train have enter insert to insert a number, then the user specifies the scarf while
102
00:05:32,740 --> 00:05:34,210
I'm typing really fast today.
103
00:05:34,960 --> 00:05:35,640
Now up.
104
00:05:36,160 --> 00:05:37,090
Here's the mistake.
105
00:05:37,180 --> 00:05:39,330
And now we are going to call these functions.
106
00:05:39,340 --> 00:05:40,140
So print out.
107
00:05:40,200 --> 00:05:41,980
We are going to print out right away.
108
00:05:41,980 --> 00:05:45,120
We are not going to create another variable to hold the result.
109
00:05:45,490 --> 00:05:47,350
We are going to create print.
110
00:05:47,490 --> 00:05:51,390
There are some of digits for percentage.
111
00:05:51,500 --> 00:05:54,370
They equals two percentage D..
112
00:05:54,510 --> 00:05:54,770
OK.
113
00:05:55,330 --> 00:06:00,850
And instead of these first place holder, we are going to use NARM, which is the number itself.
114
00:06:00,910 --> 00:06:03,910
And here we are going to use some of digits.
115
00:06:04,000 --> 00:06:04,280
OK.
116
00:06:04,330 --> 00:06:07,770
Make these function call salved digits for now.
117
00:06:08,290 --> 00:06:14,470
So instead of these percentage they will have Nahm so some of digits for a num for a given number.
118
00:06:14,490 --> 00:06:18,930
Let's say two one three are will be equal to percentage Judy.
119
00:06:18,970 --> 00:06:20,260
To these result.
120
00:06:20,650 --> 00:06:23,650
That will be instead of these function ones.
121
00:06:23,710 --> 00:06:26,760
It's on over with the execution.
122
00:06:26,890 --> 00:06:30,880
So let's try to build and run it and see what happens if everything OK.
123
00:06:30,940 --> 00:06:34,150
And we didn't mess up anything are in the process.
124
00:06:34,240 --> 00:06:36,430
So OK, so here's the problem.
125
00:06:36,940 --> 00:06:37,220
OK.
126
00:06:37,330 --> 00:06:39,040
So that's just a comment.
127
00:06:39,700 --> 00:06:44,260
Don't be so mad and now build and run and enter.
128
00:06:44,290 --> 00:06:45,200
Insert the number.
129
00:06:45,270 --> 00:06:46,690
It's go with this example.
130
00:06:46,690 --> 00:06:49,530
And we know that we expect to receive six at the end of it.
131
00:06:49,540 --> 00:06:49,780
Right.
132
00:06:49,810 --> 00:06:51,220
Two plus one plus three.
133
00:06:51,460 --> 00:06:55,080
So some of digits, four to one three equals two six.
134
00:06:55,720 --> 00:06:56,410
Fantastic.
135
00:06:56,800 --> 00:07:02,890
We can use some another example, let's say nine nine nine one zero five.
136
00:07:02,950 --> 00:07:04,690
So we just sum it up.
137
00:07:04,780 --> 00:07:06,790
It's 27, 28.
138
00:07:07,510 --> 00:07:09,400
It's going to be thirty three.
139
00:07:09,410 --> 00:07:09,730
Right.
140
00:07:10,000 --> 00:07:11,290
So let's print.
141
00:07:11,350 --> 00:07:11,800
Enter.
142
00:07:11,830 --> 00:07:13,090
And there you go.
143
00:07:13,110 --> 00:07:19,480
You can find your sum of digits for any positive number that you may insert it.
144
00:07:19,570 --> 00:07:22,510
Of course, as long as it does not exceed the integer range.
145
00:07:22,660 --> 00:07:25,920
So that's basically it for this video, for this challenge.
146
00:07:25,930 --> 00:07:32,470
I hope you try this one on your own and you probably got it right because there is nothing special here.
147
00:07:32,470 --> 00:07:40,510
We talked about how to find how to access digits in a given number in one or two or four previous challenges,
148
00:07:40,790 --> 00:07:43,480
not in this section, but in the previous sections.
149
00:07:43,660 --> 00:07:45,100
And there is nothing new here.
150
00:07:45,100 --> 00:07:47,370
We just users use the base case.
151
00:07:47,380 --> 00:07:54,970
We used some recursive call and we find found out some rules for the use on their vision to solve problems.
152
00:07:55,760 --> 00:07:57,340
So thank you guys for watching.
153
00:07:57,400 --> 00:07:59,870
I hope this material is clear to you.
154
00:07:59,890 --> 00:08:05,860
And in the next video, in the next challenge, we are going to look for also something similar to some
155
00:08:05,860 --> 00:08:06,580
of digits.
156
00:08:06,640 --> 00:08:08,150
But let's go there.
12568
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.