tags:

views:

237

answers:

2

Can someone give an example for finding greatest common divisor algorithm for more then two numbers?

I believe programming language doesn't matter.

+9  A: 

Start with the first pair and get their GCD, then take the GCD of that result and the next number. The obvious optimization is you can stop if the running GCD ever reaches 1. I'm watching this one to see if there are any other optimizations. :)

Oh, and this can be easily parallelized since the operations are commutative/associative.

280Z28
+3  A: 

The GCD of 3 numbers can be computed as gcd(a, b, c) = gcd(gcd(a, b), c). You can apply the Euclidean algorithm, the extended Euclidian or the binary GCD algorithm iteratively and get your answer. I'm not aware of any other (smarter?) ways to find a GCD, unfortunately.

Michael Foukarakis