Close Menu
    Code and Debug
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram YouTube WhatsApp
    • Home
    • Our Courses
    • Blog
    • About Us
    • Contact Us
    Facebook Instagram YouTube LinkedIn WhatsApp
    Code and Debug – BlogCode and Debug – Blog
    Code and Debug – BlogCode and Debug – Blog
    Home»Data Structures & Algorithms»Sum of first n terms | Using Recursion | Explained
    Data Structures & Algorithms

    Sum of first n terms | Using Recursion | Explained

    codeanddebugBy codeanddebug29 June 2025No Comments2 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Featured image of question to find the sum of first N terms
    Share
    Facebook Twitter LinkedIn Pinterest Email

    The GeeksforGeeks problem “Sum of first n terms” asks you to compute the value of

    1³ + 2³ + 3³ + … + n³

    and return the result. All calculations must be done recursively, no loops allowed.

    Here’s the [Problem Link] to begin with.

    Example
    Input n = 4
    Series 1³ + 2³ + 3³ + 4³ = 1 + 8 + 27 + 64
    Output 100

    A well-known mathematical identity states that

    1³ + 2³ + … + n³ = (n · (n + 1) / 2)²

    …but when recursion is required, we’ll implement the summation directly via a functional approach.

    Contents:
     [hide]
    • Functional Recursion Solution
      • Intuition & Approach
      • Code Implementation
      • Code Explanation
      • Dry Run (n = 3)
      • Time & Space Complexity
      • Conclusion

    Functional Recursion Solution

    Intuition & Approach

    • Base Case: The series stops at 1³ (= 1).
    • Recursive Case: For num > 1, return the current cube num³ plus the result of the same function on num − 1.
    • This drives the recursion downward until it hits the base case, then the partial sums bubble back up the call stack.

    Code Implementation

    class Solution:
        def func(self, num):
            # Base case: the sum of the first 1 term is simply 1³ = 1
            if num == 1:
                return 1
            # Recursive step: add the current cube and recurse on num - 1
            return num**3 + self.func(num - 1)
    
        def sumOfSeries(self, n):
            # Entry point: delegate work to the recursive helper
            return self.func(n)

    Code Explanation

    1. Base Case (num == 1)
      • Returns 1, ending further recursion.
    2. Recursive Step
      • Computes num³.
      • Calls func(num − 1) to sum the remaining cubes.
      • Adds them together and returns upward.
    3. Driver (sumOfSeries) simply kicks off the helper with the original n.

    Dry Run (n = 3)

    Call Stack LevelnumReturned Value
    1327 + func(2)
    228 + func(1)
    3 (base)11

    Unwinding:

    func(1) → 1
    func(2) → 8 + 1 = 9
    func(3) → 27 + 9 = 36

    Final answer: 36 (which matches (1 + 2 + 3)² = 6² = 36).

    Time & Space Complexity

    • Time: O(n) – one function call per integer from n down to 1.
    • Space: O(n) – call-stack depth equals n because Python lacks tail-call optimization.

    Conclusion

    Using functional recursion gives an elegant, one-liner style recurrence:

    Base Case + Self-Call = Solution.

    While not as fast or space-efficient as the closed-form formula, it satisfies the “no loops” constraint and reinforces fundamental recursion concepts, perfect preparation for larger divide-and-conquer problems.

    Join our Advance DSA COURSE

    For any changes to the article, kindly email at code@codeanddebug.in or contact us at +91-9712928220.

    Easy Math Recursion
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePrint N to 1 without loop | Normal Recursion and Backtracking
    Next Article Recursive Bubble Sort in Python
    codeanddebug
    • Website

    Related Posts

    Data Structures & Algorithms

    Rat in a Maze Problem – I | GFG Practice | 2 different solutions

    22 July 2025
    Data Structures & Algorithms

    N-Queens | Leetcode 51 | Brute and Optimal Solution

    22 July 2025
    Data Structures & Algorithms

    Letter Combinations of a Phone Number | Leetcode 17 | Optimal Solution in Python

    22 July 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • Data Structures & Algorithms (154)
      • Beginner (56)
      • Expert (38)
      • Intermediate (60)
    • Uncategorised (1)
    Recent Posts

    Rat in a Maze Problem – I | GFG Practice | 2 different solutions

    22 July 2025

    N-Queens | Leetcode 51 | Brute and Optimal Solution

    22 July 2025

    Letter Combinations of a Phone Number | Leetcode 17 | Optimal Solution in Python

    22 July 2025

    Combination Sum III | Leetcode 216 | Optimal Solution in Python

    22 July 2025

    Subset Sums | GFG Practice | Brute and Optimal Solution

    22 July 2025
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.