# Number of primes between two integers x and y (with x < y and excluding x and y)

There is a formula given at the bottom of the following webpage:

https://math.stackexchange.com/questions/288747/how-to-find-number-of-prime-numbers-between-two-integers

to calculate the number of primes between two integers x and y (with x < y and excluding x and y).

The formula is:

PrimePi[y - 1] - PrimePi[x + 1]


(where I'm using Mathematica's prime counting function PrimePi instead of Pi[n]).

I'm attempting to build and improve the above formula.

The following examples suggest a way to get such a formula:

(** Example 1: Find the primes between 10 and 20: **)

table1 = Table[Prime[i], {i, PrimePi[NextPrime[10]], PrimePi[NextPrime[20, -1]]}]
(* {11, 13, 17, 19} *)

Length[table1]
(* 4 *)


(** Example 2: Find the primes between 10 and 50: **)

table2 = Table[Prime[i], {i, PrimePi[NextPrime[10]], PrimePi[NextPrime[50, -1]]}]
(* {11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47} *)

Length[table2]
(* 11 *)


(** Example 3: Find the primes between 11 and 47 (excluding 11 and 47): **)

table3 = Table[Prime[i], {i, PrimePi[NextPrime[11]], PrimePi[NextPrime[47, -1]]}]
(* {13, 17, 19, 23, 29, 31, 37, 41, 43} *)

Length[table3]
(* 9 *)


But when I attempt to build the function:

NumberOfPrimes[L_, M_] = Length[Table[Prime[i], {i, PrimePi[NextPrime[L]], PrimePi[NextPrime[M, -1]]}]]


I get a message:

"Table::iterb: Iterator {i,PrimePi[NextPrime[L]],PrimePi[NextPrime[M,-1]]} does not have appropriate bounds."

As far as I know; a function to calculate the number of primes between two integers does not exist in Mathematica's repertoire a priori. Can anyone help me to build this function? Thank you!

• Lookup := vs =. – Kuba Oct 6 '17 at 20:09
• Please use proper formatting of Mathematica code: indent the code block 4 spaces. That way it's easy for users to copy the code into a notebook. – murray Oct 6 '17 at 20:25
• Also, why did you insert all those spaces into the URL you cite so that it's no longer a hyperlink. If you avoid inserting the spurious spaces, you'll directly get a hyperlink: math.stackexchange.com/questions/288747/… – murray Oct 6 '17 at 20:28

As @Kuba said, use := and not =, that is:

    numberOfPrimes[a_, b_] :=
Length@Table[Prime[i], {i, PrimePi@NextPrime[a], PrimePi@NextPrime[b, -1]}]
numberOfPrimes[11, 47]
(* 9 *)


The reason you cannot use = (abbreviation for Set), which tries to evaluate the right-hand side and immediately assign it to the left-hand side, is that you don't have any specific values for the symbols used in the iterator bounds.

Style suggestions:

• Please do not use uppercase letters L and M for your function arguments. Names beginning with uppercase letters should be reserved for Mathematica's built-in objects. And since lowercase l is hard to distinguish from the number 1, it would be better to use different letters entirely, such as my a and b.
• Likewise, don't use an uppercase letter to begin your name for the function (unless you're designing a package).
• The "trick" of using prefix notation @ in the definition of the functions obviates the need for so many nested square brackets.

Why not just use PrimePi?

Is there some particular reason you want to define this function instead of using literally the mathematical formula you found (changing $\pi$ to PrimePi of course)? Using PrimePi seems to be more efficient:

   Table[
{a, b} = Sort@RandomInteger[{500, 1000}, 2];
First /@ Timing /@ {numberOfPrimes[a, b], PrimePi[b] - PrimePi[a]},
{10}] // TableForm
(*
5.*10^-6  1.*10^-6
2.*10^-6  1.*10^-6
1.*10^-6  1.*10^-6
1.*10^-6  0.
2.*10^-6  0.
1.*10^-6  0.
2.*10^-6  1.*10^-6
2.*10^-6  0.
0.        0.
1.*10^-6, 0.
*)