### factorial

LaTeX4Web 1.4 OUTPUT The factorial function is written as n! and can be computed using the recursive function an+1=nan. For instance, 5!=120 because 5! = 5 · 4 · 3 · 2 · 1 = 120. First we provide a code to compute it recursively.
 factorial1.txt Size : 0.359 Kb Type : txt

Here you can see an example of the output of the code.

However, the integer variables in C++ cannot hold very large numbers and unfortunately the factorial function grows very fast. You can see in the picture above that for 17! the program is already not working. This is why we need to implement vectors. In each cell of the vector we place one digit of the number, and because vectors have no limitation in size we can compute indefinately large factorials. First we need a code that adds and multiplies two integers using vectors. We place each digit in every cell and then we add and multiply themas we would do it by hand.

LaTeX4Web 1.4 OUTPUT It is also interesting to sum and add vectors in any base. Representing a number in base b means that we express it as a sum of consecutive powers of b. For instance, 57 in base 3 is equal to 2010, because 57 = 2 · 33 + 0 · 32 + 1 · 31 + 0 · 30. Here we provide the codes to add and multiply two integers in any base using vectors. The two numbers for the input need to be in base 10.
 sumbase.txt Size : 1.494 Kb Type : txt
 multbase.txt Size : 2.703 Kb Type : txt

Here you can see an example of the output of the two codes. The first one is for the sum and the second one for the product.

We can implement these two codes to create a better code for the factorial. For each iteration of the recursion we need to multiply two vectors using the addition. The following code computes the factorial up to any very large integer in any base. The number in the input needs to be in base 10.

 factorial2.txt Size : 3.933 Kb Type : txt

Here you can see an example of the output of the code. Note that the digits of the factorial are separated by a space because for any base larger than 10 we can have numbers with more than one digit in any cell.