I made a simple function to calculate the factorial of a number but from number 34 returns 0 . It should be from number 51 .
public class Métodos {
pub
Factorials become large very fast. The int
data type you are using is too small to store the huge numbers that result. You can store up to only 12! in an int
. Switch to long
and you can store up to 20!. You will need to use BigInteger to go beyond that.
The reason why every result is 0 starting at 34!, is because 34! is:
295232799039604140847618609643520000000
The prime factors of this number are:
232 × 315 × 57 × 74 × 113 × 132 × 172 × 19 × 23 × 29 × 31
Notice that the prime factorization has 32 twos. That means that when written in binary, the number ends in exactly 32 zeros:
1101111000011011110001001101000110011110111111001010110010000010
0100010001011101101001110101101100000000000000000000000000000000
Since an int
is only 32 bits wide, it holds only the low 32 bits of that number, which are all 0, and an int
having all bits 0 represents the numeric value 0.
(I mentioned above that it actually goes wrong sooner; the correct result overflows an int
after 12!. However, it is a simple fact of multiplication that when two numbers are multiplied, digits in the numbers cannot affect result digits in lower position; only digits in same or higher position. E.g., multiply any two long numbers which both end in 4. The result must end in 6, no matter what the full numbers were. This means that even after the factorial calculation overflows the int
, all the low 32 bits are still correct!)
After 34!, every factorial will have at least 32 twos in its prime factorization (because it is a simple multiple of the previous factorial), so when the correct values are cut to fit into an int
they will all be 0. Another way of looking at it is that after 34!, every factorial is computed as 0 because it is just a multiple of the previously computed 0.