This document serves as an overview for solving problems common in Computational
Mathematics. Of note, primeSieve and
primeCount are based on the excellent work by Kim Walisch.
primeSieve
The primeSieve function is based on the Segmented Sieve of Eratosthenes. As stated in the linked article, the sieve itself is already very efficient. The problem from an efficiency standpoint, is due to the memory requirements. The segmented version overcomes this by only sieving small sections at a time, which greatly facilitates use of the cache.
library(RcppAlgos)
library(microbenchmark)
options(width = 90)
microbenchmark(primeSieve(1e6))
#> Warning in microbenchmark(primeSieve(1000000)): less accurate nanosecond times to avoid
#> potential integer overflows
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> primeSieve(1e+06) 161.909 165.2915 168.8085 166.706 170.2115 239.153 100
## Single threaded primes under a billion!!!
system.time(primeSieve(10^9))
#> user system elapsed
#> 0.367 0.017 0.384
## Using 8 threads we can get under 0.5 seconds!!!
system.time(primeSieve(10^9, nThreads = 8))
#> user system elapsed
#> 0.488 0.019 0.088
## Quickly generate large primes over small interval. N.B. The
## order for the bounds does not matter.
options(scipen = 50)
system.time(myPs <- primeSieve(10^13 + 10^3, 10^13))
#> user system elapsed
#> 0.000 0.000 0.002
myPs
#> [1] 10000000000037 10000000000051 10000000000099 10000000000129 10000000000183
#> [6] 10000000000259 10000000000267 10000000000273 10000000000279 10000000000283
#> [11] 10000000000313 10000000000343 10000000000391 10000000000411 10000000000433
#> [16] 10000000000453 10000000000591 10000000000609 10000000000643 10000000000649
#> [21] 10000000000657 10000000000687 10000000000691 10000000000717 10000000000729
#> [26] 10000000000751 10000000000759 10000000000777 10000000000853 10000000000883
#> [31] 10000000000943 10000000000957 10000000000987 10000000000993
## Object created is small
object.size(myPs)
#> 320 bytesLarger primes
Since version 2.3.0, we are implementing the
cache-friendly improvements for larger primes originally developed by Tomás
Oliveira.
## Version <= 2.2.0.. i.e. older versions
system.time(old <- RcppAlgos220::primeSieve(1e15, 1e15 + 1e9))
#> user system elapsed
#> 1.977 0.026 2.008
invisible(gc())
## v2.3.0+ is faster
system.time(a <- primeSieve(1e15, 1e15 + 1e9))
#> user system elapsed
#> 0.743 0.019 0.762
invisible(gc())
## And using nThreads is much much faster
system.time(b <- primeSieve(1e15, 1e15 + 1e9, nThreads = 8))
#> user system elapsed
#> 1.350 0.048 0.221
identical(old, a)
#> [1] TRUE
identical(a, b)
#> [1] TRUEprimeCount
primeCount gives the number of primes less than or equal
to n, without constructing a vector of all those primes. It
supports Legendre, Meissel, and Lehmer methods, all of which return the
same exact count throughout the supported range.
The Legendre implementation and shared partial-sieve routines build
on the excellent work by Kim Walisch, which
requires OpenMP. One of the original goals was to make parallel prime
counting available without requiring OpenMP, including with the default
macOS toolchain. For this reason, RcppAlgos uses C++
threads for its parallel calculations.
By default, method = "auto" selects a method based on
n and the number of threads it can use. You can also select
a method explicitly, which is useful when comparing them on your own
inputs. See ?primeCount for the selection rules, counting
formulas, and memory requirements.
Calculations are serial by default. Use nThreads for
parallel computation when the problem is large enough; the actual number
of threads is limited by the available hardware and useful work.
## Count the primes less than or equal to a trillion
system.time(underOneTrillion <- primeCount(10^12))
#> user system elapsed
#> 0.056 0.001 0.056
underOneTrillion
#> [1] 37607912018
## Select a method explicitly
primeCount(10^6, method = "legendre")
#> [1] 78498
primeCount(10^6, method = "meissel")
#> [1] 78498
primeCount(10^6, method = "lehmer")
#> [1] 78498
## Count the primes less than or equal to ten billion
## and rely on auto choice for the method
microbenchmark(primeCount(10^10))
#> Unit: microseconds
#> expr min lq mean median uq max neval
#> primeCount(10^10) 791.259 797.0605 832.4226 833.1815 839.188 1932.781 100
## Use multiple threads for larger inputs
## again relying on auto choice for the method
system.time(underOneHundredTrillion <- primeCount(1e14, nThreads = 8))
#> user system elapsed
#> 2.163 0.007 0.317
underOneHundredTrillion
#> [1] 3204941750802
system.time(primeCount(1e14, nThreads = 8, method = "legendre"))
#> user system elapsed
#> 3.316 0.026 0.572
system.time(primeCount(1e14, nThreads = 8, method = "meissel"))
#> user system elapsed
#> 1.755 0.015 0.291
system.time(primeCount(1e14, nThreads = 8, method = "lehmer"))
#> user system elapsed
#> 1.932 0.006 0.283
## Still not as fast as Kim Walisch's primecount library:
cat(paste(
system("primecount 1e14 --legendre --time", intern = TRUE),
collapse = "\n"
))
#> 3204941750802
#> Seconds: 0.212
cat(paste(
system("primecount 1e14 --meissel --time", intern = TRUE),
collapse = "\n"
))
#> 3204941750802
#> Seconds: 0.087
## For some reason, lehmer's method is not as efficient as the other
## methods in the primecount library
cat(paste(
system("primecount 1e14 --lehmer --time", intern = TRUE),
collapse = "\n"
))
#> 3204941750802
#> Seconds: 0.404nthPrime
nthPrime finds the prime at a given position, starting
with nthPrime(1) returning 2. For example,
nthPrime(25) returns 97, whereas
primeCount(25) returns 9.
For larger positions, nthPrime combines prime counting
with a segmented sieve, without storing all preceding primes. Like
primeCount, it runs serially by default and accepts
nThreads for parallel computation when useful. See
?nthPrime for the supported range.
nthPrime(25)
#> [1] 97
## Find the millionth prime
nthPrime(10^6)
#> [1] 15485863
## Confirm a prime's position
p <- nthPrime(1000)
primeCount(p)
#> [1] 1000
primeCount(p - 1)
#> [1] 999While nthPrime is convenient for finding individual
primes by position, generating consecutive primes this way repeats the
counting and search work for every prime. A better approach is to find
the two endpoints with nthPrime, then generate the primes
between them with primeSieve:
primes_by_position <- function(first, last) {
sapply(seq.int(first, last), nthPrime)
}
primes_by_interval <- function(first, last) {
lower <- nthPrime(first)
upper <- nthPrime(last)
primeSieve(lower, upper)
}
first <- 1e9
last <- first + 1000
system.time(by_position <- primes_by_position(first, last))
#> user system elapsed
#> 2.691 0.003 2.693
system.time(by_interval <- primes_by_interval(first, last))
#> user system elapsed
#> 0.007 0.000 0.007
identical(by_position, by_interval)
#> [1] TRUEOther Sieving Functions
RcppAlgos comes equipped with several functions for
quickly generating essential components for problems common in
computational mathematics. All functions below can be executed in
parallel by using the argument nThreads.
The following sieving functions (primeFactorizeSieve,
divisorsSieve, numDivisorSieve, &
eulerPhiSieve) are very useful and flexible. Generate
components up to a number or between two bounds.
## get the number of divisors for every number from 1 to n
numDivisorSieve(20)
#> [1] 1 2 2 3 2 4 2 4 3 4 2 6 2 4 4 5 2 6 2 6
## If you want the complete factorization from 1 to n, use divisorsList
system.time(allFacs <- divisorsSieve(10^5, namedList = TRUE))
#> user system elapsed
#> 0.019 0.005 0.024
allFacs[c(4339, 15613, 22080)]
#> $`4339`
#> [1] 1 4339
#>
#> $`15613`
#> [1] 1 13 1201 15613
#>
#> $`22080`
#> [1] 1 2 3 4 5 6 8 10 12 15 16 20 23 24
#> [15] 30 32 40 46 48 60 64 69 80 92 96 115 120 138
#> [29] 160 184 192 230 240 276 320 345 368 460 480 552 690 736
#> [43] 920 960 1104 1380 1472 1840 2208 2760 3680 4416 5520 7360 11040 22080
## Between two bounds
primeFactorizeSieve(10^12, 10^12 + 5)
#> [[1]]
#> [1] 2 2 2 2 2 2 2 2 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 5
#>
#> [[2]]
#> [1] 73 137 99990001
#>
#> [[3]]
#> [1] 2 3 166666666667
#>
#> [[4]]
#> [1] 61 14221 1152763
#>
#> [[5]]
#> [1] 2 2 17 149 197 501001
#>
#> [[6]]
#> [1] 3 5 66666666667
## Creating a named object
eulerPhiSieve(20, namedVector = TRUE)
#> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#> 1 1 2 2 4 2 6 4 6 4 10 4 12 6 8 8 16 6 18 8
system.time(a <- eulerPhiSieve(1e12, 1e12 + 1e7))
#> user system elapsed
#> 0.216 0.007 0.223
## Using nThreads for greater efficiency
system.time(b <- eulerPhiSieve(1e12, 1e12 + 1e7, nThreads = 8))
#> user system elapsed
#> 0.745 0.036 0.108
identical(a, b)
#> [1] TRUEVectorized Functions
There are three very fast vectorized functions for general factoring
(e.g. all divisors of number), primality testing, as well as prime
factoring (divisorsRcpp, isPrimeRcpp,
primeFactorize).
## get result for individual numbers
primeFactorize(123456789)
#> [1] 3 3 3607 3803
## or for an entire vector
## N.B. The R Version you are using... random sampling
## has changed throughout the years
R.version[["version.string"]]
#> [1] "R version 4.6.1 (2026-06-24)"
set.seed(100)
myVec <- sample(-100000000:100000000, 5)
divisorsRcpp(myVec, namedList = TRUE)
#> $`33331928`
#> [1] 1 2 4 7 8 14 19 28 38
#> [10] 56 76 133 152 266 532 1064 31327 62654
#> [19] 125308 219289 250616 438578 595213 877156 1190426 1754312 2380852
#> [28] 4166491 4761704 8332982 16665964 33331928
#>
#> $`99961494`
#> [1] 1 2 3 6 37 74 111 222 450277
#> [10] 900554 1350831 2701662 16660249 33320498 49980747 99961494
#>
#> $`30377219`
#> [1] 1 19 1598801 30377219
#>
#> $`-46085563`
#> [1] -46085563 -201247 -229 -1 1 229 201247 46085563
#>
#> $`-26510714`
#> [1] -26510714 -13255357 -73846 -36923 -718 -359 -2 -1
#> [9] 1 2 359 718 36923 73846 13255357 26510714
## Creating a named object
isPrimeRcpp(995:1000, namedVector = TRUE)
#> 995 996 997 998 999 1000
#> FALSE FALSE TRUE FALSE FALSE FALSE
system.time(a <- primeFactorize(1e12:(1e12 + 1e5)))
#> user system elapsed
#> 0.497 0.006 0.503
## Using nThreads for greater efficiency
system.time(b <- primeFactorize(1e12:(1e12 + 1e5), nThreads = 8))
#> user system elapsed
#> 0.698 0.006 0.112
identical(a, b)
#> [1] TRUE