This is a pre-class assignment for Week 3.
This practice is heavily-commented to clarify logics.
Fill in these functions so that they do what is stated in their comments:
This function checks if a number x is divisible by 2.
is.even <- function(x) {
if ((x%%2) == 0) {
return(TRUE)
# return TRUE if x is divisible by 2
} else {
return(FALSE)
}
}
# *** use ifelse function to determine *** ifelse(x %% == 0, T, F)
# Test
is.even(10)
## [1] TRUE
is.even(11)
## [1] FALSE
is.even(-6)
## [1] TRUE
This function checks if a number x is divisible by a number d.
is.divisible <- function(x, d) {
if ((x%%d) == 0) {
return(TRUE)
# return TRUE if x is divisible by d
} else {
return(FALSE)
}
}
# *** use ifelse function to determine *** ifelse(x %% d == 0, T, F)
# Test
is.divisible(10, 2)
## [1] TRUE
is.divisible(10, 3)
## [1] FALSE
is.divisible(-10, 2)
## [1] TRUE
This function checks if a number x is a prime number using trial division.
is.prime <- function(x) {
# check if the input number x is an integer.
if (all.equal(x, as.integer(x)) == TRUE) {
# check if the input number x is equal to 2.
if (x == 2) {
return(TRUE)
# if the input number x is 2, return TRUE.
}
# if not, check if x is greater than 1?
else if (x > 1) {
# check if x can be divided by any number from 2 to sqrt(x).
for (i in 2:floor(sqrt(x))) {
if ((x %% i) == 0) {
return(FALSE)
# return FALSE if x can be divided by (2, sqrt(x)).
}
else {
return(TRUE)
}
}
}
else {
return(FALSE)
# return FALSE if x is not greater than 1.
}
}
else {
return(FALSE)
# return FALSE if x is not an integer.
}
}
# Test
is.prime(2)
## [1] TRUE
is.prime(6)
## [1] FALSE
is.prime(-7.5)
## [1] FALSE
is.prime(17)
## [1] TRUE
is.prime(997)
## [1] TRUE
is.prime(1)
## [1] FALSE
Rewrite is.prime without a loop.
is.prime <- function(x) {
if (all.equal(x, as.integer(x)) == TRUE) {
# check if the input number x is an integer.
if (x > 1) {
# check if the input number x is greater than 1.
if (x == 2) {
# check if the input number x is equal to 2.
return(TRUE)
# return TRUE if the input number x is 2. (2 is a prime.)
} else if (all(x%%c(2:sqrt(x)) != 0)) {
# check if x is not dividable by any of integers from 2 to sqart root x.
return(TRUE)
# return TRUE if x cannot be divided by any of integers specified above.
# this means x can only be divided by 1 and itself.
} else {
return(FALSE)
# return FALSE if x can be divided by any of integers specified above. this
# means x has one or more divisor that is not equal to 1 or itself.
}
} else {
return(FALSE)
# return FALSE if x is not greater than 1.
}
} else {
return(FALSE)
# return FALSE if x is not an integer.
}
}
# Test
is.prime(2)
## [1] TRUE
is.prime(3)
## [1] TRUE
is.prime(17)
## [1] TRUE
is.prime(25)
## [1] FALSE
is.prime(39)
## [1] FALSE
is.prime(991)
## [1] TRUE
Given a natural number n, use a loop to test 1:n for primality.
Return only primes, somehow in a vector.
# Find prime numbers below a natural number n.
primeOnly <- function(n) {
if (n == 1) {
return("There is no prime number.")
} else {
i <- 1
# index
j <- c(2:n)
# vector for results
while (i <= sqrt(n)) {
j <- j[(j%%j[i]) != 0 | j == j[i]]
# rewrite j so that elements in j are numbers that are either not dividable
# by n or equal to n.
i = i + 1
}
print(j)
}
}
# Test
primeOnly(25)
## [1] 2 3 5 7 11 13 17 19 23
primeOnly(2)
## [1] 2
primeOnly(9)
## [1] 2 3 5 7
primeOnly(500)
## [1] 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
## [18] 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139
## [35] 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233
## [52] 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337
## [69] 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
## [86] 443 449 457 461 463 467 479 487 491 499
Create a time (multiplcation) table.
# apply two for loops for cross calculation.
m <- matrix(NA, 10, 10)
for (i in 1:10) {
for (j in 1:10) {
m[i, j] <- i * j
}
}
m
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 2 3 4 5 6 7 8 9 10
## [2,] 2 4 6 8 10 12 14 16 18 20
## [3,] 3 6 9 12 15 18 21 24 27 30
## [4,] 4 8 12 16 20 24 28 32 36 40
## [5,] 5 10 15 20 25 30 35 40 45 50
## [6,] 6 12 18 24 30 36 42 48 54 60
## [7,] 7 14 21 28 35 42 49 56 63 70
## [8,] 8 16 24 32 40 48 56 64 72 80
## [9,] 9 18 27 36 45 54 63 72 81 90
## [10,] 10 20 30 40 50 60 70 80 90 100