%% how many primes to find
% https://primes.utm.edu/nthprime/index.php#nth
nprimes = 1e5;
%% use MATLAB's built-in isprime() function
tic
nfound = 1; % how many primes we've found so far
pfound = 2; % the most recent prime found
n = 3; % where to start looking for the rest
nmsg = floor(nprimes/10); % msg to screen in 10% increments
while (nfound < nprimes)
if isprime(n)
nfound = nfound + 1;
pfound = n;
if (mod(nfound,nmsg)==0)
fprintf("... found %d primes so far (%d) ...\n", nfound, pfound);
end
end
n = n + 2;
end
fprintf("the %dth prime is %d\n", nprimes, pfound);
toc
%% use plg_isprime() function
tic
nfound = 1; % how many primes we've found so far
pfound = 2; % the most recent prime found
n = 3; % where to start looking for the rest
nmsg = floor(nprimes/10); % msg to screen in 10% increments
while (nfound < nprimes)
if plg_isprime(n)
nfound = nfound + 1;
pfound = n;
if (mod(nfound,nmsg)==0)
fprintf("... found %d primes so far (%d) ...\n", nfound, pfound);
end
end
n = n + 2;
end
fprintf("the %dth prime is %d\n", nprimes, pfound);
toc