There is a formula given at the bottom of the following webpage:
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!
:=
vs=
. $\endgroup$