Question 1

Write a loop that calculates 13-factorial. Bonus - try to do it two different ways (for example use a for loop and a while loop). Do not use the standard factorial function. The goal is to learn about how R uses loops.

count = 1
for(i in 13:1)
  count<- count * i
print(count)
## [1] 6227020800
count=1
x<-1
while(x<=13)
{
  count = count * x
  x<- x+1
}
print(count)
## [1] 6227020800

Question 2

Show how to create a numeric vector that contains the sequence from 10 to 50 by 5.

seq_vector<-seq(10,50,5)
print(seq_vector)
## [1] 10 15 20 25 30 35 40 45 50

Question 3

Create the function “lines” that takes two x,y points and calculates three things: the distance between those two points, the slope and the y intercept. The function should allow you to input both x and y values and give you the three answers.

lines <- function(a,b,c,d)
{
  distance = sqrt( (d-b)^2 + (c-a)^2 )
  print("The distance between the two points is:")
  print(distance)
  
  slope = (d-b)/(c-a)
  print("The slope is: ")
  print(slope)
  
  yintercept = slope * a + b
  print("The y intercept is: ")
  print(yintercept)
}

lines(3,4,5,6)
## [1] "The distance between the two points is:"
## [1] 2.828427
## [1] "The slope is: "
## [1] 1
## [1] "The y intercept is: "
## [1] 7