Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greatest common divisor of multiple (more than 2) numbers

Tags:

math

matlab

I am seeking for the easiest solution to get the greatest common divisor of multiple values. Something like:

x=gcd_array(30,40,35) % Should return 5
x=gcd_array(30,40) % Should return 10

How would you solve this?

Many thanks!

like image 479
Caniko Avatar asked Jun 19 '12 09:06

Caniko


1 Answers

gcd(a,b,c) = gcd(a,gcd(b,c))

Which means you can use recursion.

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.73.3&rep=rep1&type=pdf&ei=90jgT9KPAtLS4QSNlOGdDQ&usg=AFQjCNGH_GewFofxWPfX2BDN6T5NF9PxAA

like image 89
flec Avatar answered Oct 19 '22 19:10

flec