Using Mathematica to know the number primes that are contained in a given interval

188 Views Asked by At

So I want to know basically how to determine how many prime numbers (integers that can only be divided by 1 and themselves) are contained in a given interval.

Thank you.

1

There are 1 best solutions below

6
On BEST ANSWER

If $a$ and $b$ are integers, with $a$ composite, then the number of primes in the interval $[a,b]$ is the number of primes less than or equal to $b$ minus the number of primes less than or equal to $a$. If $a$ itself is prime, we should account for this by adding that one prime back on. To determine the number of primes less than or equal to a number in mathematica we use the command PrimePi[number].

We can program this as follows:

numPrimes[m_Integer, n_Integer] := If[PrimeQ[m],
  PrimePi[n] - PrimePi[m] + 1,
  PrimePi[n] - PrimePi[m]
];
numPrimes[11, 31]
numPrimes[10, 31]
numPrimes[555,666]

(* Out: 
  7
  7
  20
*)

If you're new to Mathematica, you can also use the WolframAlpha interface, which (in principle) allows you to ask your question in English.

enter image description here

I hope this helps.
Best wishes, $\mathcal H$akim.