1. Write a loop that calculates 12-factorial
print (paste("This calculates the factorial of the number 12"))
## [1] "This calculates the factorial of the number 12"
num <- 1
my_factorial <- 1
  while (num <= 12) {
    my_factorial <- my_factorial * num
    num <- num + 1   #increments num by 1 
  }

print (paste("The factorial of 12 is", my_factorial))
## [1] "The factorial of 12 is 479001600"
  1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
numvec <- seq(20, 50, 5)   #use sequence (start num, end num, offset)

print (numvec)
## [1] 20 25 30 35 40 45 50

3a. Create the function “quad” 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. Please run and test your answer for (1,2,1), (1,6,5) and (1,1,1)

# solve quadratic equation in the form of ax^2 + bx + c

my_quad_function = function (a,b,c){
  ans1 = -(b+sqrt(b^2-4*a*c))/(2*a)   #ans 1
  ans2 = -(b-sqrt(b^2-4*a*c))/(2*a)   #ans 2
  return(paste0("The answers are ", ans1, " and ", ans2)) 
}

#Scenario 1 where a=1, b=2, c=1
case1 <- my_quad_function(1,2,1)
print(paste0(case1))
## [1] "The answers are -1 and -1"
#Scenario 2 where a=1, b=6, c=5
case2 <-my_quad_function(1,6,5)
print(paste0(case2))
## [1] "The answers are -5 and -1"
#Scenario 3 where a=1, b=1, c=1
case3 <-my_quad_function(1,1,1)
## Warning in sqrt(b^2 - 4 * a * c): NaNs produced

## Warning in sqrt(b^2 - 4 * a * c): NaNs produced
print(paste0(case3))
## [1] "The answers are NaN and NaN"

3b. There are cases when a combination of variables will cause the discriminant, the part of the equation inside the square root that reads “(b^2 - 4ac)”, to return either a negative number which is undefined in a square root, or only one number.

Thus there are three possible outcomes:

In the equation 0 = ax^2 + bx + c:

Scenario 1: If (b^2 - 4ac) > 0, then there are two (2) distinct solutions Scenario 2: If (b^2 - 4ac) = 0, then there is only one (1) possible solution, and Scenario 3: If (b^2 - 4ac) < 0, then there are no possible solutions because a negative value cannot be inside the square rooot.

#Revised quadratic equation function that considers the effects of the discriminant

my_quad_function2 <- function (a,b,c) {
 
#show user the formula with their choices
print(paste0("You are looking to solve the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
  
discriminant <- (b^2) - (4*a*c)

if(discriminant > 0) {
    ans1 <- (-b + sqrt(discriminant)) / (2*a)
    ans2 <- (-b - sqrt(discriminant)) / (2*a)
   
     return(paste0("The two solutions are ", ans1, " and ", ans2, "."))
  }
  else if(discriminant < 0) {
    return(paste0("This quadratic equation has no real numbered roots."))
  }  
  else #discriminant = 0 scenario
    ans3 <- (-b) / (2*a)
    return(paste0("This quadratic equation has only one solution  ", ans3, "."))
}

#Scenario 1 where a=1, b=2, c=1
my_quad_function2(1,2,1)
## [1] "You are looking to solve the quadratic equation 1x^2 + 2x + 1."
## [1] "This quadratic equation has only one solution  -1."
#Scenario 2 where a=1, b=6, c=5
my_quad_function2(1,6,5)
## [1] "You are looking to solve the quadratic equation 1x^2 + 6x + 5."
## [1] "The two solutions are -1 and -5."
#Scenario 3 where a=1, b=1, c=1
my_quad_function2(1,1,1)
## [1] "You are looking to solve the quadratic equation 1x^2 + 1x + 1."
## [1] "This quadratic equation has no real numbered roots."

.