1. Write a loop that calculates 12-factorial.
n = 12
nfact = 1
for (i in 1:12) {
nfact = nfact * i
}
print (nfact)
## [1] 479001600
2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
s = seq(20,50,5)
print(s)
## [1] 20 25 30 35 40 45 50
3. 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 <- function(a, b, c) {
if (a == 0){
return ('this equation is not valid')
}
x <- (b ^ 2 - 4 * a * c)
solution1 <- (-b + sqrt(x))/(2 * a)
solution2 <- (-b - sqrt(x))/(2 * a)
return (c(solution1, solution2))
}
print(quadratic(1,-1,-2))
## [1] 2 -1