You can also view the output at http://rpubs.com/mehtablocker/cuny_r_bridge_hw_week_1
This loop calculates 12-factorial:
answer <- 1
for (i in seq(12, 1, -1)){
answer <- answer * i
}
print(answer)
## [1] 479001600
Here is a numeric vector that contains the sequence from 20 to 50 by 5:
seq(20, 50, 5)
## [1] 20 25 30 35 40 45 50
A function to solve the quadratic formula:
factorial <- function(a, b, c){
pos_x <- (-1 * b + sqrt(b^2 - 4 * a * c)) / (2 * a)
neg_x <- (-1 * b - sqrt(b^2 - 4 * a * c)) / (2 * a)
c(pos_x, neg_x)
}
factorial(2, 20, 5)
## [1] -0.2565835 -9.7434165
Note this function will return NAs if there are no real roots.
factorial(1, 1, 1)
## Warning in sqrt(b^2 - 4 * a * c): NaNs produced
## Warning in sqrt(b^2 - 4 * a * c): NaNs produced
## [1] NaN NaN