Caniko
Caniko

Reputation: 872

Greatest common divisor of multiple (more than 2) numbers

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!

Upvotes: 10

Views: 12221

Answers (2)

Shashank
Shashank

Reputation: 9

 `% 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
  '

Upvotes: 0

Related Questions