Knowledge Guide
HomeDSARecursion

Solution Basic Sum

Problem Statement

Calculate the Sum of the First N Natural Numbers Using a Recursive Approach.

The sum of first N natural numbers is equal to N + (N-1) + (N-2) + ... + (3) + (2) + (1). The following table shows a sample input/output description table:

Input (s)Output (s)Explanation
N = 5Sum = 15The first 5 natural numbers are 1, 2, 3, 4, and 5. The sum of these numbers is 1 + 2 + 3 + 4 + 5 = 15.
N = 10Sum = 55The first 10 natural numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. The sum of these numbers is 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55.
N = 1Sum = 1The first natural number is 1. The sum of this number is 1.

Solution

The algorithm to calculate the sum of the first N natural numbers using recursion can be defined as follows:

  1. If N is equal to 0 or less, return 0 (base case).
  2. Otherwise, return the sum of number N and the recursive call to calculate the sum of N-1.

The recursive approach works by breaking down the problem into smaller subproblems. We calculate the sum of N by adding N to the sum of the numbers from 1 to N-1. This process continues until the base case is reached, where N is 0 or less.

The base case stops the recursion and returns 0. By summing the numbers in a decreasing order, we ensure that each number is added to the previous sum, eventually giving us the total sum of the first N natural numbers.

Example dry run to calculate Sum of first 5 Natural numbers
Example dry run to calculate Sum of first 5 Natural numbers

Code

Here is the code for this algorithm:

java
public class Solution {

  public static int calculateSum(int N) {
    if (N <= 0) {
      return 0; // Base case
    }
    return N + calculateSum(N - 1); // Recursive call
  }

  public static void main(String[] args) {
    // Example inputs
    int[] examples = { 5, 10, 1 };

    for (int N : examples) {
      int sum = calculateSum(N);
      System.out.println("Sum of first " + N + " natural numbers: " + sum);
    }
  }
}

Time and Space Complexity

The time complexity of the algorithm is since we make N recursive calls, where N is the input natural number.

The space complexity of the algorithm is also because the maximum depth of the recursion is N. Each recursive call adds a new stack frame, consuming additional space on the call stack.

🤖 Don't fully get this? Learn it with Claude

Stuck on Solution Basic Sum? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Solution Basic Sum** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Solution Basic Sum** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Solution Basic Sum**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Solution Basic Sum**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes