Last week I posted about various forms of the Euclidean Algorithm. This week, what with recursion being a topic in CSC148, I thought I'll implement the subtraction method using recursion (division next week).
First, just to quickly restate, Euclidean Algorithm tells us that, if x > y then gcd(x, y) = gcd(x - y, y). In class we implemented this using an iterative method (involving loops, etc.). Here is how we could implement it recursively:
def gcd(x, y):
if x == 0:
return y
elif y = 0:
return x
elif x > y:
return gcd(x - y, y)
elif y > x:
return gcd(x, y - x)
Quick explanation of code: You can probably guess why the first two if's are there; they're there because gcd(x, 0) = x and gcd(0, y) = y. But what about the last two if statements? Well we know if x > y then gcd(x, y) = gcd(x - y, y), thus we can evaluate and return this simplified expression. In the process of evaluating it, it may have to evaluate a further simplified gcd. It will keep doing this until one of the expressions contains a parameter that equals 0; when the function can return a value to the previous function.
Okay, that last paragraph was a mouthful; hopefully everyone understood what I was trying to say. If not, feel free to ask in the comments!
Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts
Wednesday, 22 January 2014
Sunday, 12 January 2014
Euclidean Algorithm - Subtraction vs Division
Just a quick recap first: On Friday's lecture (January 10th) we utilized Euclidean algorithm. As mentioned in the class, the basic principle of said algorithm is that the GCD(Greatest Common Divisor) of two unique positive numbers is the same as the GCD of the greater number minus the lesser and the lesser number. For example, GCD(10,9) = GCD(10 - 9 = 1, 9). This means one can find the GCD of two unique positive numbers by continuously subtracting the lesser number from the greater until they are equal; once they are equal you know the GCD (since the GCD of x and x is x).
For those of you who took Mathematical Proofs(MAT102), you will remember Euclidean's algorithm being slightly different. In MAT102, the main principle was that the GCD of two numbers in the same as the GCD of the remainder (when dividing the greater number by the lesser) and the lesser number. You can thus work it down to when the remainder becomes 0 and thus determine the GCD (the now lesser number).
Interestingly, the method used in MAT102 is more efficient. As proven by Gabriel Lamé in 1844, it will never take more iterations than five times the number of digits. This means it will take a maximum of 15 iterations to determine GCD(100, 1) while the prior method would take 100 iterations.
I'm not entirely sure why Euclidean subtraction seemsto be used more in CS (in the CSC108 textbook and CSC148 lecture), but I thought the difference was rather interesting. If someone wants to shed some light on why the prior algorithm appears dominant, I'd be more than happy to know in the comments. My best guess is the prior one is simpler to understand and that neither CSC108 or 148 are math-oriented courses.
For those of you who took Mathematical Proofs(MAT102), you will remember Euclidean's algorithm being slightly different. In MAT102, the main principle was that the GCD of two numbers in the same as the GCD of the remainder (when dividing the greater number by the lesser) and the lesser number. You can thus work it down to when the remainder becomes 0 and thus determine the GCD (the now lesser number).
Interestingly, the method used in MAT102 is more efficient. As proven by Gabriel Lamé in 1844, it will never take more iterations than five times the number of digits. This means it will take a maximum of 15 iterations to determine GCD(100, 1) while the prior method would take 100 iterations.
I'm not entirely sure why Euclidean subtraction seemsto be used more in CS (in the CSC108 textbook and CSC148 lecture), but I thought the difference was rather interesting. If someone wants to shed some light on why the prior algorithm appears dominant, I'd be more than happy to know in the comments. My best guess is the prior one is simpler to understand and that neither CSC108 or 148 are math-oriented courses.
Subscribe to:
Posts (Atom)