#1. Write a loop that calculates 12-factorial
r<-2
fact<-1
for (i in 1:12)
{
fact<-fact*(r-1)
r<-r+1
}
print(fact)
## [1] 479001600
#2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
vect<-seq(20,50,5)
vect
## [1] 20 25 30 35 40 45 50
quad=function(vec){
#find roots
#input vec<-(a,b,c)
if (length(vec)==3) {
x1<-((-vec[2]+((vec[2]^2)-(4*vec[1]*vec[3]))^.5)/(2*vec[1]))
x2<-((-vec[2]-((vec[2]^2)-(4*vec[1]*vec[3]))^.5)/(2*vec[1]))
print(x1)
print(x2)}
else {r<-"input not 3 values"
print(r)}
}
vec<-c(2,5,-3)
quad(vec)
## [1] 0.5
## [1] -3
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: