This is the solution for problem 1 in the week 1 homework for the r bridge course. It is a loop that finds the value of 12-factorial.

a <- 1
for(i in 1:12) {a <- a * i}
print(paste0("12! = ",a))
## [1] "12! = 479001600"

This is the solution for problem 2 in the week 1 homework for the r bridge course. To generate the sequence from 20 to 50 by 5, it first creates the sequence from 4 to 10 by 1 and then multiplies each element of that sequence by 5.

list1 <- c(4:10)
list2 <- list1*5
print (list2)
## [1] 20 25 30 35 40 45 50

This is the solution for problem 3 in the week 1 homework for the r bridge course. It defines a function quad that takes the values of a, b, and c from a quadratic equation in standard form as the input, determines from the discriminant whether the function has 2, 1, or 0 real solutions, and then outputs the solutions.

quad <- function(a,b,c) {

discriminant <- b ** 2 - 4 * a * c

x1 <- 
  if (discriminant < 0) 
    {complex(real = (-1 * b) / (2 * a), imaginary = sqrt(abs(discriminant)) / (2 * a))}
  else {(-1 * b + sqrt(discriminant)) / (2 * a)}
x2 <- 
  if (discriminant < 0) 
    {complex(real = (-1 * b) / (2 * a), imaginary = -1 * sqrt(abs(discriminant)) / (2 * a))}
  else {(-1 * b - sqrt(discriminant)) / (2 * a)}

if (discriminant > 0) 
  {print(paste0("This equation has two real solutions, x = ", x1, " and x = ", x2))}
if (discriminant == 0) 
  {print(paste0("This equation has one real solution, x = ", x1))}
if (discriminant < 0)
  {print(paste0("This equation has no real solutions. The imaginary solutions are x = ", x1, " and x = ", x2))}
}
quad(1,2,1)
## [1] "This equation has one real solution, x = -1"
quad(1,6,5)
## [1] "This equation has two real solutions, x = -1 and x = -5"
quad(1,1,1)
## [1] "This equation has no real solutions. The imaginary solutions are x = -0.5+0.866025403784439i and x = -0.5-0.866025403784439i"