0
$\begingroup$

How would I find the total number of prime numbers between 1 million and 2 million on MATLAB. I have the code for displaying every prime number between 2 integers being:

n = firstnumber : secondnumber;
p = isprime(n);
n(p)     %displays the primes

But how would I code it so it gives me a total number of primes between 2 numbers.

Any help will be appreciated.

$\endgroup$
  • $\begingroup$ From p, you only need sum(p) or nnz(p) $\endgroup$ – Luis Mendo Jan 23 '17 at 10:15
0
$\begingroup$

Not necessarily the most efficient way of doing it but for your numbers it's still very fast:

p = primes(secondnumber);
n = length(find(p>=firstnumber));

About 20ms here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.