- Write a loop that calculates 12-factorial
factorial <- 1
for (i in 1:12)
{
factorial <- i * factorial
}
print(factorial)
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from
20 to 50 by 5
myvector <- c(4:10)
newvector <- myvector * 5
print(newvector)
## [1] 20 25 30 35 40 45 50
- 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)
{
square <- (b**2)-(4*a*c)
first <- (-b+sqrt(square))/(2*a)
second <- (-b-sqrt(square))/(2*a)
if (square <0)
{
return("No valid answers")
} else
{
print(c(first, second))
}
}
quad(1,2,1) # Should return -1 -1
## [1] -1 -1
quad(1,6,5) # Should return -1 -5
## [1] -1 -5
quad(1,1,1) # No valid answer
## Warning in sqrt(square): NaNs produced
## Warning in sqrt(square): NaNs produced
## [1] "No valid answers"