public class Factorial { public static void main(String[] args) { //Print the factorial of 10. System.out.println(f(10)); System.exit(0); } //Return the factorial of n. private static int f(int n) { if (n <= 1) { return 1; //No multiplication needed. } return n * f(n - 1); //return n <= 1 ? 1 : n * f(n - 1); } } /* From Donal Knuth, "The Art of Computer Programming. Volume 1: Fundamental Algorithms", (2nd ed., 1973) p. 45: "It is helpful to keep the value 10! = 3,628,800 in mind; one should remember that 10! is about 3.5 million. In a sense, the number 10! represents an approximate dividing line between things which are practical to compute and things which are not. If an algorithm requires a testing of more than 10! cases, chances are it may take too long to run on a computer to be practical. On the other hand, if we are to test 10! cases and each case requires, say, one millisecond of computer time, then the entire run will take about an hour. These comments are very vague, of course, but they can be used to give an intuitive idea og what is computationally feasible." */