MATLAB: For the prime number HW

homework

dd

Best Answer

In answer to the question "Why the for loop ends on sqrt(n) and not on (n)?", a number is not prime if and only if it has at least two positive integer factors each greater than one. These can't all be greater than sqrt(n), for otherwise their product would exceed n. Hence if all j values from 1 to sqrt(n) are tried for a non-prime n, one of those factors must have been encountered. There is no need to go any further.
Second observation: the code you display is not matlab code.
Related Question