set.seed(123) dice <- sample(1:6, 4*10000, replace=TRUE) x <- matrix(dice, 4, 1000) ans <- x[1,]+x[2,]+x[3,]+x[4,] # 普通は colSums(x) とする summary(ans)
## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 4.00 12.00 14.00 13.96 16.00 23.00
var(ans)
## [1] 10.53151
sd(ans)
## [1] 3.245228
(freq <- table(ans))
## ans ## 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ## 1 1 5 14 27 43 55 91 96 114 116 102 108 86 57 43 27 7 5 2
barplot(freq, main="出た目の和の分布")
s <- 0 for (i in 1:10000) { if (i %% 3 == 0 || i %% 5 == 0) { s <- s+i } } print(s)
## [1] 23341668
print(sum(1:(10000%/%3))*3+sum(1:(10000%/%5))*5-sum(1:(10000%/%15))*15)
## [1] 23341668
n <- 100 a <- numeric(n) a[1] <- a[2] <- 1 for (i in 3:n) { a[i] <- a[i-2]+a[i-1] } a <- a[seq(1, n, by=2)] sum(a[a < 10^10])
## [1] 12586269025
euclid <- function(m, n) { while ((temp <- n %% m) != 0) { n <- m m <- temp } return(m) } n <- 1; for (i in 1:25) { n <- n/euclid(n, i)*i } print(n)
## [1] 26771144400
prod(c(16,9,25,7,11,13,17,19,23))
## [1] 26771144400
n <- 500 s <- s2 <- 0 for (i in 1:n) { s <- s+i s2 <- s2+i^2 } print(s^2-s2)
## [1] 15645770750
print((n*(n+1)/2)^2 - n*(n+1)*(2*n+1)/6)
## [1] 15645770750
p <- numeric(1001) p[1] <- 2 p[2] <- 3 m <- 2 n <- 3 repeat { n <- n+2 is.prime <- TRUE for (i in 1:m) { if (p[i]^2 > n) { break } else if (n %% p[i] == 0) { is.prime <- FALSE break } } if (is.prime) { m <- m+1 if (m == 1001) break p[m] <- n } } print(n)
## [1] 7927
library(gmp) s <- 0 n <- 1 repeat { s <- s+1 n <- nextprime(n) if (s == 1001) { break } } print(n)
## Big Integer ('bigz') : ## [1] 7927
mx <- 100000 tbl <- 1:mx tbl[1] <- 0 for (i in 2:floor(sqrt(mx))) { if (tbl[i]) { mx2 <- mx %/% i tbl[2:mx2*i] <- 0 } } prime <- tbl[tbl > 0] length(prime) # 総数
## [1] 9592
head(prime) # 最初の 6 個
## [1] 2 3 5 7 11 13
tail(prime) # 最後の 6 個
## [1] 99923 99929 99961 99971 99989 99991
for (i in 10:99) { for (j in 0:99) { if (i*100+j == i^2+j^2) { cat(i, j, "\n") } } }
## 12 33 ## 88 33