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


Steven Lord Ha marcado con alerta este/a topic
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 el 8 de Jul. de 2026 a las 4:44
Steven Lord
Steven Lord el 6 de Jul. de 2026 a las 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 el 4 de Jul. de 2026 a las 18:57
https://stackoverflow.com/questions/26753839/efficiently-getting-all-divisors-of-a-given-number

Etiquetas

Aún no se han introducido etiquetas.