primes: Prime Numbers

Description Usage Arguments Details Value See Also Examples

View source: R/primes.R

Description

Generate a list of prime numbers less or equal n, resp. between n1 and n2.

Usage

1
  primes(n)

Arguments

n

nonnegative integer greater than 1.

Details

The list of prime numbers up to n is generated using the "sieve of Erasthostenes". This approach is reasonably fast, but may require a lot of main memory when n is large.

In double precision arithmetic integers are represented exactly only up to 2^53 - 1, therefore this is the maximal allowed value.

Value

vector of integers representing prime numbers

See Also

isprime, factors

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
primes(1000)
## Not run: 
##  Appendix:  Logarithmic Integrals and Prime Numbers (C.F.Gauss, 1846)

library('gsl')
# 'European' form of the logarithmic integral
Li <- function(x) expint_Ei(log(x)) - expint_Ei(log(2))

# No. of primes and logarithmic integral for 10^i, i=1..12
i <- 1:12;  N <- 10^i
# piN <- numeric(12)
# for (i in 1:12) piN[i] <- length(primes(10^i))
piN <- c(4, 25, 168, 1229, 9592, 78498, 664579,
         5761455, 50847534, 455052511, 4118054813, 37607912018)
cbind(i, piN, round(Li(N)), round((Li(N)-piN)/piN, 6))

#  i     pi(10^i)      Li(10^i)  rel.err  
# --------------------------------------      
#  1            4            5  0.280109
#  2           25           29  0.163239
#  3          168          177  0.050979
#  4         1229         1245  0.013094
#  5         9592         9629  0.003833
#  6        78498        78627  0.001637
#  7       664579       664917  0.000509
#  8      5761455      5762208  0.000131
#  9     50847534     50849234  0.000033
# 10    455052511    455055614  0.000007
# 11   4118054813   4118066400  0.000003
# 12  37607912018  37607950280  0.000001
# --------------------------------------
## End(Not run)

Example output

  [1]   2   3   5   7  11  13  17  19  23  29  31  37  41  43  47  53  59  61
 [19]  67  71  73  79  83  89  97 101 103 107 109 113 127 131 137 139 149 151
 [37] 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251
 [55] 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359
 [73] 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
 [91] 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593
[109] 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701
[127] 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827
[145] 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953
[163] 967 971 977 983 991 997

Attaching package: 'gsl'

The following objects are masked from 'package:pracma':

    erf, erfc, eta, expint_E1, expint_Ei, fact, hypot, psi, sinc, zeta

       i         piN                     
 [1,]  1           4           5 0.280109
 [2,]  2          25          29 0.163239
 [3,]  3         168         177 0.050979
 [4,]  4        1229        1245 0.013094
 [5,]  5        9592        9629 0.003833
 [6,]  6       78498       78627 0.001637
 [7,]  7      664579      664917 0.000509
 [8,]  8     5761455     5762208 0.000131
 [9,]  9    50847534    50849234 0.000033
[10,] 10   455052511   455055614 0.000007
[11,] 11  4118054813  4118066400 0.000003
[12,] 12 37607912018 37607950280 0.000001

pracma documentation built on Jan. 30, 2018, 3:01 a.m.