factorial<- function(x){
t <-1
for (i in 1:x)
{
t <- t * ((1:x)[i])
}
print(t)
}
factorial(12)
## [1] 479001600
t <-as.numeric(c(seq(20,50,5)))
t
## [1] 20 25 30 35 40 45 50
I tried to use the chaining method while calculating ‘theta’. Chaining method can be loaded through ‘dplyr’ package.
library(dplyr)
factorial <- function(a,b,c)
{
theta <- (b*b - (4*a*c)) %>% sqrt() #chaining method
if ( theta >= 0){
root1 <- (- b - (theta))/(2*a)
root2 <- (- b + (theta))/(2*a)
return(c(root1,root2))
}
}
factorial(1,4,4)
## [1] -2 -2
factorial(5,6,1)
## [1] -1.0 -0.2