More Prime-Counting Functions
July 26, 2011
In the previous exercise we studied Legendre’s prime-counting function. In today’s exercise we will look at two more prime-counting functions, one from Ernst Meissel in the late 1800s and the other from Derrick Lehmer in the mid 1990s.
Both formulas are based on Legendre’s original formula; in both cases, they simply rearrange the terms of Legendre’s formula to eliminate some work. We won’t try to give derivations, as they are complex and chock-full of Greek letters; if you are interested, Hans Riesel’s book Prime Numbers and Computer Methods for Factorization was the source for all three formulas.
Meissel: , where
and
Lehmer: , where
,
,
, and
for
.
Your task is to implement the prime-counting functions of Meissel and Lehmer, then compare timings with Legendre's prime-counting function of the previous exercise. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Pages: 1 2
I think your write-up of Lehmer’s formula on the first page doesn’t match your code on the second. Specifically, what is
b_isupposed to be? And is the fraction out front(b + a - 2) * (b - a + 1) / 2or(b + c - 2) * (b - c + 1) / 2?Oops. I didn’t do that very well, did I? It’s now fixed.
b_i = pi(isqrt(x/p_i) for a<i<=c
The fraction is (b+a-2)(b-a+1)/2
I’m completely off my game; Python is either giving me nonsense or complaining of exceeding the recursion depth. I’m going to start over, so I won’t have a solution for a bit.