Part 1

Write a loop that calculates 12-factorial.

input <- 12 # Stores number for which factorial should be calculated
series <- 1:input # Creates vector of integers from 1 to said number
for (i in series){
  if (i == 1){
    #We're at the beginning, so we start computing
    cur <- series[i] * series[i + 1]
  }else if(i == length(series)){
    #We're at the end, so we print the results
    print(cur)
    break
  }else{
    #We're in the middle, so we keep computing from our previous step
    cur <- cur * series[i + 1]
  }
}
## [1] 479001600

Part 2

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

vec <- seq(from = 20, to = 50, by = 5)
print(vec)
## [1] 20 25 30 35 40 45 50

Part 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){
    print("The value for a cannot be zero.")
  }else{
    sol1 <- (-1 * b + sqrt(b^2 - 4 * a * c)) / (2 * a)
    sol2 <- (-1 * b - sqrt(b^2 - 4 * a * c)) / (2 * a)
    print("Solutions for x:")
    print(sol1)
    print(sol2)
  }
}

quadratic(3, -5, 2) #Example for quadratic equation: 3x^2 - 5x + 2 = 0
## [1] "Solutions for x:"
## [1] 1
## [1] 0.6666667