#1. Write a loop that calculates 12-factorial.
n = 1
for (i in 1:12) {
n <- n * i
}
print(n)
## [1] 479001600
#2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
vec <- seq(20, 50, by = 5)
print(vec)
## [1] 20 25 30 35 40 45 50
#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.
dis <- function(a, b, c) b^2 - 4*a*c
#If discriminant is negative, there are no real roots.
factorial <- function(a, b, c)
{
if(dis( a, b, c) >= 0) {
sol1 <- (-b + sqrt(dis(a, b, c))) / (2*a)
sol2 <- (-b - sqrt(dis(a, b, c))) / (2*a)
print(unique(c(sol1, sol2)))
}
else {
print("There are no real roots.")
}
}
factorial(1, -2, 1)
## [1] 1
factorial(1, -4, 3)
## [1] 3 1
factorial(4, -1, 5)
## [1] "There are no real roots."