The greatest common divisor of several (more than 2) numbers

I am looking for the simplest 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!

+8
math matlab
source share
2 answers
+21
source share
  `% GCD OF list of Nos using Eucledian Alogorithm function GCD= GCD(n); x=1; p=n; while(size(n,2))>=2 p= n(:,size(n,2)-1:size(n,2)); n=n(1,1:size(n,2)-2); x=1; while(x~=0) x= max(p)-min(p); p = [x,min(p)]; end n=[n,max(p)]; p= []; end ' 
+1
source share

All Articles