# Factorial Kata

By: Dan Stewart

July 22, 2013

MIT License

For recursion to work, two properties must hold. First, there must be one or more

base cases, where we compute the solution directly without recursion. Second, each recursive call of the procedure must be on a smaller instance of the same problem that will eventually reach a base case.

The quote above came from a chapter on recursion in the book, Algorithms Unlocked by Thomas H. Cormen. I’ve always believed C# programming can be broken down into assignment, iteration, and recursion. Recursion is the last and most difficult concept to grasp.

A factorial is the result of starting with a number and multiplying it by itself minus one. It is expressed with the n! notation where n is the number.

5! = 5 × 4 × 3 × 1 = 120

If you have five marbles each with a different color, there are 120 ways of arranging the marbles.

FactorialKata.cs

```
public class FactorialKata
{
public static void Main()
{
Console.WriteLine( factorial( 5 ) );
}
private static int factorial( int number )
{
if ( number <= 1 )
{
return 1;
}
return number * factorial( number - 1 );
}
}
```