# Henri Pellemans RBA ## Factorial - introduction

Factorial is expessed as n! where n is any integer number >= 1 and n! = n * (n-1) * (n-2) * ... * 2 * 1
Starting at n = 1 the values of n! are as follows:

1! = 1

2! = 2 * 1 = 2

3! = 3 * 2 * 1 = 6

4! = 4 * 3 * 2 * 1 = 24

5! = 5 * 4 * 3 * 2 * 1 = 120

6! = 6 * 5 * 4 * 3 * 2 * 1 = 720 and so on

Factorial is to be interpreted as the sum of unique combinations you can make with n elements. With n = 3 we have the following 6 possibilities: 123, 132, 213, 231, 312 and 321, so 3! = 6.

With n = 4 we get 24 unique combinations: from 1234, 1243, ... to 4321. Please notice that 4! = 4 * 3!.

While increasing n, the number of possible unique combinations grows at a very fast pace. Just try it in Excel and you will see that Excel (2010 32-bit) has reached its limits at 20! = 2432902008176640000.

## Factorial - calculation of n!

Enter an integer number and press the button to calculate the factorial of this number

I have limited the number n to a maximum of 1000. Without any action the result would run of the screen. So I split the result in parts of 3 digits.

## FactorFactorial - calculation of the trailing zeros of n!

Starting at 5!, the last digits of factorial are always one or more zeros. From the example above we learn that 20! has 4 trailing zeros. Do you have any idea how many zeros the number 1000! has at the end? With a smart algorithm you can count them almost by head. There's no need to actually calculate the factorial of 1000. Calculating 1000000! is a bit time consuming, but the calculation of the trailing zeros is done in a split second. Just press the button:

Enter an integer number and press the button to calculate the trailing zeros of factorial(number)

and you will find the answer here...

BTW, I also limited the input of the integer number to the maximum value of the data type Integer. In the old versions of VB this maximum value (of Integer) was only 32,767 *. But we are lucky, the VB.NET version of Integer goes up to 2,147,483,647 so you must not enter e.g. 3,000,000,000, otherwise my software will tell you that something has gone wrong.

* a funny story from long time ago: somewhere around the year 2000, maybe a bit earlier, I had a problem while updating my anti-virus software on my brand new computer. During the update process I got an error message. Somehow I was able to dive into the installation script and found the following piece of code:

Dim FreeMB as Integer

My new computer had a harddisk of 80 GB, which was very huge in those days. The free space of my disk was at least 65 GB, or 65,000 MB. The installation script checked the free space on my computer, but ran into a sort of overflow because my free space was greater than the (old) maximum value of the data type Integer. That was the easy part of the story. It took some time to convince the (American) AV company that they indeed had a bug in their program. That was the difficult part of the story.