Mikhail Broomes

Please create the following exercises in .rmd format, publish to rpub and submit both the .rmd file and the rpub link. Full credit will only be given to those who publish their code AND ANSWERS to the run code on Rpubs. This will happen if you place your code in the grey chunks by starting with an .rmd file NOT a .R file

Question 1

Write a loop that calculates 12-factorial

fact <- 1
for (x in 1:12){
    fact <- fact * x
}
fact
## [1] 479001600

Question 2

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

vector <- seq(from = 20, to = 50, by = 5)
vector
## [1] 20 25 30 35 40 45 50

Question 3

Create the function “quad” 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. Please run and test your answer for (1,2,1), (1,6,5) and (1,1,1).

quad <- function(a,b,c){  
disc <- (b^2 - 4*a*c)
if (disc < 0 )
{
imagine <- sqrt(-disc)/(2*a)
hd <- -b/(2*a)
ans1 <- complex(hd , imagine ) 
ans2 <- complex(hd , -imagine )  
result <- c(ans1,ans2)
print (result)
}

else{
  
ans1 <- (-b + sqrt(b^2 - 4*a*c))/(2*a)
ans2 <- (-b - sqrt(b^2 - 4*a*c))/(2*a)
result <- c(ans1,ans2)
print (result)
}
}
quad(1,2,1)  
## [1] -1 -1
quad(1,6,5)
## [1] -1 -5
quad(1,1,1)
## [1]  0.8660254+0i -0.8660254+0i