MATLAB This code results in an infinite loop I do not know w

MATLAB: This code results in an infinite loop: I do not know where to place \"temp\" equal to num or if my incerements are correct.


while count < input
for (value = 2: ceil(sqrt(num)))
temp = num;
if(rem(num,value)==0) %not a prime number
temp = 0;
break
num = num + 1;
end
end
if temp == num
fprintf(\'%f\ \',temp)
count = count +1;
end
end
  

Solution

I have answered base on my understanding of what you are trying to achieve. If you have some doubt please ask.

#1 --> (Even the previous location will give the correct answer. But this makes it easier to read and understand.)

#2 --> (There is no point in keeping this after break. It will not get executed.)

#3 --> (Generally the count should be increase on every loop iteration. Although incrementing it for certain condition is required for certain programs .)

while count < input
temp = num; #1
for (value = 2: ceil(sqrt(num)))
if(rem(num,value)==0) %not a prime number
temp = 0;
break
end
end
if temp == num
fprintf(\'%f\ \',temp)   
end
num = num + 1; #2
count = count +1; #3
end

MATLAB: This code results in an infinite loop: I do not know where to place \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site