- Write a loop that calculates 12-factorial
find_factorial <- function(n = 0) {
if (n < 0) return(-1)
if (n == 0) return(1)
my_factorial <- 1
for (i in 1:n) {
my_factorial <- my_factorial * i
}
return(my_factorial)
}
find_factorial(12)
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from
20 to 50 by 5.
five_vector <- c(seq.int(20, 50, by = 5))
five_vector
## [1] 20 25 30 35 40 45 50
- Create the function “quad” that takes a trio of input numbers a, b,
and c and solve the quadratic equation. The function should print as
output the two solutions.
quad <- function(a, b, c) {
discriminant <- b^2 - 4*a*c
if (discriminant < 0) {
return("No real solutions")
} else {
sol1 <- (-b + sqrt(discriminant)) / (2*a)
sol2 <- (-b - sqrt(discriminant)) / (2*a)
return(c(sol1, sol2))
}
}
quad(1, 2, 1)
## [1] -1 -1
quad(1, 6, 5)
## [1] -1 -5
quad(1, 1, 1)
## [1] "No real solutions"