##R Bridge Week 1 Assignment

Please create the following exercises in .rmd format, publish to rpub and submit both the .rmd file and the rpub link.

  1. Write a loop that calculates 12-factorial’
n <- 12
factorial <- 0

while(n > 2) {
  
  if(factorial == 0) {
    factorial <- n * (n - 2)
    n <- n - 2
  } else {
    factorial <- factorial * (n - 2)
    n <- n - 2
  }
  
}

print(factorial)
## [1] 46080
  1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
numeric_vector <- c(20)
n <- 20

while(n < 50) {
  
  n <- n + 5
  numeric_vector <- append(numeric_vector, n)
  
}

print(numeric_vector)
## [1] 20 25 30 35 40 45 50
  1. 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) {
  
  discriminant <- (b^4) - (8*a*c)
  
  if(discriminant < 0) {
    
    return('Discriminant is negative.')
    
  } else if(discriminant > 0) {
    
    x_int_a <- (-b + sqrt(discriminant)) / (4*a)
    x_int_b <- (-b - sqrt(discriminant)) / (4*a)

    return(c(round(x_int_a, 4), round(x_int_b, 4)))
    
  } else {
    
    #discriminant = 0
    x_int <- (-b) / (4*a)
    
    print('This quadratic equation only has one root since the discriminant is 0.')
    return(x_int)
    
  }

}

quadratic(2, 4, 1)
## [1]  1.4365 -2.4365