R Bridge Week 1 Assignment 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 2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5. 3. Create the function “factorial” 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
i <- 1
for (i in 1:12) {
Ans <- print(c("Number" = i,"Factorial" = factorial(i)))
Ans
}
## Number Factorial
## 1 1
## Number Factorial
## 2 2
## Number Factorial
## 3 6
## Number Factorial
## 4 24
## Number Factorial
## 5 120
## Number Factorial
## 6 720
## Number Factorial
## 7 5040
## Number Factorial
## 8 40320
## Number Factorial
## 9 362880
## Number Factorial
## 10 3628800
## Number Factorial
## 11 39916800
## Number Factorial
## 12 479001600
r <-seq(20,50,by=5)
r
## [1] 20 25 30 35 40 45 50
quadratic <- function(a,b,c){
output <- (-b + c(-1,1) * sqrt(b^2-4*a*c))/2*a;
output
}
quadratic(1,-2,1)
## [1] 1 1
# x=-b/(2*a) so =0 roots will be 1
quadratic(1,-4,1)
## [1] 0.2679492 3.7320508
quadratic(4,-1,5)
## Warning in sqrt(b^2 - 4 * a * c): NaNs produced
## [1] NaN NaN
#NaN is produced for the last test as a result of the b^2-4*a*c < 0