Python Forum
GCF function w recursion and helper function(how do i fix this Recursion Error)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GCF function w recursion and helper function(how do i fix this Recursion Error)
#1
def gcd(x,y):
    if x > y:
        return gcd_helper(x, y, y)
    else:
        return gcd_helper(x, y, x)
    if x == y:
        return gcd_helper(x, y ,x)
def gcd_helper(x, y, f):
    f = gcd(x,y)
    if x % f == 0:
        return f
    else: 
        return gcd_helper(x, y, f-1)
    
print(gcd(2,3))
Error:
RecursionError: maximum recursion depth exceeded in comparison
How can I fix this?
Reply
#2
lines 6 and 7 are unreachable. you will return on the if or the else.
you don't understand the proper use of return.
Please explain what you are trying to acheive.
Reply
#3
A recursion depth error says you have too many functions calling functions calling functions and so on and so on and Python has decided that is enough. I think the limit is 1000 functions deep. Usually this happens because your recursive function has a bug, but it could just be that you need more recursive function calls than Python allows.

The reason you are exceeding the max recursive calls limit is because your code is wrong. I will simplify.
def gcd(x,y):
    gcd_helper(x, y, y)
    
def gcd_helper(x, y, f):
    gcd(x,y)
See it now? Without changing the value of x or y, gcd calls gcd_helper(x, y, -), The first thing gcd_helper does is call gcd(x, y). This goes on about 500 times and your program crashes.

I do not know what the logic should be, but somewhere there has to be something that returns a value without having to make a function call. Your code doesn't have that.

Larz is correct about lines 6 and 7 being unreachable, but it doesn't make any difference because the results would be the same even if the function did include them in the execution path.
def gcd(x,y):
    if x > y:
        return gcd_helper(x, y, y)
    if x == y:
        return gcd_helper(x, y ,x)
    return gcd_helper(x, y, x)
Here we have different logic for x == y and x < y, but it does the same thing as this
def gcd(x,y):
    if x > y:
        return gcd_helper(x, y, y)
    return gcd_helper(x, y, x)
Since the code is all wrong it probably doesn't matter.
Reply
#4
Does GCD stand for Greatest Common Denominator? Is there any chance that this is how the code is supposed to work?
def gcd(x,y):
    if x > y:
        return gcd_helper(x, y, y)
    else:
        return gcd_helper(x, y, x)

def gcd_helper(x, y, f):
    if x % f == y % f == 0:
        return f
    else: 
        return gcd_helper(x, y, f-1)
     
print(gcd(3,2))
If so it would be faster and simpler to replace the helper function with a loop.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  rotate error function schniefen 0 890 Dec-07-2022, 02:40 PM
Last Post: schniefen
  Manually raising two error types with a function that computes sqfeet to sqmeters sean1 7 2,086 Nov-12-2021, 05:01 PM
Last Post: deanhystad
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,356 Jun-23-2021, 07:33 PM
Last Post: deanhystad
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,262 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  How to write a recursion syntax for Sierpinski triangle using numpy? Bolzano 2 3,852 Apr-03-2021, 06:11 AM
Last Post: SheeppOSU
  recursion task scorp08 9 3,143 Feb-01-2021, 02:56 AM
Last Post: subtra3t
  Annuity function for school - syntax error peterp 2 1,966 Oct-12-2020, 10:34 PM
Last Post: jefsummers
  "RecursionError: maximum recursion depth exceeded in comparison" hhydration 1 1,807 Sep-26-2020, 03:07 PM
Last Post: deanhystad
  math problem using recursion? mnh001 14 4,481 Sep-03-2020, 07:34 PM
Last Post: mnh001
  recursion bilbo_dragons 1 1,738 Feb-04-2020, 07:20 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020