primeCount {RcppAlgos} | R Documentation |
Prime Counting Function
Description
Prime counting function for counting the prime numbers less than an integer, , using Legendre's formula. It is based on the the algorithm developed by Kim Walisch found here: kimwalisch/primecount.
Usage
primeCount(n, nThreads = NULL)
Arguments
n |
Positive number |
nThreads |
Specific number of threads to be used. The default is |
Details
Legendre's Formula for counting the number of primes less than makes use of the inclusion-exclusion principle to avoid explicitly counting every prime up to
. It is given by:
Where is the number of positive integers less than or equal to
that are relatively prime to the first
primes (i.e. not divisible by any of the first
primes). It is given by the recurrence relation (
is the
prime (e.g.
)):
This algorithm implements five modifications developed by Kim Walisch for calculating efficiently.
Cache results of
Calculate
using
if
prime[a]
prime[a]
(i.e. Euler's totient function)
Calculate
using
lookup table
Calculate all
upfront
Stop recursion at
if
or
instead of
Value
Whole number representing the number of prime numbers less than or equal to .
Note
The maximum value of is
Author(s)
Joseph Wood
References
Computing
: the combinatorial method
Tomás Oliveira e Silva, Computing pi(x): the combinatorial method, Revista do DETUA, vol. 4, no. 6, March 2006, p. 761. https://sweet.ua.pt/tos/bib/5.4.pdf
See Also
Examples
## Get the number of primes less than a billion
primeCount(10^9)
## Using nThreads
system.time(primeCount(10^10, nThreads = 2))