#RBridge Week 1 Assignment

#1.Write a loop that calculates 12-factorial

factorial <- function(i){
  x1=1;

  for (i in 1:i) {
    x1 = x1*i
}
    return(x1)
}
factorial(12)
## [1] 479001600
#Bonus 1 if you wanted to find factorial for 1-12
p1 <- c(1:12)
for (p in p1){
  p <- factorial(p)
  print(p)
}
## [1] 1
## [1] 2
## [1] 6
## [1] 24
## [1] 120
## [1] 720
## [1] 5040
## [1] 40320
## [1] 362880
## [1] 3628800
## [1] 39916800
## [1] 479001600

#2. Create a Numeric vector that contains the sequence from 20 to 50 by 5.

VectorNum <- seq(20,50,5)
print(VectorNum)
## [1] 20 25 30 35 40 45 50
typeof(VectorNum)
## [1] "double"

#2-Explanation: Number Vectors are described as Integer or Double Vectors

#3. Create a Quadratic Function that takes inputs for A,B,C and solves the quadratic formula should output 2 roots.

quadratic <- function(a, b, c) {
  
  print(paste("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
  discr <- (b^2) - (4*a*c)
  
  if(discr < 0) {
    return(paste("This quadratic equation has no real numbered roots."))
  }
  else if(discr > 0) {
    x_pos <- (-b + sqrt(discr)) / (2*a)
    x_neg <- (-b - sqrt(discr)) / (2*a)
    
    return(paste("The two x-intercepts for the quadratic equation are ",
                 format(round(x_pos, 3), nsmall = 3), " and ",
                 format(round(x_neg, 3), nsmall = 3), "."))
  }
  else #discriminant is 0
    x_int <- (-b) / (2*a)
  return(paste("The quadratic equation has only one root. This root is ",
               x_int))
 
}
#2 roots
quadratic(3,78,-41)
## [1] "You have chosen the quadratic equation  3 x^2 +  78 x +  -41 ."
## [1] "The two x-intercepts for the quadratic equation are  0.515  and  -26.515 ."
#1 root
quadratic(1,6,+9)
## [1] "You have chosen the quadratic equation  1 x^2 +  6 x +  9 ."
## [1] "The quadratic equation has only one root. This root is  -3"
#No real Root
quadratic(1,1,1) 
## [1] "You have chosen the quadratic equation  1 x^2 +  1 x +  1 ."
## [1] "This quadratic equation has no real numbered roots."