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)
#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


Messages In This Thread
RE: GCF function w recursion and helper function(how do i fix this Recursion Error) - by deanhystad - Oct-05-2020, 07:47 PM

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