R Bridge Week 1 Assignment

Question 1:

Write a loop that calculates 12-factorial.

Answer 1:

Factorial of a number is the product of integers from 1 to the number. In this case 12-factorial (12!) is; \(12!=12*11*10*9*8*7*6*5*4*3*2*1\)

x <- 12 # x is the variable which is 12 as per the question. 
y <- 1 # y is the variable that is the first value; which is 1. 
# Below loop will start from 1 and loops until 12.
for (i in 1:x){
  y <- y * (i)
}
print(y)
## [1] 479001600

Above code will output something like this

\(y = x*1\)

\(y = 1*1\)

$y = 1*2

\(y = 1*2*3\)

\(y = 1*2*3*4\)

\(y = 1*2*3*4*5\)

\(y = 1*2*3*4*5*6\)

\(y = 1*2*3*4*5*6*7\)

\(y = 1*2*3*4*5*6*7*8\)

\(y = 1*2*3*4*5*6*7*8*9\)

\(y = 1*2*3*4*5*6*7*8*9*10\)

\(y = 1*2*3*4*5*6*7*8*9*10*11\)

\(y = 1*2*3*4*5*6*7*8*9*10*11*12\)

\(y = 479001600\)

We could have also used factorial() function which would have given us the same result.

factorial(12)
## [1] 479001600

Question 2:

Show how to create a numeric vector that contains the sequences from 20 to 50 by 5.

Answer 2:

The question is asking for a vector that needs to be numeric, which i can use as.numeric() function. The vector also needs to be numbers between 20 to 50 and needs to go up by 5 in each element. I can use seq(from=.., to=.., by=..) function.

numeric_vector <- as.numeric(seq(from=20, to=50, by=5)) # creating the numberic vector with sequence from 20 to 50 by 5. 
numeric_vector # display the numeric vector
## [1] 20 25 30 35 40 45 50
class(numeric_vector) # confirming it is numeric 
## [1] "numeric"

Question 3:

Create the function “factorial” that takes a trio of input numbers a,b,c and solve the quadratic equation. The function should print as output the two solutions.

Answer 3:

In order to create this function we need to look at what quadratic equation is, what a,b,c represents and how we solve it.

\(ax^2+bx+c=0\)

x is the unknown , a b and c are known numbers. In order to solve the quadratic formula we need to follow the two below formulas depending if the discriminant is positive or zero. If the discriminant is negative there are no real roots. Discriminant will tell us what type of roots the equation has.

\(D=b^2-4ac\)

D is postive: Formula is: \(x_{1,2}=\frac{-b+-\sqrt{D}}{2a}\)

D is zero: Formula is: \(x=\frac{-b}{2a}\)

We need to define these formulas along with the condition(if statement) in our function.

factorial <- function(a,b,c){
  if(delta(a,b,c) > 0){ # this outlines the formula if the discriminant is positive. 
    x_1 = (-b+sqrt(delta(a,b,c)))/(2*a)
    x_2 = (-b-sqrt(delta(a,b,c)))/(2*a)
    factorial = c(x_1,x_2)
  }
  else if(delta(a,b,c) == 0){ # this outlines the formula if discrimant equals to 0.  
    x = -b/(2*a)
  }
}

# This outlines the formula to find the discriminant. 
delta<-function(a,b,c){
  b^2-4*a*c
}

We can test our factorial function with an example;

\(ax^2+bx+c=0\) -is the quadratic equation.

\(a=1\), \(b=-2\), \(c=1\)

\(x^2+-2x+1=0\)

example <- factorial(1,-2,1)
example
## [1] 1

Another example to show if discriminant is negative ( \(D=b^2-4ac\) ) and we want the function to return “There are no real roots”

factorial <- function(a,b,c){
  if(delta(a,b,c) > 0){ # this outlines the formula if the discriminant is positive. 
    x_1 = (-b+sqrt(delta(a,b,c)))/(2*a)
    x_2 = (-b-sqrt(delta(a,b,c)))/(2*a)
    factorial = c(x_1,x_2)
  }
  else if(delta(a,b,c) == 0){ # this outlines the formula if discrimant equals to 0.  
    x = -b/(2*a)
  }
  else {"There are no real roots"} # this outlines if D<0 there are no real roots. 
}

# This outlines the formula to find the discriminant. 
delta<-function(a,b,c){
  b^2-4*a*c
}

example_2 <- factorial(5,5,5)
example_2
## [1] "There are no real roots"