This is an R Markdown document containing Sean Amato’s work for the week 1 bridge homework.
Question 1: Calculate 12-factorial using a loop
x <- 12
n <- x
while(x > 2)
{
x <- x - 1
n <- n * x
}
print(n)
## [1] 479001600
Question 2: Create a numeric vector from 20 to 50 in increments of 5
# Using a function I created.
create_vector <- function(start, end, increment)
{
vector <- c(start)
current <- start
while(current < end)
{
current <- current + increment
vector <- c(vector, current)
}
return(vector)
}
create_vector(20, 50, 5)
## [1] 20 25 30 35 40 45 50
# Using the built in function seq().
vector <- seq(20,50,5)
print(vector)
## [1] 20 25 30 35 40 45 50
Question 3: Create a function called quad that solves the quadratic equation given coefficients a, b, and c.
quad <- function(a, b, c)
{
solution_1 <- (-b + (b**2 - 4*a*c)**0.5)/(2*a)
solution_2 <- (-b - (b**2 - 4*a*c)**0.5)/(2*a)
return(c(solution_1, solution_2))
}
quad(1,2,1)
## [1] -1 -1
quad(1,6,5)
## [1] -1 -5
quad(1,1,1)
## [1] NaN NaN