Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write sum_to(n) that adds by recursion. What are sum_to(5) and sum_to(100)?
n + the sum of the smaller problem.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Base case. sum_to(0) is 0.
Recursive case. sum_to(n) is n + sum_to(n - 1).
sum_to(5) = 5 + 4 + 3 + 2 + 1 = 15.sum_to(100) = 5050. (Check: . ✓)Log in to join the discussion.
Loading…
With Standard, you also open 10 more problems, 2 Python labs and 1 extra course. Everything you have used here stays free, always.