Ask Your Question

Revision history [back]

Recursion oddity

I've been having a problem with a recursive factorization algorithm I've been writing in Sage code. The problem is reproduced on my machine using the code below:

def gcd_recur(num):
    if(num <= 1):
        return
    g = gcd(15,num)
    print "GCD of 15 and {0}: {1}".format(num, g)
    gcd_recur(num/2)  

gcd_recur(50)

The result I'm getting is:

GCD of 15 and 50: 5
GCD of 15 and 25: 1
GCD of 15 and 25/2: 1
GCD of 15 and 25/4: 1
GCD of 15 and 25/8: 1
GCD of 15 and 25/16: 1

However, I can run the code below outside of the algorithm and receive the correct result:

gcd(15, 25)

5

Are there some variable scoping issues that I'm not seeing here? Thanks.