HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClassesGroups

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Blog

Account

  • Dashboard
  • My Content
  • Profile
  • Log in
© 2026 senLearn. All rights reserved.HelpAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Problems
  2. /Write sum_to(n) that adds 1 + 2 + … + n by recursion. What are sum_to(5) and sum_to(100)?
EasyPython ProgrammingHigh School (9–10)High School (11–12)UndergraduateGeneralPython Essentials › RecursionNumericalPython-CodedA-LevelIBPCEP (Python Institute)

Write sum_to(n) that adds 1 + 2 + … + n by recursion. What are sum_to(5) and sum_to(100)?

StemPy LabContributed by StemPy Lab

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 1+2+⋯+n1 + 2 + \cdots + n1+2+⋯+n by recursion. What are sum_to(5) and sum_to(100)?

Need a reminder? The key ideaShowHide

n + the sum of the smaller problem.

The worked solution

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: 100×1012=5050\frac{100 \times 101}{2} = 50502100×101​=5050. ✓)
Log in to save this for later
← All Problems
← Write a recursive function countdown(n) that shows n, n-1, …, 1 and then Go!Write a recursive factorial(n). What are factorial(5) and factorial(10)? →
More onPython Essentials › Recursion

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Factorials and the call stackA Function That Calls Itself

More on this topic

  1. A staircase has n steps. You climb one step or two steps at a time…Medium
  2. Without running anything, say how many subsets there are for lists of 5, 10 and 20 items…Medium
  3. For each function, say whether a memory (a dictionary of answers) would help, and why…Easy
  4. Write a recursive function all_positive(items) that gives True if every item of the list is more…Medium
For each job, say whether a loop or recursion is the better choice, and why…Easy
All problems on this topic →

Want to go further in Python Essentials › Recursion?

With Standard, you also open 10 more problems, 2 Python labs and 1 extra course. Everything you have used here stays free, always.

Part of our upcoming paid plans.