필터 지우기
필터 지우기

Loop is taking a lot of time

조회 수: 1 (최근 30일)
Kevin
Kevin 2013년 5월 27일
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.

채택된 답변

David Sanchez
David Sanchez 2013년 5월 27일
Matlab 2013 in a i3 machine takes time =
0.3825
seconds to perform your code. I guess your problem lies on your machine.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by