The recursive principle is this: given a list of values, if I can do something to the
first item in the list in relation to the
rest of the list, I can do it to the entire list.
So, given a number like 74, you know that we can split it up (into a list of digits) using the remainder and division operators.
74 % 10 --> 4
74 / 10 --> 7 |
Pause to think: 4 is the
first item in the list, and 7 is the
rest of the list.
The largest of 4 and 7 is 7, so the answer is 7.
Let's extend: 374. Thinking a moment, our list of digits will be 4, 7, 3. We can figure out what the largest of
two digits, but we can't do three, unless we do this:
Largest( 4, Largest( 7, 3 ) ) |
That's the trick in the algorithm. Find the largest of the first item in the list and the largest of the rest of the list. It works for numbers of any size, like 9374:
Largest( 4, Largest( 7, Largest( 3, 9 ) ) ) |
Now that we are thinking recursively, the trick is to know when to stop. Thinking again, we can say "That's easy, we stop when there is no more list!"
And that is the case. When the number becomes zero, there is no more list. Conveniently, the largest digit in the number 0 is 0, so any other digits (if there were any, we don't know) will be larger.
I hope this helps clear things up.