factorial(N) got too big to hold in an
factorial(N) gets very big even for small N.
It soon gets too big to hold in a Java
int and wrong answers result.
In fact, 12 is the largest
int that will work as an argument.
This situation is called overflow.
The range of values that a Java
int can hold is
(roughly) -2 billion to +2 billion.
Overflow happens when a value is computed that is outside of
You could improve the Java method by using
in place of
This would extend the range of arguments that could be used with it,
range would not be extended very far.
If a mathematical formula includes the factorial function, you need to be careful to avoid overflow. The correct approach is to manipulate the formula until factorial is removed. Usually this makes the formula less pretty, but makes it suitable for implementation in a program. Topics like these are the subject of numerical analysis, a common course in a computer science curriculum.