# Even though, it was asked to just write a loop to calculate factorial for 12, I have taken this oppurtunity to work on creating functions, which works for calculating all the factorials, including 0! which is 1. Please consider this as my interest, but not as my disobedience for this question.
# Factorial Function can be used to calculate factorial for any number.
factorialFunction <- function(factorialNumber)
{
# Sets default answer to be zero
factorialAnswer <- 0
# Loop through the facorialNumber to calculate factorialAnswer. Accepts 'factorialNumber' to be zero as well and calculates.
for (multiplyFactor in 0:factorialNumber)
{
# Check for initial set of 'factorialAnswer' and add it by '1', to start calculating the factorial.
if (factorialAnswer == 0)
{
factorialAnswer <- factorialAnswer + 1
}
else
{
# Calculate factorial by mutliplying with numbers less than 'factorialNumber'
factorialAnswer <- factorialAnswer * multiplyFactor
}
}
factorialAnswer
}
factorialFunction(12)
## [1] 479001600
# Using seq function, we generate a numeric vector
numericVector <- c(seq (20,50,5))
numericVector
## [1] 20 25 30 35 40 45 50
#class function is used to check the type of this vector.
class(numericVector)
## [1] "numeric"
# Solving quadratic equations using R
# Standard form for the quadratic equation is ax^2 + bx + c = 0, where a!= 0
# Soving for x :
# positive number = (-b + sqrt(b^2 - 4*a*c))/2^a
# negative number = (-b - sqrt(b^2 - 4*a*c))/2^a
#
factorial <- function(a,b,c)
{
#First Step, check the passed value of 'a', is not zero, as it will not represet a valid quadratic equation.
if (a != 0)
{
a <- as.integer(a)
b <- as.integer(b)
c <- as.integer(c)
#Calulate 'discriminant' value, which is (b^2 - 4*a*c)
discriminant <- (b^2 - 4*a*c)
positiveValue <- (-b + sqrt(abs(discriminant)))/ (2^a)
negativeValue <- (-b - sqrt(abs(discriminant)))/ (2^a)
answer <- paste("Positive Value of 'x'=",positiveValue, sep = " ")
answer <- paste(answer, ", Negative value of x =", sep = " ")
answer <- paste(answer, negativeValue, sep = " ")
}
else
{
answer <- ("Unable to find solution of a quadratic equation with given 'a' value to be '0'. Please pass any non zero value for 'a'")
}
answer
}
factorial(200,0,2)
## [1] "Positive Value of 'x'= 2.48920611114446e-59 , Negative value of x = -2.48920611114446e-59"