1

For quite a while now I've successfully used a simple function that reliably (over-)estimates the number of primes up to a given n, for example for allocating space to hold the primes. Now I'm looking for something that does the same for the number of primes in an interval [m, n].

The objective is to get as close to the actual prime count as possible in order to minimise the waste of memory, but never, ever underestimate the actual count because that would mean costly resizings/reallocations.

Here's the function that I've been using:

double overestimate_prime_count_up_to (double n)
{
    if (n < 137)
        return 32;
    else
        return 32 + (n / (Math.Log(n) - 1.08513));
}

ATM it is only certified for the 32-bit range but I intend to change that.

Anyway, now I'm looking for a way to get a similar estimate for the number of primes in a given interval [m, n] instead of for all primes up to n.

If a reliable underestimate for the number of primes up to n could be found then it could be used to construct a reliable overestimate for an interval [m, n], by subtracting an (under-)estimate for the lower boundary from on (over-)estimate for the upper boundary.

Naturally, the underestimate thing is just an idea for a possible solution; the goal remains to get a reliable (over-)estimate of the number of primes in an interval, in order to waste as little space as possible while never paying the price for an underestimate (or at least so rarely that it doesn't matter overall).

I've studied the page How Many Primes Are There? at the superb Prime Pages site, followed oodles of links and read umpteen papers. But all it did was make my head swim...

P.S.: comments regarding the extension of usability to the 64-bit range are very welcome, especially as this may well require differentiating more cases or even completely different strategies (like approximating prime density functions).

DarthGizka
  • 4,347
  • 1
  • 24
  • 36
  • 1
    You will probably have better luck on [math.se]. – Nico Schertler Dec 31 '19 at 18:30
  • @ Nico:They might object because the purpose of my question is purely mercenary/practical - hankering after an approximate closed form without any respect for the beauty of infinite series, Riemann zeroes and Möbius functions. ;-) – DarthGizka Dec 31 '19 at 18:57
  • I'm voting to close this question as off-topic because it is a mathematics question. Estimating the number of primes in a range is a noble mathematical endeavor. – Raymond Chen Jan 01 '20 at 00:12
  • 1
    There are proven and close overestimates and proven and close underestimates. By combining them you will have your answer. – President James K. Polk Jan 01 '20 at 03:34

1 Answers1

0

One thing you can do is to first calculate the exact value for the number of primes less than or equal to n when n is <=55 (basically just a piecewise function breaking up at every one of the 16 primes in [2,55] Then you can use the following estimates by B. Rosser

If n>55 then the amount of primes less than or equal to n is bounded above by n/(ln(n)-4) and bounded from below by n/(ln(n)+2).

So you can do

def underestimate_prime(n):
    if n<55:
        return exact_count_prime(n)
    else:
        return n/(ln(x)+2)
def numprimesininterval(upperbound,lowerbound):
    return overestimate_prime_count_up_to(upperbound)-underestimate_prime(lowerbound-1)

Juan Carlos Ramirez
  • 2,054
  • 1
  • 7
  • 22