Sunday, January 8, 2017

Factorial algorithm

This algorithm is also basically part of recursion umbrella. Before discussing this further, lets first discuss what factorial is and what it is used for.

Conceptually, in triangle number , position n is the sum of  n + all previous positions to n.

In the similar way, in factorial, position n is the multiplication of n * all previous positions to n.

n! is the product of all integers greater than or equal to 1, where ! symbol means factorial.

So big question is that why do we use factorials ? Factorial can be used to find total possible combinations.

For example if we have some coins, total possible combinations in which we can arrange them are:-

1 coin ==> 1 = 1
2 coins ==> 2* 1 = 2
3 coins ==> 3*2*1 = 6
4 coins ==> 4*3*2*1 = 24
etc

Point to note, factorial of 0 is 1

Why factorial of 0 is 1 ?
According to factorial definition, n! is product of all integers greater than or equal to 1. For 0! , since there are no integers between 0 & 1 which are greater than 1 , so we take 0! as 1.


public static int findingFactorialWithRecursion(int position)
{
if(position==0) // this is one difference from triangle number as it goes till position 0
return 1;
else
return (position * findingFactorialWithRecursion(position-1));

}




No comments:

Post a Comment