Write a loop that calculates 12-factorial
y<-1
range <- 1:12
for (i in range){
y<-y*(range)[i]
}
y
## [1] 479001600
Show how to create a numeric vector that contains the sequence from 20 to 50 by 5
a<-seq(20,50,5)
a
## [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.
factorial <- function(a,b,c){
discriminant <- delta(a,b,c)
if(discriminant > 0){ # when D>0
discrSqrt = sqrt(discriminant)
x_1 = (-b+discrSqrt)/(2*a)
x_2 = (-b-discrSqrt)/(2*a)
result = c(x_1,x_2)
}
else if(discriminant == 0){ # when D=0
x = -b/(2*a)
}
else {"There are no real roots."} # when D<0
}
# delta
delta<-function(a,b,c){
b^2-4*a*c
}
result_1 <- factorial(2,-11,5) # two real roots
result_1
## [1] 5.0 0.5
result_2<-factorial(-4,12,-9) # one real roots
result_2
## [1] 1.5
result_3<-factorial(1,-3,4) # no real roots
result_3
## [1] "There are no real roots."