Q1Answer <- 1
for (i in 1:12)
{Q1Answer <- Q1Answer * i}
Q1Answer
## [1] 479001600
2.Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
Q2Answer <- seq(20,50, 5)
Q2Answer
## [1] 20 25 30 35 40 45 50
3.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){
if (a == 0){
Q3Answer <- -c/b
return (Q3Answer)
}
else{
d <- b^2 - 4*a*c
if (d == 0){
Q3Answer <- -b / (2*a)
return (Q3Answer)
}
else if (d > 0){
x1 <- (-b + sqrt(d))/(2*a)
x2 <- (-b - sqrt(d))/(2*a)
Q3Answer <- c(x1,x2)
return (Q3Answer)
}
else{
part1 <- -b / (2*a)
part2 <- sqrt(-d)/(2*a)
x1 = complex(real = part1, imaginary = part2)
x2 = complex(real = part1, imaginary = -part2)
Q3Answer <- c(x1,x2)
return (Q3Answer)
}
}
}
This function returns 1 solution if a = 0 or b^2 - 4ac = 0. For example, for a = 0, b = 1, c =1
factorial(0,1,1)
## [1] -1
This function returns 2 complex numbers solution if b^2 - 4ac < 0. For example, for a = 1, b = 1, c = 2
factorial(1,1,2)
## [1] -0.5+1.322876i -0.5-1.322876i
This function returns 2 real numbers solution if b^2 - 4ac > 0. For example, for a = 1, b = 5, c = 1
factorial(1,5,1)
## [1] -0.2087122 -4.7912878