Please create the following exercises in .rmd format, publish to rpub and submit both the .rmd file and the rpub link.
###1. Write a loop that calculates 12-factorial.
reference:https://en.wikipedia.org/wiki/Factorial
5!=5x4!=5x4x3x2x1=120
n!=n x (n-1)!
x <- 1
y <- 12 # this var could change to an input.
if (y==0)
{
x <- 1 # 0!=1
cat("0!=", x)
}else
{
#following is repeating 12 time based on question. 12!=479001600
for (i in 1:y){
x<-x*i
cat(i,"!=",x,"\n")
}
}
## 1 != 1
## 2 != 2
## 3 != 6
## 4 != 24
## 5 != 120
## 6 != 720
## 7 != 5040
## 8 != 40320
## 9 != 362880
## 10 != 3628800
## 11 != 39916800
## 12 != 479001600
###2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
reference:https://www.tutorialspoint.com/r/r_vectors.htm
show numbers in between 20 to 50 and increment is 5.
vector in R is the most basic R data objects and there are 6
types.
in this example will using seq for multiple elements vector.
factor in R the data objects which are used to categorize the data and
store it as levels. They can store both strings and integers.
x<- seq(20,50,by=5)
print(x)
## [1] 20 25 30 35 40 45 50
###3. Create the function “quadratic” that takes a trio of input numbers a, b, and c and solve the quadratic equation. The function should print as output the two solutions.
reference:https://en.wikipedia.org/wiki/Quadratic_function
quadratic <- function(a,b,c)
{
if (a==0)
{
return("first arg should not = 0")
}else
{
x <- (-b + sqrt(b^2 - 4*a*c)) / 2*a
y <- (-b - sqrt(b^2 - 4*a*c)) / 2*a
output <- c(s1 = x, s2 = y)
return(output)
}
}
quadratic(0,5,0)
## [1] "first arg should not = 0"
quadratic(1,7,12)
## s1 s2
## -3 -4