1. Write a loop that calculates 12-factorial
f <- 1
for (i in c(2:12)) {
f <- f * i
}
f
[1] 479001600
1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
myVec <- as.numeric(seq(20,50,5))
myVec
[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=NULL, b=NULL, c=NULL) {
# Computes the factors of a quadratic equation of the form:
#   ax^2 + bx + c = 0
#
# Args:
#   a, b, c: values from quadratic polynomial
#
# Returns:
#   The median of x

# Check for no input
if(is.null(a) || is.null(b) || is.null(c)) {
stop("Please enter 3 numeric values: a, b, c")
}

# make sure input value a is numeric
if (class(a) != "numeric" && class(a) != "integer") {
stop("Input value, a, needs to be numeric")
}

# make sure input value b is numeric
if (class(b) != "numeric" && class(a) != "integer") {
stop("Input value, a, needs to be numeric")
}

# make sure input value c is numeric
if (class(c) != "numeric" && class(a) != "integer") {
stop("Input value, a, needs to be numeric")
}

# Assume we verified a, c, d are numeric
if(a == 0) {
print("a cannot equal 0")

} else if(b^2 - 4*a*c < 0) {
print("No real roots")

} else {
sol1 <- (-b) + (sqrt(b^2 - 4*a*c) / 2*a )
sol2 <- -b - sqrt(b^2 - 4* a * c) / (2 * a )
print(paste(sol1,sol2))
}
}
factorial(1, 4, 4)
[1] "-4 -4"
factorial(1, 2, 1)
[1] "-2 -2"
factorial(3, 5, 2)
[1] "-3.5 -5.16666666666667"
factorial(3, -5, 2)
[1] "6.5 4.83333333333333"
factorial(3, 2, 1)
[1] "No real roots"
factorial(0, 2, 3)
[1] "a cannot equal 0"
LS0tCnRpdGxlOiAiV2VlayAxIEFzc2lnbiAtIERvbm55IExvZmxhbmQiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCjEuIFdyaXRlIGEgbG9vcCB0aGF0IGNhbGN1bGF0ZXMgMTItZmFjdG9yaWFsCmBgYHtyfQpmIDwtIDEKZm9yIChpIGluIGMoMjoxMikpIHsKICBmIDw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