0
$\begingroup$

Legendre's formula can be very easily be generalised as mentioned here (visible after login) which is like this

${\pi}(v,p)={\pi}(v,p-1)-1.[{\pi}(v/p,p-1)-{\pi}(p-1,p-1)]$

${ \big\downarrow}$

$S(v,p)=S(v,p-1)-p[S(v/p,p-1)-S(p-1,p-1)]$

This is still $O(n^{3/4})$ algorithm

I have been trying to achieve the same with Lehmer's formula

$\pi(n)=φ(n, a)+\frac{1}{2}(b+a-2)(b-a+1)-\sum_{i=a}^{b}{\pi}(\frac{x}{p_i})-\sum_{i=a+1}^c \sum_{j=i}^{b_{i}}[{\pi}(\frac{x}{p_ip_j})-(j-1)]$

where $b = n^{1/2}, c = \pi(n^{1/3}), a = \pi(n^{1/4}), φ(n,a)-$ number of integers in [1;n] such that they are not divisible by any prime among first $a$ primes.

${ \big\downarrow}$

$S(n)=\Phi(n, a)+\frac{1}{2}(\sum b+\sum a-2)(\sum b-\sum a+1)-\sum_{i=a}^{b}p_i{S}(\frac{x}{p_i})-\sum_{i=a+1}^c \sum_{j=i}^{b_{i}}[p_ip_j{S}(\frac{x}{p_ip_j})-\sum(j-1)]$

The above expression works fine till $n=8$; after that it doesn't. I am not exactly sure what to do with $\frac{1}{2}(b+a-2)(b-a+1)$ It should not be left as is. Adding $\sum$ may not be the right way to go.

Then I realised $\frac{1}{2}(b+a-2)(b-a+1)=a+(a+1)+(a+2)+.......for (b-a) terms$

My required sum should be $\sum a+\sum (a+1)+\sum (a+2)+.......for (b-a) terms$ but even that gives an expression $(b-a)[(a^2+ab+b^2)-1]/6$ which is also giving the correct answer for $n \le 8$.

I have successfully transitioned to from $φ(n, a)$ to $\Phi(n, a)$ where $\Phi(n, a)$ - sum of integers in [1;n] such that they are not divisible by any prime among the first $a$ primes.

I guess the equivalent expression of $\frac{1}{2}(b+a-2)(b-a+1)$ should be $s'(a)+s'(a+1)+s'(a+2)+.......for (b-a)$

note: $s'(x)$ is the sum of first $x$ primes; $S(x)$ is the sum of all primes up to $x$.

PS: I am not looking for any suggestion of alternate $O(n^{2/3})$ or similar algos.
For that I have already gone through Fastest Algorithm to Compute the Sum of Primes? and more similar literature.
As a part of this question I am only looking to generalise the Meissel-Lehmer.

$\endgroup$

2 Answers 2

2
$\begingroup$

This might be a "shameless plug" but i did recently "generalize Meissel-Lehmer to count sum of the powers of primes". This was supposed to give an exposition, so it might be of help https://arxiv.org/abs/2111.15545

$\endgroup$
4
  • 1
    $\begingroup$ Even after generalizing Meissel-Lehmer I am finding my recursive implementation of Meissel-Lehmer is slower than the Iterative implementation of Legendre's one. Thanks for publishing the code. I dont know why but very few number theory researchers do. $\endgroup$ Commented Sep 17, 2022 at 20:57
  • $\begingroup$ Great work. No wonder I was gettiing such terrible performance. I had just plugged in P2() ,P3() as in definition who in turn calls F() back making a mess of mutual recursion. Simplification really helped. I knew fenwik tree would help to optimise further. This paper just seems to be the work that I needed. May I take this opputunity to ask a realted("shameless") question. Can you tell me what algo is this No sieve, no advanced data structure, mere 50 lines of code yet performing way better than meissel-lehmer. Is it LMO or Deleglise-Rivat? $\endgroup$ Commented Sep 18, 2022 at 5:39
  • $\begingroup$ I have no clue what they are doing with smalls, roughs and larges. It might be efficient way of implementing meissel-lehmer where they are using larges for $\phi$ , roughs for $P_2$ and smalls for $P_3$ as what it seems. I know comments may not be the best place to have such extensive discussion, but I guess my mathoverflow profile is too new to allow chat invitation functionality. I am anyways available at [email protected] for more extensive. $\endgroup$ Commented Sep 18, 2022 at 8:36
  • 1
    $\begingroup$ @LucyBenjamin, smalls and larges are conceptually one array. If $x \le \sqrt{N}$ then the value is found in smalls[x], otherwise in larges[N/x]. (Actually it's slightly more complicated because only odd indices are handled, but I hope you get the general idea). I haven't analysed the code to figure out what roughs is, but I suspect that this is Meissel-Lehmer implemented non-recursively. $\endgroup$ Commented Sep 21, 2022 at 14:06
3
$\begingroup$

If you consult Lehmer's paper, the terms in question arise from $$P_2(x, a) = \sum_{p_a < p_i \le \frac x{p_i}} \sum_{p_i \le p_j \le \frac x{p_i}} 1 = \sum_{p_a < p_i \le b} \left\{ \pi\left(\frac x{p_i}\right) - (i-1) \right\}$$ counting semiprimes made up from primes in a certain range. The analogue would be

\begin{eqnarray*} \Phi_2(n, a) &=& \underset{p_a < p_i \le p_j \le \frac n{p_i}}{\sum\sum} p_i p_j \\ &=& \sum_{p_a < p_i \le b} p_i \left( S\left(\frac n{p_i}\right) - S(p_i - 1) \right) \\ &=& \sum_{p_a < p_i \le b} p_i \left( S\left(\frac n{p_i}\right) - S(p_i) + 1 \right) \\ &=& \sum_{p_a < p_i \le b} p_i - \sum_{p_a < p_i \le b} p_i S(p_i) + \sum_{p_a < p_i \le b} p_i S\left(\frac n{p_i}\right) \\ &=& S(b) - a - \sum_{p_a < p_i \le b} p_i S(p_i) + \sum_{p_a < p_i \le b} p_i S\left(\frac n{p_i}\right) \end{eqnarray*}

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .