R Bridge Week 1 Assignment

Write a loop that calcualates 12-factorial

Created the loop with all result and the final 12!

factorial = 1
for (i in 1:12){
  factorial = factorial * i
  print (factorial)
}
## [1] 1
## [1] 2
## [1] 6
## [1] 24
## [1] 120
## [1] 720
## [1] 5040
## [1] 40320
## [1] 362880
## [1] 3628800
## [1] 39916800
## [1] 479001600
print(paste(" 12! is equal to", factorial ))
## [1] " 12! is equal to 479001600"

Create a number vector that contains sequence by 20 to 50 by 5

This code creates a sequence and checks to see if the vector is numeric

vecna <- seq(20,50, by = 5)
is.numeric(vecna)
## [1] TRUE
print (vecna)
## [1] 20 25 30 35 40 45 50

Solve the quadratic equation given 3 inputs

The quadratic equation is ax^2 + bx + c = y

This example code uses (1, 7, 5) to solve for y in equation.

The quad equation calculation code is from the site below and checks for the negative and zero cases of the calc. https://dk81.github.io/dkmathstats_site/rmath-quad-formula-r.html

quadratic <- function(a, b, c) {

  print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))

  discriminant <- (b^2) - (4*a*c)

  if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) {
    x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
    x_int_neg <- (-b - sqrt(discriminant)) / (2*a)

    return(paste0("The two x-intercepts for the quadratic equation are ",
                  format(round(x_int_plus, 5), nsmall = 5), " and ",
                  format(round(x_int_neg, 5), nsmall = 5), "."))
  }
  else #discriminant = 0  case
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}

quadratic(1,7,5)
## [1] "You have chosen the quadratic equation 1x^2 + 7x + 5."
## [1] "The two x-intercepts for the quadratic equation are -0.80742 and -6.19258."