Write a loop that calculates 12-factorial:

twelve.fact <- function()
{
  ans <- 1
  for(i in 12:1)
  {
    ans <- ans * i
  }
  
  return(ans)
}

twelve.fact()
## [1] 479001600

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

We can simply do this by creating a vector containing the required sequence.

basic.vector <- c(20, 25, 30, 35, 40, 45, 50)
class(basic.vector)
## [1] "numeric"

We see the vector is numeric.

Or we can do this using the seq function in R.

basic.vector2 <- seq(20, 50, by = 5)
class(basic.vector2)
## [1] "numeric"

Again, the vector is numeric.

Create the function “factorial” 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.

factorial <- function(a, b, c)
{
  # Check for whether there is a real or imaginary solution
  temp <- b^2 - 4 * a * c
  
  # If temp is positive, the solution is real and proceed as normal
  if (temp > 0)
  {
    positive.x <- (b * (-1) + sqrt(temp)) / (2 * a)
    negative.x <- (b * (-1) - sqrt(temp)) / (2 * a)
    ans <- c(positive.x, negative.x)
    return(ans)
  } else
  
  # Otherwise, temp is negative and we need to coerce temp
  # into becoming a complex number
  {
    positive.x <- (b * (-1) + sqrt(as.complex(temp))) / (2 * a)
    negative.x <- (b * (-1) - sqrt(as.complex(temp))) / (2 * a)
    ans <- c(positive.x, negative.x)
    return(ans)
  }
}

factorial(5, 6, 1)
## [1] -0.2 -1.0