go to previous page   go to home page   go to next page

Answer:

factorial(17) is too big to hold in an int.


Iterative Factorial

factorial(N) gets very big even for small N. It quickly gets too big to hold in a Java int, so wrong answers are produced. 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 that range.

You could improve the Java method by using long or perhaps double in place of int. This would extend the range of N, although not 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 to remove the factorial function. Often this can be done through cancellation. Usually this makes the formula less compact, 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.


QUESTION 7:

Here is the formula for permutations of n things taken r at a time:

n!/(n-r)!

Say that n is 100 and r is 3. What does the formula give you?