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

#initialize variables
numFactorial <- 12 # 12!
factorialResult <- 1 # initialize our output as 1 since 0! = 1

#our n! factorial for loop
for( fact in 1:numFactorial )
{
  nextFact = fact * factorialResult
  factorialResult = nextFact
}
#display our result
cat( '12! = ', factorialResult )
## 12! =  479001600

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

#using the seq() fxn to specify the range from 20 to 50 in steps of 5
numVec <- seq( from = 20, to = 50, by = 5 )
#display our result
cat( 'numVec: ', numVec ) 
## numVec:  20 25 30 35 40 45 50

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

quadratic <- function( a, b, c )
{
  positiveR <- ((-b ) + sqrt( ( b^2 ) - 4*a*c ) )/( 2*a )
  negativeR <- ((-b ) - sqrt( ( b^2 ) - 4*a*c ) )/( 2*a )
  cat( 'The positive root = ', positiveR, "\n" )
  #print( positiveR )
  cat( 'The negative root = ', negativeR )
  #print( negativeR )
}
#test cases for quadratic: 
# moar @ https://www.mesacc.edu/~scotz47781/mat120/notes/quad_formula/quad_formula_practice.html
#quadratic( 1,6,14 )
quadratic( 8,14,-15 )
## The positive root =  0.75 
## The negative root =  -2.5