Please create the following exercises in .rmd format, publish to rpub and submit both the .rmd file and the rpub link. 1. Write a loop that calculates 12-factorial
factorial <-1
for (x in 1:12){
# browser()
if(x == 1) factorial <- 1
else {
factorial <- factorial * x
}
cat(x, "!:", factorial, "\n")
}
## 1 !: 1
## 2 !: 2
## 3 !: 6
## 4 !: 24
## 5 !: 120
## 6 !: 720
## 7 !: 5040
## 8 !: 40320
## 9 !: 362880
## 10 !: 3628800
## 11 !: 39916800
## 12 !: 479001600
numeric_vector <- c(20)
n <- 20
while(n < 50) {
n <- n + 5
numeric_vector <- append(numeric_vector, n)
}
print(numeric_vector)
## [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 ("the quadrtic equation has one root")
}
x <- (b ^ 2-4 * a * c)
answer1 <- (-b + sqrt(x))/(2*a)
answer2 <- (-b - sqrt(x))/(2*a)
return (c(answer1, answer2))
}
print(quadratic(2,4,1))
## [1] -0.2928932 -1.7071068