# How to code how many prime numbers are there between 1 million and 2 million on MATLAB

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.

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

## 1 Answer

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.