- Write a loop that calculates 12-factorial
n <- 1
for (m in 1:12) {
n = n*m
}
print(n)
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
n <- c(NULL)
for (i in seq(20,50,5)) {
n <- c(n, i)
}
print(n)
## [1] 20 25 30 35 40 45 50
- 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.
factorial <- function(a,b,c) {
n <- ((-b + sqrt(b**2-4*a*c))/(2*a))
m <- ((-b - sqrt(b**2-4*a*c))/(2*a))
print(n)
print(m)
}
factorial(2,-1,-1)
## [1] 1
## [1] -0.5