Six problems where one recursive call does the job. Most could also be loops — the point is to solve them recursively, answering the four questions first: what the function means, its base case, how it gets smaller, and how the smaller answer is used.
- Write the base case first, then the recursive line.
- The answer can be big: 20! and the steps of the "strange algorithm" don't fit
an
int— uselong long. - For the greatest common divisor, the inputs can be negative: work with their absolute values.
- Sum to n goes up to 100 000 calls deep. If it crashes in Code::Blocks but the logic is right, give your program a bigger stack — the Call Stack lesson shows how.