For loop checking if number is prime (without using MATLAB functions)

6 visualizaciones (últimos 30 días)
This is my code. Task is to check if x2 is prime. If yes, set y2 to be logical one and if no, set y2 to be logical zero. We are not allowed to use pre-made functions.
x2=22;
for i=2:ceil(x2/2)
if mod(x2,i) ==0
y2=false;
else
y2=true;
end
end
My code runs and seems to work well for all numbers beside x2=99. For this input, it is giving me logical one as a result. Why and how to fix it?
Thanks in advance

Respuesta aceptada

John D'Errico
John D'Errico el 28 de En. de 2021
Editada: John D'Errico el 28 de En. de 2021
Your code does NOT work well. In fact, it has a serious bug in it.
You are using a loop. EVERY pass through the loop, it sets y2 to be either true or false. So if the final pass through the loop, you just happen to get the result that mod(x2,i) == 0 or not, then your code will fail.
Instead, if ANY of those mods is zero, then you are done. There is no need to test any further, as the number cannot be prime. So you can use break to exit the loop as soon as that happens. Or you can use a while loop.
Next, you should recognize that going all the way out to x2/2 is far too long of a loop. Do you need to go past sqrt(x2)? THINK ABOUT IT!
Consider the difference between
sqrt(1e6)
ans = 1000
1e6/2
ans = 500000
Which loop do you want to execute?
  3 comentarios
John D'Errico
John D'Errico el 28 de En. de 2021
Editada: John D'Errico el 28 de En. de 2021
x2/2? Insane. But homework is homework. Suppose you did this?
y2 = true;
for i = 2:ceil(x2/2)
if mod(x2,i) == 0
y2 = false;
break
end
end
So as soon as it hits a zero, it quits. If you have no clue that break exists, or have not been taught about break yet, or you are not allowed to use break...
y2 = true;
for i = 2:ceil(x2/2)
if y2 && (mod(x2,i) == 0)
y2 = false;
end
end
The latter loop will run across the entire set. It is less efficient of course. Why would you really want to test EVERY possible divisor when x2 is even number? Surely, the first loop will quit as soon as it tests to see if x2 is divisible by 2? The loop never actually runs when x2 is 2, so y2 is automatically true there.
Note that the above code will have a subtle problem when x2 == 1, but x2 is not actually prime, so good code would verify that x2 is at least as large as 2.
Losoupbowla
Losoupbowla el 28 de En. de 2021
I think break will do, since there were no specific rules saying not to use break. As regarding why I would test every divisor is that when prof is grading them, he actually uses a program that inputs random values at the place of x2 so we never know what will get tested.
Thank you for your help, truly appreciate it.

Iniciar sesión para comentar.

Más respuestas (1)

John Wirzburger
John Wirzburger el 28 de En. de 2021
It appears that it has to do with when you set y2. In short, you are only reporting back the y2 value when i=ceil(x2/2).

Categorías

Más información sobre Matrix Indexing 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