how can I get the gcd for more than 2 terms?

I want to find the greatest common divisor of "n" terms to apply the following method 72 = 2 ^ 3 * 3 ^ 2 108 = 2 ^ 2 ^ 3 * 3 60 = 2 ^ 2 * 3 * 5 the gcd (76,108,60) will be = 2 ^ 2 * 3 = 12 It is the result of multiplying the common factors with lower exponent I have this so far it only works with this problem but I need it to work with any kind of terms
function mcd
B=[72 108 50];
a=1;
for i=1:length(B)
[p,h]=min(mode(factor(B(i))));
a=p.^h.*a;
end
disp(a);

답변 (1개)

Roger Stafford
Roger Stafford 2015년 7월 10일

0 개 추천

Just apply the 'gcd' function multiple times:
p = B(1);
for k = 2:length(B)
p = gcd(p,B(k));
end

댓글 수: 1

thanks a lot but what i want is to know how to solve it by the method that i wrote with factors

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 7월 10일

댓글:

2015년 7월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by