- Write a loop that calculates 12-factorial
result <- 1
num <- 1:12
calc_factorial <- function()
{
for(i in num){
result <- result*num[i]
}
print(result)
}
calc_factorial()
## [1] 479001600
- Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
create_vect <- function(){
vec <- seq(from=20, to=50, by=5)
print(vec)
}
create_vect()
## [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
# calculate discriminant
get_dicr <- function(a,b,c){
b**2-4*a*c
}
#evaluate the quadratic function
factorial <- function(a,b,c){
if(get_dicr(a,b,c) > 0){ # first case D>0
root_1 = (-b+sqrt(get_dicr(a,b,c)))/(2*a)
root_2 = (-b-sqrt(get_dicr(a,b,c)))/(2*a)
paste0("The roots of the equation are: ", root_1, " and ", root_2)
}else if(get_dicr(a,b,c) == 0){
root <- -b/(2*a)
paste0("The given numbers produced a single root: ", root)
}
else {
print("The given numbers do not produce real roots.")}
}
#test for three scenarios: single root, two roots, no real roots
factorial(1,2,1)
## [1] "The given numbers produced a single root: -1"
factorial(3,-4,1)
## [1] "The roots of the equation are: 1 and 0.333333333333333"
factorial(4,-1,3)
## [1] "The given numbers do not produce real roots."