1) Write loop that calculates 12 factorial

factorial<- function(x){
t <-1
for  (i in 1:x)
{
  t <- t * ((1:x)[i])
 } 
print(t)
}
factorial(12)
## [1] 479001600

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

t <-as.numeric(c(seq(20,50,5)))
t
## [1] 20 25 30 35 40 45 50

3) 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

I tried to use the chaining method while calculating ‘theta’. Chaining method can be loaded through ‘dplyr’ package.

library(dplyr)
factorial <- function(a,b,c)
{
  theta <- (b*b - (4*a*c)) %>% sqrt() #chaining method
  if ( theta >= 0){
    root1 <- (- b - (theta))/(2*a)
    root2 <- (- b + (theta))/(2*a)
  return(c(root1,root2))
    }
}
factorial(1,4,4)
## [1] -2 -2
factorial(5,6,1)
## [1] -1.0 -0.2