R Bridge Week 1 Assignment

Please create the following exercises in .rmd format, publish to rpub and submit both the .rmd file and the rpub link.

  1. Write a loop that calculates 12-factorial
factorial_num <- function(n) {
  if(n <= 1) {
    return(1)
  } else { 
    return(n * factorial_num(n-1))
  }
}

factorial_num(12)
## [1] 479001600
  1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
vec<-seq(20, 50, by = 5)
vec
## [1] 20 25 30 35 40 45 50
  1. 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.
# Constructing Quadratic Formula
factorial_ <- function(a,b,c){
  if(discriminant(a,b,c) > 0){ # first case Discriminant>0
    x_1 = (-b+sqrt(discriminant(a,b,c)))/(2*a)
    x_2 = (-b-sqrt(discriminant(a,b,c)))/(2*a)
    result = c(x_1,x_2)
    
    
  }
  else if(discriminant(a,b,c) == 0){ # second case Discriminant=0
    x = -b/(2*a)
  }
  else {"There are no real roots."} # third case Discriminant<0
}

# Constructing discrimant to determine roots
discriminant<-function(a,b,c){
  b^2-4*a*c
}

For the case of Discriminant >0

c<-factorial_(1,-2,-3)
c
## [1]  3 -1

For the case of Discriminant =0

e<-factorial_(1,1,1)
e
## [1] "There are no real roots."

For the case of Discriminant <0

f<-factorial_(1,-2,1)
f
## [1] 1