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:
compute_factorial <- function(x){
# take input from the user
if (x < -1){
return (-1)
} else if (x == 0) {
return (0)
} else {
value <- 1
# for loop to calculate the value
for(i in 1:x) {
value = value * i
}
return (value)
}
}
# Call the function and print the value
factorial <- compute_factorial(12)
print (factorial)
## [1] 479001600
# Create a seq by specifiying the start, end and interval values
y <- c(seq(20,50,5))
# Print the vector
print (y)
## [1] 20 25 30 35 40 45 50
factorial <- function(a,b,c){
##
# Quadratic equation ax2 + bx + c = 0
# can be solved with formula below
# (-b(+_)sqrt(b^2 - 4 * a * c))/(2 * a)
##
if(a!=0){
##
# Breaking the formula into 2 parts for simplicity
# val is common
# x1 and x2 are computed by their respective +- signs
##
val <- sqrt(b^2 - 4 * a * c)
x1 <- (-b - val) / (2 * a)
x2 <- (-b + val) / (2 * a)
return(sprintf ("The quadratics equation (%s,%s,%s) has answers %s and %s",a,b,c,x1, x2))
}else{
return (sprintf ("(%s,%s,%s) is not an quadratic equation!",a,b,c))
}
}
factorial(1,-1,-2)
## [1] "The quadratics equation (1,-1,-2) has answers -1 and 2"
factorial(1,-3,-4)
## [1] "The quadratics equation (1,-3,-4) has answers -1 and 4"
factorial(1,0,-4)
## [1] "The quadratics equation (1,0,-4) has answers -2 and 2"
factorial(6,11,-35)
## [1] "The quadratics equation (6,11,-35) has answers -3.5 and 1.66666666666667"
factorial(0,11,-35)
## [1] "(0,11,-35) is not an quadratic equation!"