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

Getting a Range error: Maximum call stack size exceeded, while multiplying 2 positive integers using recursion

  • Thread starter Thread starter Pranav S
  • Start date Start date
P

Pranav S

Guest
Code:
#TODO: Create multiplication,multiply(), of two natural numbers using addition and our friend recursion..

def multiply(a, b):

    if b == 0:
        return 0
    if a==0:
        return 0
    if b==1:
        return a
    
    else:
        return a + multiply(a, b-1)

I believe the base case and the recursion function is correct. but the compiler says that there is a range error: Maximum call stack size exceeded. How do I limit the stack size?
<pre class="lang-py prettyprint-override"><code>#TODO: Create multiplication,multiply(), of two natural numbers using addition and our friend recursion..

def multiply(a, b):

if b == 0:
return 0
if a==0:
return 0
if b==1:
return a

else:
return a + multiply(a, b-1)
</code></pre>
<p>I believe the base case and the recursion function is correct. but the compiler says that there is a range error: Maximum call stack size exceeded. How do I limit the stack size?</p>
 

Latest posts

Top