Linear Recursion: Factorial and Sum

Intermediate Algorithms and Data Structures English
Also available: Deutsch
Created by Best · 16.08.2026 at 09:13 UTC

Linear Recursion: each call generates at most one further call. Factorial: n! = 1 for n <= 1, otherwise n * (n-1)!. Sum 1..n is analogous.

The call depth is $\Theta(n)$. The work is $\Theta(n)$ if constant work is done per frame.

Iterative variants avoid stack growth and are often more robust in Java.

Linear recurrence as stairs: Climbing Stairs [1].

Where used

Teaching form for induction and for reading specifications. In production, linear chains are often replaced by loops; the recursive form remains visible in proofs, in functional languages, and in code generator output.

Depth

Linear recursion generates at most one further recursive call per activation. The number of active levels corresponds, in factorial or prefix sum computations, to the size of the input that has yet to be processed.

The factorial definition combines the smaller result upon returning with the current factor. A sum function over an array can similarly use the index or remaining length as a progress measure. Both require linear time and linear call storage.

Numerical limits remain independent of the recursion form. int and long overflow even with moderate factorial arguments; for exact large values, BigInteger is necessary.

Difficulty levels

  1. Track the call and return phase of linear recursion.
  2. Derive time and space requirements from the progress measure.
  3. Separate recursion logic from numerical overflow.

Pitfalls

A correct termination does not protect against arithmetic overflow. Often, only the number of calls is considered, while the concurrently used call storage is forgotten.


Sources

University approvals: 0
Tasks
Question 1

Implement fact(n) recursively for n >= 0 (fact(0)=fact(1)=1).

Hint

A recursive call in the same class is simply written as fact(...). Integer literals can be written as long using the suffix L.

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Question 2

How many recursive calls (without the first one) does fact(4) make in the classical definition?

Question 3

Which resource typically grows with n in a linear recursion over n elements?

Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Intermediate
  • Completed: 0 users
Creator
Best
Best
BestBuddy