count1 <- function(m){
count <- 0
for (i in 1:m)
count <- count + 1
return(count)}
count1(10)
[1] 10
count2 <- function(m) {
count <- 0
for (i in 1:m) {
count <- count + 1
cat("i = ",count,",","count = ",count,"\n")
}
}
count2 <- function(m){
count <- 0
for (i in 1:m){
count <- count + 1
cat("i = ",count,",","count = ",count,"\n")}
}
count2(10)
i = 1 , count = 1
i = 2 , count = 2
i = 3 , count = 3
i = 4 , count = 4
i = 5 , count = 5
i = 6 , count = 6
i = 7 , count = 7
i = 8 , count = 8
i = 9 , count = 9
i = 10 , count = 10
count3 <- function(m,n) {
count <- 0
for (i in 1:m) {
for (j in 1:n) {
count <- count + 1 }}
return(count)}
count3(3,2)
[1] 6
count4 <- function(m,n) {
count <- 0
cat("i = ", 0,",","j = ", 0,",", "count = ", count, "\n")
for (i in 1:m) {
for (j in 1:n) {
count <- count + 1
cat("i = ", i,",","j = ", j,",", "count = ", count, "\n")}}
}
count4(3,2)
i = 0 , j = 0 , count = 0
i = 1 , j = 1 , count = 1
i = 1 , j = 2 , count = 2
i = 2 , j = 1 , count = 3
i = 2 , j = 2 , count = 4
i = 3 , j = 1 , count = 5
i = 3 , j = 2 , count = 6
count4(4,3)
i = 0 , j = 0 , count = 0
i = 1 , j = 1 , count = 1
i = 1 , j = 2 , count = 2
i = 1 , j = 3 , count = 3
i = 2 , j = 1 , count = 4
i = 2 , j = 2 , count = 5
i = 2 , j = 3 , count = 6
i = 3 , j = 1 , count = 7
i = 3 , j = 2 , count = 8
i = 3 , j = 3 , count = 9
i = 4 , j = 1 , count = 10
i = 4 , j = 2 , count = 11
i = 4 , j = 3 , count = 12
count5 <- function(m) {
count <- 0
for (i in 1:m) {
for (j in 1:i) {
count <- count + 1 }}
return(count)}
count5(3)
[1] 6
count6 <- function(m) {
count <- 0
cat("i = ", 0,",","j = ", 0,",", "count = ", count, "\n")
for (i in 1:m) {
for (j in 1:i) {
count <- count + 1
cat("i = ", i,",","j = ", j,",", "count = ", count, "\n")}}
}
count6(3)
i = 0 , j = 0 , count = 0
i = 1 , j = 1 , count = 1
i = 2 , j = 1 , count = 2
i = 2 , j = 2 , count = 3
i = 3 , j = 1 , count = 4
i = 3 , j = 2 , count = 5
i = 3 , j = 3 , count = 6
count6(4)
i = 0 , j = 0 , count = 0
i = 1 , j = 1 , count = 1
i = 2 , j = 1 , count = 2
i = 2 , j = 2 , count = 3
i = 3 , j = 1 , count = 4
i = 3 , j = 2 , count = 5
i = 3 , j = 3 , count = 6
i = 4 , j = 1 , count = 7
i = 4 , j = 2 , count = 8
i = 4 , j = 3 , count = 9
i = 4 , j = 4 , count = 10
\[ 2,3,5,7,11,13,17,... \]
IsPrime <-function(n){
if (n == 2)
return(TRUE)
for (i in 2:sqrt(n))
if (n %% i == 0)
return(FALSE)
return(TRUE) }
c(IsPrime(2), IsPrime(3), IsPrime(4))
[1] TRUE TRUE FALSE
IsPrime <-function(n){
if (n == 2)
return(TRUE)
for (i in 2:sqrt(n))
if (n %% i == 0)
return(FALSE)
return(TRUE) }
sqrt(3)
[1] 1.732051
3 %% 2
[1] 1
IsPrime(3)
[1] TRUE
IsPrime <-function(n){
if (n == 2)
return(TRUE)
for (i in 2:sqrt(n))
if (n %% i == 0)
return(FALSE)
return(TRUE) }
sqrt(11)
[1] 3.316625
c(11 %% 2, 11 %% 3)
[1] 1 2
IsPrime(11)
[1] TRUE
IsPrime <-function(n){
if (n == 2)
return(TRUE)
for (i in 2:sqrt(n))
if (n %% i == 0)
return(FALSE)
return(TRUE) }
sqrt(12)
[1] 3.464102
c(12 %% 2, 12 %% 3)
[1] 0 0
IsPrime(12)
[1] FALSE
IsPrime <-function(n){
if (n == 2)
return(TRUE)
for (i in 2:sqrt(n))
if (n %% i == 0)
return(FALSE)
return(TRUE) }
sqrt(15)
[1] 3.872983
c(15 %% 2, 15 %% 3)
[1] 1 0
IsPrime(15)
[1] FALSE
library(cmna)
isPrime(3)
[1] TRUE
isPrime(4)
[1] FALSE
pi
[1] 3.141593
isPrime(pi)
[1] TRUE
IsPrime <-function(n){
if (n == 2)
return(TRUE)
for (i in 2:sqrt(n))
if (n %% i == 0)
return(FALSE)
return(TRUE) }
VecCount <- function(m) {
count <- 0
x <- 1:100
y <- rep(1,100)
for (i in 1:m) {
y <- y*x
count <- count + 1}
return(count)}
VecCount(10)
[1] 10
(x <- 1:5)
[1] 1 2 3 4 5
(y <- rep(1,5))
[1] 1 1 1 1 1
(y*x)
[1] 1 2 3 4 5
VecCount <- function(m) {
count <- 0
x <- 1:100
y <- rep(1,100)
for (i in 1:m) {
y <- y*x
count <-count+1}
return(count)}
VecCount(10)
[1] 10
VecCount <- function(m) {
count <- 0
c(x <- 1:100, y <- rep(1,100))
for (i in 1:m) {
y <- y*x
count <- count + 1}
return(count)}