This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
my_sum <- 1
for (x in c(1:12)) {
my_sum <- my_sum * x
}
my_sum
## [1] 479001600
start <- 20/5
end <- 50/5
my_vector <- c(start:end*5)
my_vector
## [1] 20 25 30 35 40 45 50
factorial <- function(a, b, c) {
# Takes a quadratic equation, determines if discriminant is positive/negative or 0 and solves for the roots
# args: a, b, c, all type (ints), variables are extracted from quadratic equation ax^2+bx+c=0
# Returns: roots as x_1 &| x_2 type(ints) or prints no roots-----------------------------------
discriminant <- b^2 - (4 * a * c)
if (discriminant > 0) {
x_1 <- ((-b + sqrt(discriminant))/(2 * a))
x_2 <- ((-b - sqrt(discriminant))/(2 * a))
print(x_1)
print(x_2)
# Make list to return multiple values-------------------
roots_list <- list(x_1, x_2)
return (roots_list)
} else if (discriminant==0) {
x_1 <- -b / (2 * a)
return (x_1)
} else {
print("there are no roots")
}
}
# Test-------------------------------------------------
factorial(1, 11, 10)
## [1] -1
## [1] -10
## [[1]]
## [1] -1
##
## [[2]]
## [1] -10