R Bridge Week 1 Assignment

Question One

Write a loop that calculates 12 factorial.

fact<-1
factorials<-1
for(i in 1:12)
{
factorial<-fact*i
factorials<-factorial * factorials
}
print(factorials)
## [1] 479001600

Question Two

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

numvec<-seq(from=20, to =50, by = 5)
class(numvec)
## [1] "numeric"

Question Three

Create the function “factorial” that takes a trio of input numbers a,b,c and solve the quadratic equation. The function should print as output the two solutions.

factorial<-function(a,b,c)
{
discriminant<-b^2-4*a*c
print(discriminant)

#Check to see if discriminant is negative
  if(discriminant < 0)
  {
    print("This equation has no solution in the real number system. ")
  }
  
#Check to see if discriminant is positive
   else if(discriminant > 0)
  {
    Solution_one<- ((-1)*b + sqrt(discriminant))/2*a
     Solution_two<- ((-1)*b - sqrt(discriminant))/2*a
    
         cat("The solutions to the equation are:", Solution_one, "and: ", Solution_two)
  }
#Check to see if discriminant is exactly 0
        else if (discriminant==0)  
  {
     Solution<- ((-1) * b + sqrt(discriminant))/2*a
      
           cat("The solution to the equation is:", Solution)   
  }
  
}

factorial(-6,3,5)
## [1] 129
## The solutions to the equation are: -25.07345 and:  43.07345