factorial1.txt Size : 0.359 Kb Type : txt |
|
sumbase.txt Size : 1.494 Kb Type : txt |
|
multbase.txt Size : 2.703 Kb Type : txt |
|
factorial2.txt Size : 3.933 Kb Type : txt |
|
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.
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.
Make a free website with Yola