0

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.

hmmmm
  • 5,616

1 Answers1

2

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.

Mark McClure
  • 30,510
Hakim
  • 10,213
  • There are a few issues, which I tried to address in an edit (as is very common on stackexchange) - not sure why you rolled it back? First, the answer is incorrect if the smaller number is prime itself: PrimePi[7] - PrimePi[5] returns 1, but there are two primes in that closed interval. Second, there's a standard way to post code that allows users to copy it, which an image does not. Finally, why are you using Mathematica's WolframAlpha interface? – Mark McClure Feb 22 '14 at 12:42
  • @MarkMcClure 1) Yes, you're right; I assumed in that $a$ is not a prime, so your code is more efficient. 2) I posted an image just as an example but I used the <code></code> command to write the code. 3) I don't know.

    Anyway, thank you so much and cheers!

    – Hakim Feb 22 '14 at 12:51
  • It's not a question of efficiency - it's a question of correctness. I hope you don't mind if I make another edit. Again, that's quite common on the StackExchange network - even assumed. – Mark McClure Feb 22 '14 at 12:59
  • I went ahead and made the edit. I hope you appreciate it. Notice that I'm not typing Mathematica code at the WolframAlpha prompt, but an actual English language question. – Mark McClure Feb 22 '14 at 13:06
  • @MarkMcClure Thank you for your effort! Just a question: what did you mean by that I'm using Mathematica's WolframAlpha interface? Is there another interface? Sorry for being still new to mathematica. – Hakim Feb 22 '14 at 15:31
  • In your original answer, you posted this image that contained an equals sign outlined in an orange triangle. That indicates that you're accessing Mathematica's short WA interface by pushing the equals sign button once at the beginning of a standard Input cell. The result is a single, simple interpretation of your input - in your case, just exactly what you typed in the first place. Note that you could just as easily have typed = prime pi 33 - prime pi 11, even though that's not standard Mathematica syntax for what you were trying to type. – Mark McClure Feb 22 '14 at 15:56