set result and n to 0
n <- 0
result <- 1
while (n < 12){
n <- n + 1
result <- result * (n)
}
result
## [1] 479001600
verify n ends as 12
n
## [1] 12
calculate expected result
expected <- 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12
check result
result == expected
## [1] TRUE
v <- seq(20,50,5)
v
## [1] 20 25 30 35 40 45 50
factorial <- function(a,b,c){
j1 <- b ^ 2
j2 <- 4 * a * c
if (j1 < j2){return(cat("I\'m sorry, Dave, but I can\'t do that (it\'ll return an NA
because b-sqared minus 4ac will result in a negative and the
square root would be i)"))}
j3 <- sqrt(j1 - j2)
j4 <- 2 * a
k1 <- (a + j3) / j4
k2 <- (a - j3) / j4
cat("the solutions to your quadratic equation are ")
cat(k1)
cat(" and ")
cat(k2)
}
check with variables that should not trigger n/a
factorial(-2,60,800)
## the solutions to your quadratic equation are -24.5 and 25.5
check with variables that should trigger n/a
factorial(3,2,1)
## I'm sorry, Dave, but I can't do that (it'll return an NA
## because b-sqared minus 4ac will result in a negative and the
## square root would be i)