MATLAB: How to write a program that can display the first 50 consecutive prime numbers starting with 2

homeworkprime numbers

Primes(x) and isPrime(x) are not allowed for this question. fprintf should be used. I was thinking of using a while loop with maybe a for and/or if's inside. I am new to MATLAB so any help would be appreciated. Thanks!

Best Answer

for ii = 1:inf
nguess = ii;
mat = tril(bsxfun(@times,2:nguess,(2:nguess).'));
pn = setdiff(2:nguess,mat(logical(mat)));
if numel(pn)>50
pn = pn(1:50);
break
end
end
Maybe?