OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Runtime Error Hangup(SIGHUP) for recursion solution to sum of series problem

  • Thread starter Thread starter Kartikey Joshi
  • Start date Start date
K

Kartikey Joshi

Guest

Problem:​


Given an int N, calculate the sum of series 13+23+33+43...... till the nth term using recursion (Link to problem)

Implemented Solution in Python:​


Code:
#User function Template for python3

class Solution:
    def sumOfSeries(self,n):
        #code here
        if n>0:
            return (n*n*n) + self.sumOfSeries(n-1)
        else:
            return 0
            
#{ 
 # Driver Code Starts
#Initial Template for Python 3

if __name__=='__main__':
    t=int(input())
    for _ in range(t):
        N=int(input())
        ob=Solution()
        print(ob.sumOfSeries(N)) 
# } Driver Code Ends

The solution works for small inputs but is giving the above error of large values like 18468

I am unable understand the meaning of the error and what is causing it

I attempted to solve the same problem using recursion in C++ and it is not presenting any error

C++ Solution:​


Code:
//{ Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
// User function template for C++

class Solution {
  public:
    long long sumOfSeries(long long n) {
        // code here
        if (n==1) 
            return 1;
        else
            return (n*n*n)+sumOfSeries(n-1);
    }
};

//{ Driver Code Starts.
int main() {
    int t;
    cin >> t;
    while (t--) {
        long long N;
        cin >> N;
        Solution ob;
        cout << ob.sumOfSeries(N) << "\n";
    }
}
// } Driver Code Ends
<h3>Problem:</h3>
<p>Given an int N, calculate the sum of series 1<sup>3</sup>+2<sup>3</sup>+3<sup>3</sup>+4<sup>3</sup>...... till the nth term using recursion (<a href="https://practice.geeksforgeeks.org/problems/sum-of-first-n-terms5843/1" rel="nofollow noreferrer">Link to problem</a>)</p>
<h3>Implemented Solution in Python:</h3>
<pre><code>#User function Template for python3

class Solution:
def sumOfSeries(self,n):
#code here
if n>0:
return (n*n*n) + self.sumOfSeries(n-1)
else:
return 0

#{
# Driver Code Starts
#Initial Template for Python 3

if __name__=='__main__':
t=int(input())
for _ in range(t):
N=int(input())
ob=Solution()
print(ob.sumOfSeries(N))
# } Driver Code Ends
</code></pre>
<p>The solution works for small inputs but is giving the above error of large values like 18468</p>
<p>I am unable understand the meaning of the error and what is causing it</p>
<p>I attempted to solve the same problem using recursion in C++ and it is not presenting any error</p>
<h3>C++ Solution:</h3>
<pre><code>//{ Driver Code Starts
// Initial template for C++
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
// User function template for C++

class Solution {
public:
long long sumOfSeries(long long n) {
// code here
if (n==1)
return 1;
else
return (n*n*n)+sumOfSeries(n-1);
}
};

//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
long long N;
cin >> N;
Solution ob;
cout << ob.sumOfSeries(N) << "\n";
}
}
// } Driver Code Ends
</code></pre>
 
Top