1. Write a loop that calculates 12-factorial.

x <- 12

for (i in 12:2) {
  x <- x * (i - 1)
}
print(sprintf("12! = %s", x))
## [1] "12! = 479001600"

2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.

y <- 5 * c(4:10)
print(y)
## [1] 20 25 30 35 40 45 50

3. Create a function “quadratic” that takes a trio of input numbers a, b, c and solves the quadratic equation. The function should print as output the two solutions.

quadratic.equation <- function(a, b, c) {
  x_1 <- (-b + sqrt((b ^ 2) - (4 * a * c))) / (2 * a)
  x_2 <- (-b - sqrt((b ^ 2) - (4 * a * c))) / (2 * a)
  
  if (x_1  != x_2) {
    cat("The quadratic equation ", a, "x^2 + ", b, "x + ", c, " = 0 has the following solutions:\n")
    print(sprintf("x = %s and x = %s", x_1, x_2))
  } else {
    cat("The quadratic equation ", a, "x^2 + ", b, "x + ", c, " = 0 has the following solution:\n")
    print(sprintf("x = %s", x_1))
  }
}
quadratic.equation(2, 4, 2)
## The quadratic equation  2 x^2 +  4 x +  2  = 0 has the following solution:
## [1] "x = -1"
quadratic.equation(1, 4, 3)
## The quadratic equation  1 x^2 +  4 x +  3  = 0 has the following solutions:
## [1] "x = -1 and x = -3"
quadratic.equation(4, 4, 0)
## The quadratic equation  4 x^2 +  4 x +  0  = 0 has the following solutions:
## [1] "x = 0 and x = -1"