| 1 |
####---- Prime numbers, factorization, etc. --- "illustration of programming" |
| 2 |
####---- A Collection of pure S / R -- Experiments from the 1990's |
| 3 |
####---- mostly carried by discussions on the good old S-news mailing list |
| 4 |
|
| 5 |
### Mostly using the functions currently hidden in sfsmisc namespace |
| 6 |
### FIXME: ---> Move these function definitons to ../inst/ ---> see ../TODO (10) |
| 7 |
factorizeBV <- sfsmisc:::factorizeBV |
| 8 |
primes. <- sfsmisc:::primes. |
| 9 |
test.factorize <- sfsmisc:::test.factorize |
| 10 |
prime.sieve <- sfsmisc:::prime.sieve |
| 11 |
factors <- sfsmisc:::factors |
| 12 |
|
| 13 |
## |
| 14 |
factorizeBV(6) |
| 15 |
##[1] 2 3 |
| 16 |
str( factorizeBV(4:8) ) |
| 17 |
|
| 18 |
|
| 19 |
### 1) The super speedy primes() function from Bill Venables |
| 20 |
### {and improved by M.Maechler}: |
| 21 |
|
| 22 |
## on a Pentium 4 2.80 GHz with 2 GB RAM ; |
| 23 |
|
| 24 |
N <- 1e7 |
| 25 |
|
| 26 |
## keep this working for S+ ! compatible |
| 27 |
for(i in 1:3) print(system.time(p7 <- primes.(N))[1:3]) ## Bill Venables' original |
| 28 |
##- [1] 3.86 1.93 8.75 |
| 29 |
##- [1] 4.02 1.60 11.34 |
| 30 |
##- [1] 4.14 1.60 11.51 |
| 31 |
## about 10-20% slower on 'lynne' |
| 32 |
|
| 33 |
for(i in 1:3) print(system.time(p7. <- primes(N))[1:3]) ## Martin Maechler's improvement |
| 34 |
##- [1] 2.29 0.76 6.47 |
| 35 |
##- [1] 2.58 0.73 6.67 |
| 36 |
##- [1] 2.71 0.59 6.64 |
| 37 |
stopifnot(p7 == p7.) |
| 38 |
|
| 39 |
## On 'lynne' (AMD Athlon 64bit 2800+, 1G RAM), speedup somewhat similar; |
| 40 |
## Also here |
| 41 |
system.time(for(i in 1:50) p5 <- primes (1e5))[1:3] |
| 42 |
system.time(for(i in 1:50) p5. <- primes.(1e5))[1:3] |
| 43 |
stopifnot(p5 == p5.) |
| 44 |
|
| 45 |
|
| 46 |
## 2) |
| 47 |
|
| 48 |
factorize(n <- c(7,27,37,5*c(1:5, 8, 10))) |
| 49 |
factorize(47) |
| 50 |
factorize(7207619)## quick ! |
| 51 |
factorize(131301607)# prime -> still only 0.02 seconds (on lynne)! |
| 52 |
|
| 53 |
## Factorizing larger than max.int -- not prime; |
| 54 |
## should be much quicker with other algo (2nd largest prime == 71) !! |
| 55 |
factorize(76299312910) |
| 56 |
|
| 57 |
system.time(fac.1ex <- factorize(1000 + 1:99)) #-- 0.95 sec (sophie Sparc 5) |
| 58 |
#-- 0.02 sec (P 4, 1.6GHz); 0.4 / .65 sec (florence Ultra 1/170) |
| 59 |
system.time(fac.2ex <- factorize(10000 + 1:999)) |
| 60 |
## R 0.49 : 5.4 sec (florence Ultra 1/170) |
| 61 |
## ------ 6.1 sec (sophie Ultra 1/140) |
| 62 |
## R 0.50-: ~ 3.5 sec (sophie ..........) <<< FASTER ! |
| 63 |
## ------ |
| 64 |
|
| 65 |
## This really used to take time -- no longer w/ current factorize() in 2004 ! |
| 66 |
system.time(factorize.10000 <- factorize(1:10000)) |
| 67 |
## sophie: Sparc 5 (..) :lots of swapping after while, >= 20 minutes CPU; |
| 68 |
## then using less and less CPU, ..more swapping ==> KILL |
| 69 |
## florence (hypersparc): [1] 1038.90 5.09 1349. ( 17 min. CPU) |
| 70 |
## lynne (Ultra-1): [1] 658.77 0.90 677. |
| 71 |
## lynne (Pentium 4): [1] 2.43 0.16 2.68 |
| 72 |
## helen (Pentium 4), R1.9.1: 1.02 0.01 1.04 |
| 73 |
## lynne (64b,2800+), R2.0.1: 0.86 0.00 0.86 |
| 74 |
|
| 75 |
object.size(factorize.10000) #--> 3027743 now (R 1.5.1) 3188928; |
| 76 |
# '* 2' for 64-bit |
| 77 |
###--- test |
| 78 |
test.factorize(fac.1ex[1:10]) #-- T T T .. |
| 79 |
which(!test.factorize(fac.1ex)) |
| 80 |
which(!test.factorize(factorize(8000 + 1:1000))) |
| 81 |
|
| 82 |
|
| 83 |
prime.sieve(prime.sieve()) |
| 84 |
system.time(P1e4 <- prime.sieve(prime.sieve(prime.sieve()), max=10000)) |
| 85 |
##-> 1.45 (on sophie: fast Sparc 5 ..) |
| 86 |
##-> ~0.8 (on jessica: Ultra-2) |
| 87 |
##-> 0.08 (on lynne, Pentium 4 (1600 MHz)) |
| 88 |
##----> see below for a sample of 20 ! |
| 89 |
stopifnot(length(P1e4) == 1229) |
| 90 |
|
| 91 |
CPU.p1e4 <- numeric(20) |
| 92 |
for(i in 1:20) CPU.p1e4[i] <- |
| 93 |
system.time(P1e4 <- prime.sieve(prime.sieve(prime.sieve()), max=10000))[1] |
| 94 |
CPU.p1e4 |
| 95 |
summary(CPU.p1e4) |
| 96 |
##-Ultra-2 Min. 1st Qu. Median Mean 3rd Qu. Max. |
| 97 |
##-Ultra-2 0.690 0.690 0.790 0.755 0.800 0.810 |
| 98 |
## P4 R-?) 0.070 0.070 0.080 0.078 0.080 0.100 |
| 99 |
## P4 R-1.9 0.040 0.050 0.050 0.048 0.050 0.050 |
| 100 |
system.time(P1e4.2 <- prime.sieve( max=10000)) |
| 101 |
##-> 1.46 (sophie) maybe a little longer |
| 102 |
stopifnot(identical(P1e4 , P1e4.2)) |
| 103 |
|
| 104 |
system.time(P1e5 <- prime.sieve(P1e4, max=1e5)) ## note! primes() is faster! |
| 105 |
##-> 105.7 (on sophie: fast Sparc 5) |
| 106 |
##-> 58.83 (on jessica: Ultra2) |
| 107 |
##-> 5.67 (on lynne: Pentium 4) |
| 108 |
##-> 3.96 (on lynne: Pentium 4 -- R 1.9) |
| 109 |
##-> 1.37 (on lynne: AMD 64 -- R 2.0.1) |
| 110 |
|
| 111 |
stopifnot(p5 == P1e5, |
| 112 |
length(P1e5) == 9592) |
| 113 |
|
| 114 |
P1000 <- prime.sieve(max=1000) |
| 115 |
|
| 116 |
plot(P1000, seq(P1000), type='b', main="Prime number theorem") |
| 117 |
lines(P1000, P1000/log(P1000), col=2, lty=2, lwd=1.5) |
| 118 |
|
| 119 |
plot(P1e4, seq(P1e4), type='l', main="Prime number theorem") |
| 120 |
lines(P1e4, P1e4/log(P1e4), col=2, lty=2, lwd=1.5) |
| 121 |
|
| 122 |
stopifnot(require("sfsmisc")) |
| 123 |
## For a nice plot: |
| 124 |
ps.do("prime-number.ps") |
| 125 |
mult.fig(2, main="Prime number theorem") |
| 126 |
plot(P1e5,seq(P1e5), type='l', main="pi(n) & n/log(n) ", |
| 127 |
xlab='n',ylab='pi(n)', log='xy', sub = 'log - log - scale') |
| 128 |
lines(P1e5, P1e5/log(P1e5), col=2, lty=2, lwd=1.5) |
| 129 |
mtext("demo(\"prime-numbers\", package = \"sfsmisc\")", |
| 130 |
side = 3, cex=.75, adj=1, line=3, outer=TRUE) |
| 131 |
plot(P1e5, seq(P1e5) / (P1e5/log(P1e5)), type='b', pch='.', |
| 132 |
main= "Prime number theorem : pi(n) / {n/log(n)}", ylim =c(1,1.3), |
| 133 |
xlab = 'n', ylab='pi(n) / (n/log(n)', log='x') |
| 134 |
abline(h=1, col=3, lty=2) |
| 135 |
ps.end() |
| 136 |
|
| 137 |
|
| 138 |
## 3) the factors() from Bill Dunlap etc |
| 139 |
factors( round(gamma(13:14))) |
| 140 |
##- $"479001600": |
| 141 |
##- [1] 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 5 5 7 11 |
| 142 |
##- |
| 143 |
##- $"6227020800": |
| 144 |
##- [1] 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 5 5 7 11 13 |
| 145 |
|
| 146 |
|
| 147 |
## --- You can use table() to collect repeated factors : ---- |
| 148 |
|
| 149 |
lapply( factors( round(gamma(13:14))), table) |
| 150 |
##- $"479001600": |
| 151 |
##- 2 3 5 7 11 |
| 152 |
##- 10 5 2 1 1 |
| 153 |
##- |
| 154 |
##- $"6227020800": |
| 155 |
##- 2 3 5 7 11 13 |
| 156 |
##- 10 5 2 1 1 1 |
| 157 |
|