- Write a loop that calculates 12-factorial
x = 1
for( i in 1:12) {
x = x * i
}
print(x)
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
seq(20, 50, by = 5)
## [1] 20 25 30 35 40 45 50
- Create the function “quadratic” 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.
quadratic_formula <- function(a, b, c) {
discr <- (b^2) - (4*a*c)
if(discr < 0) {
return(print("The answer is a complex number."))
}
else if(discr > 0) {
x_plus <- (-b + sqrt(discr)) / (2*a)
x_negative <- (-b - sqrt(discr)) / (2*a)
return(paste("The two answers are x = ", x_plus ," and x = ",x_negative, "."))
}
else
x_int <- (-b) / (2*a)
return(paste("There is only one answer x =", x_int))
}
quadratic_formula( 11, 2, 1)
## [1] "The answer is a complex number."