twelve.fact <- function()
{
ans <- 1
for(i in 12:1)
{
ans <- ans * i
}
return(ans)
}
twelve.fact()
## [1] 479001600
We can simply do this by creating a vector containing the required sequence.
basic.vector <- c(20, 25, 30, 35, 40, 45, 50)
class(basic.vector)
## [1] "numeric"
We see the vector is numeric.
Or we can do this using the seq function in R.
basic.vector2 <- seq(20, 50, by = 5)
class(basic.vector2)
## [1] "numeric"
Again, the vector is numeric.
factorial <- function(a, b, c)
{
# Check for whether there is a real or imaginary solution
temp <- b^2 - 4 * a * c
# If temp is positive, the solution is real and proceed as normal
if (temp > 0)
{
positive.x <- (b * (-1) + sqrt(temp)) / (2 * a)
negative.x <- (b * (-1) - sqrt(temp)) / (2 * a)
ans <- c(positive.x, negative.x)
return(ans)
} else
# Otherwise, temp is negative and we need to coerce temp
# into becoming a complex number
{
positive.x <- (b * (-1) + sqrt(as.complex(temp))) / (2 * a)
negative.x <- (b * (-1) - sqrt(as.complex(temp))) / (2 * a)
ans <- c(positive.x, negative.x)
return(ans)
}
}
factorial(5, 6, 1)
## [1] -0.2 -1.0