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

    Anirudh KhuranaBy Anirudh Khurana29 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
    Anirudh Khurana
    • Website

    Anirudh Khurana is the founder of Code and Debug, an Indian EdTech platform that helps students and working professionals crack coding interviews and move into data and AI roles. He teaches DSA, Python, SQL, data engineering and machine learning through live bootcamps and his YouTube channel, Code and Debug, which has over 50,000 subscribers. Every guide on this blog is written or reviewed by him and tested with real code.

    Related Posts

    Data Structures & Algorithms

    DSA Interview Cheat Sheet: All Patterns You Need to Know

    4 April 2026
    Data Structures & Algorithms

    Symmetric Tree | Leetcode 101 | Recursive DFS Approach

    2 September 2025
    Data Structures & Algorithms

    Binary Tree Right Side View | Leetcode 199 | BFS and DFS Approaches

    2 September 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Categories
    • AI & Machine Learning (4)
    • Data Structures & Algorithms (241)
      • Beginner (82)
      • Expert (52)
      • Intermediate (106)
    • Pandas (1)
    • Python (1)
    • Resume (1)
    • SQL (1)
    Recent Posts

    Confusion Matrix in Machine Learning: An Interactive Guide

    21 October 2026

    100 SQL Interview Questions with Queries and Answers (Freshers to Experienced)

    17 October 2026

    Logistic Regression in Machine Learning: Explained Visually (with Python)

    17 October 2026

    Linear Regression in Machine Learning: Explained Visually (with Python)

    17 October 2026

    100 Python Interview Questions and Answers for Freshers and Experienced (Free PDF)

    17 October 2026
    Facebook Instagram YouTube LinkedIn WhatsApp
    © 2025 Code and Debug. All rights reserved.

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