res <- 1 # predefine vector to concatenate result into res
for (i in 1:12) {
if(i == 1) res <- 1
else {
res <- res * i
}
cat(i, "!:", res, "\n")
}
## 1 !: 1
## 2 !: 2
## 3 !: 6
## 4 !: 24
## 5 !: 120
## 6 !: 720
## 7 !: 5040
## 8 !: 40320
## 9 !: 362880
## 10 !: 3628800
## 11 !: 39916800
## 12 !: 479001600
n_vec <- seq(20, 50, 5)
print(n_vec)
## [1] 20 25 30 35 40 45 50
is.vector(n_vec)
## [1] TRUE
q_func <- function(a,b,c){
if (a == 0) {
print("equation not quadratic")
}
q_delta <- (b^2 - 4*a*c)
if(q_delta > 0) {
sol1 <- (-b + sqrt(q_delta)) / (2 * a)
sol2 <- (-b - sqrt(q_delta)) / (2 * a)
return(paste("equation has 2 solutions as follows:", sol1, ",", sol2))
} else if (q_delta == 0){
sol1 <- -b / (2*a)
return (paste("equation has 1 solution:", sol1))
} else {
sol1 <- complex(real = -b / (2 * a), imaginary = sqrt(-q_delta) / (2 * a))
sol2 <- complex(real = -b / (2 * a), imaginary = - sqrt(-q_delta) / (2 * a))
return(paste("equation has 2 solutions as follows:",sol1, ",", sol2))
}
}
q_func(1,-3,-5)
## [1] "equation has 2 solutions as follows: 4.19258240356725 , -1.19258240356725"
q_func(1,3,5)
## [1] "equation has 2 solutions as follows: -1.5+1.6583123951777i , -1.5-1.6583123951777i"
q_func(0,3,5)
## [1] "equation not quadratic"
## [1] "equation has 2 solutions as follows: NaN , -Inf"