By Md Forhad Akbar
July 19, 2019
factorial_num <- function(n) {
if(n <= 1) {
return(1)
} else {
return(n * factorial_num(n-1))
}
}
factorial_num(12)
## [1] 479001600
vec<-seq(20, 50, by = 5)
vec
## [1] 20 25 30 35 40 45 50
quadratic <- function(a, b, c) {
# Check that the equation is quadratic.
if (a == 0) {
return("Equation is not quadratic")
}
delta <- (b^2 - 4 * a * c)
if (delta > 0) {
# Two real solutions
solution1 <- (-b + sqrt(delta)) / (2 * a)
solution2 <- (-b - sqrt(delta)) / (2 * a)
return(sprintf("Equation has two real solutions: %s and %s", solution1, solution2))
} else if (delta == 0) {
# One real solution
solution1 <- -b / (2 * a)
return(sprintf("Equation has only one solution: %s", solution1))
} else {
# Two complex solutions
solution1 <- complex(real = -b / (2 * a), imaginary = sqrt(-delta) / (2 * a))
solution2 <- complex(real = -b / (2 * a), imaginary = - sqrt(-delta) / (2 * a))
return(sprintf("Equation has two complex solutions: %s and %s", solution1, solution2))
}
}
quadratic(1, -1, -2)
## [1] "Equation has two real solutions: 2 and -1"
quadratic(1, 2, 1)
## [1] "Equation has only one solution: -1"
quadratic(0, 1, 1)
## [1] "Equation is not quadratic"
quadratic(5, 7,-11)
## [1] "Equation has two real solutions: 0.940121946685673 and -2.34012194668567"
quadratic(1, 2, 2)
## [1] "Equation has two complex solutions: -1+1i and -1-1i"