TOPICS
Search

Legendre's Formula


Legendre's formula counts the number of positive integers less than or equal to a real number x>=0 which are not divisible by any of the first a primes, where a is a nonnegative integer,

 phi(x,a)=|_x_|-sum|_x/(p_i)_|+sum|_x/(p_ip_j)_|-sum|_x/(p_ip_jp_k)_|+...,
(1)

where |_x_| is the floor function. For x>=1, taking a=pi(sqrt(x)), where pi(n) is the prime counting function, gives

 phi(x,pi(sqrt(x)))=pi(x)-pi(sqrt(x))+1=|_x_|-sum_(p_i<=sqrt(x))|_x/(p_i)_|+sum_(p_i<p_j<=sqrt(x))|_x/(p_ip_j)_|-sum_(p_i<p_j<p_k<=sqrt(x))|_x/(p_ip_jp_k)_|+....
(2)

Legendre's formula holds since one more than the number of primes in a range equals the number of integers minus the number of composites in the interval.

For a nonnegative integer a, let P_a=product_(j=1)^(a)p_j, where p_j is the jth prime, with P_0=1. The inclusion-exclusion principle gives the compact equivalent form

 phi(x,a)=sum_(d|P_a)mu(d)|_x/d_|,
(3)

where mu is the Möbius function and the sum is over the positive divisors of P_a. Indeed, sum_(d|gcd(m,P_a))mu(d) is 1 when m is relatively prime to P_a and 0 otherwise. Summing this indicator function over 1<=m<=|_x_| and interchanging the two finite sums gives the formula.

For a>=1, Legendre's formula satisfies the recurrence relation

 phi(x,a)=phi(x,a-1)-phi(x/(p_a),a-1).
(4)

Let m_k=p_1p_2...p_k, then

phi(m_k,k)=|_m_k_|-sum|_(m_k)/(p_i)_|+sum|_(m_k)/(p_ip_j)_|-...
(5)
=m_k-sum(m_k)/(p_i)+sum(m_k)/(p_ip_j)-...
(6)
=m_k(1-1/(p_1))(1-1/(p_2))...(1-1/(p_k))
(7)
=product_(i=1)^(k)(p_i-1)
(8)
=phi(m_k),
(9)

where phi(n) is the totient function, and

 phi(sm_k+t,k)=sphi(m_k)+phi(t,k),
(10)

where 0<=t<=m_k. If t>m_k/2, then

 phi(t,k)=phi(m_k)-phi(m_k-t-1,k).
(11)

Legendre's formula is not practical for computing pi(n) for large arguments. A more efficient modification is Meissel's formula.


See also

Lehmer's Formula, Mapes' Method, Meissel's Formula, Prime Counting Function

Explore with Wolfram|Alpha

References

Séroul, R. "Legendre's Formula" and "Implementation of Legendre's Formula." §8.7.1 and 8.7.2 in Programming for Mathematicians. Berlin, Germany: Springer-Verlag, pp. 175-179, 2000.

Referenced on Wolfram|Alpha

Legendre's Formula

Cite this as:

Weisstein, Eric W. "Legendre's Formula." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/LegendresFormula.html

Subject classifications