Leticia Salazar

June 25th, 2021

Question 1:

Write a loop that calculates 12-Factorial

factorial <- function (x)
{
y <- 1
  for (i in 1:x) 
    {
      y <- y * ((1:x) [i])
    }
  print (y)  
}

factorial(12)

Question 2:

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

numeric_vector <- seq(20, 50, 5)
numeric_vector

Question 3:

Create a 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

quad_func <- function (a, b, c) {
  if (a == 0) {
    return ("not a valid equation")
  }
  x <- (b ^ 2 - 4 * a * c)
  
  solu1 <- (-b - sqrt(x)) / (2 * a)
  solu2 <- (-b + sqrt(x)) / (2 * a)
  
  return(c(solu1, solu2))
}
    

print(quad_func (-1, 0, 1))
## [1]  1 -1
print(quad_func (1, 1, 0))
## [1] -1  0