I am new on matla and this is my attempt on doing a code to get the GCD. Any tips?


This topic was flagged by Steven Lord
n= input('Escolhe um número inteiro postivo. ')
primo=true;
i=2;
while i<n
if mod(n,i)==0;
primo=false;
end
i= i+1;
end
if primo && n>1;
disp('É primo')
else
disp('Não é primo')
end
anterior= n-1;
while true
primo=true;
i=2;
while i< anterior
if mod(anterior,i)==0;
primo= false;
end
i= i+1;
end
if primo && anterior>1;
end
anterior= anterior-1;
end
disp(anterior)
seguinte= n+1;
while true;
primo= true;
i=2;
while i<seguinte;
if mod(seguinte,i)==0;
primo=false;
end
i=i+1;
end
if primo && seguinte>1;
end
seguinte= seguinte+1;
end
disp(seguinte)
Any ideas? It is in portuguese if you intend to translate it.
Stephen23
Stephen23 about 4 hours ago
Steven Lord
Steven Lord on 6 Jul 2026 at 15:31
User posted this question again as https://www.mathworks.com/matlabcentral/discussions/general/888872-hello-guys-i-am-new-to-matlab-and-i-made-a-code-to-get-the-gcd-of-a-number-but-i-suspect-it-could-b and it looks like that got a comment with code. The two posts ought to be consolidated. Normally I'd say the newer post should be closed as a duplicate, but the comment in that newer post seems a bit more informative.
Duncan Carlsmith
Duncan Carlsmith on 4 Jul 2026 at 18:57
https://stackoverflow.com/questions/26753839/efficiently-getting-all-divisors-of-a-given-number

Tags

No tags entered yet.