Iterative Factorial

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

factorial(N) got too big to hold in an int.

Iterative Factorial

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 that range.

You could improve the Java method by using long or perhaps double in place of int. This would extend the range of arguments that could be used with it, but the 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.

QUESTION 7:

Is an iterative version of factorial possible?