Write a loop that calculates 12 factorial
x <- 1
for(i in 1:12){
x <- x * i
}
x
## [1] 479001600
Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
y <- seq(20, 50, by = 5)
y
## [1] 20 25 30 35 40 45 50
Create the function “quad” that takes a trio of input numbers and solves 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){
print("The discriminant is > 0. There are two real roots: ")
print(((-1 * b) + (sqrt(discriminant)))/(2 * a))
print(((-1 * b) - (sqrt(discriminant)))/(2 * a))
}
else if (discriminant == 0){
print("The discriminant = 0. There is only one real root: ")
print(-1 * b)
}
else {
"The discriminant is < 0. There are two complex roots"
}
}
quad(a = 1, b = 2, c = 1)
## [1] "The discriminant = 0. There is only one real root: "
## [1] -2
quad(a = 1, b = 6, c = 5)
## [1] "The discriminant is > 0. There are two real roots: "
## [1] -1
## [1] -5
quad(a = 1, b = 1, c = 1)
## [1] "The discriminant is < 0. There are two complex roots"