Factorials are very simple things. They're just products, indicated by an exclamation mark. For instance, "four factorial" is written as "4!" and means 1×2×3×4 = 24
For n=0,
0! = 1
The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars.
Factorial definition formula
Examples:
1! = 1
2! = 1×2 = 2
3! = 1×2×3 = 6
4! = 1×2×3×4 = 24
5! = 1×2×3×4×5 = 120
Recursive factorial formula
n! = n×(n-1)!
Example:
5! = 5×(5-1)! = 5×4! = 5×24 = 120
Applications :
Permutation Formula
nPr = n(n - 1)(n - 2) ... (n - r + 1) =n!(n - r)!
Example : How many ways 4 students from a group of 15 be lined up for a photograph ?
Answer : There are 15P4 possible permutations of 4 students from a group of 15.
15P4 = 15! / 11! = 15 * 14 * 13 * 12 = 32760 different lineups.
A formula for the number of possible combinations of r objects from a set of n objects. This is written in any of the ways shown below
nCr =n!=n(n - 1)(n - 2) ... to r factors.(r!)(n - r)!r!
Note:
|
= 15! / 4!11! = (15 * 14 *3) / (4 * 3 *2 *1 ) = 1365
public static long dnrFactorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
Recursive Function :
public static long dnrFactorialRecursive(int n)
{
if (n == 0)
return 1;
else
return n * dnrFactorialRecursive(n-1);
}