Borrar filtros
Borrar filtros

Loop is taking a lot of time

1 visualización (últimos 30 días)
Kevin
Kevin el 27 de Mayo de 2013
Hello,
I am trying to create a code that gives me the prime factors of a number but it is requiring a lot of time. Here is my code:
N = 12345678;
t=1;
for j = 2 : N
s = 0;
while N/j == floor(N/j)
N = N/j;
s = s + 1;
end
if s > 0
for k=1:s
primefactors(t)=j;
t=t+1;
end
end
end
disp(primefactors)
Now, I understand that loops can take time if the number is large, although I've noticed that when you take off the ; after "primefactors(t)=j;", the output displays all of the prime numbers and just stops. I've also tried to add this code to various places to try and force the program to stop once it has found all of the prime numbers:
if prod(primefactors)==N
break
end
Unfortunately, the outcome is the same. Is there a reason why this isn't working, or is there another work around aside from rewriting the code?
Thanks in advance!
edit: I am also using octave if that makes any difference.

Respuesta aceptada

David Sanchez
David Sanchez el 27 de Mayo de 2013
Matlab 2013 in a i3 machine takes time =
0.3825
seconds to perform your code. I guess your problem lies on your machine.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by