example-code-2e/operator/factorial/SimpleFactorial.java
2014-12-30 15:55:06 -02:00

44 lines
769 B
Java

class SimpleFactorial {
public static long factorial(long n) {
return n < 2 ? 1 : n * factorial(n-1);
}
public static void main(String args[]) {
for (long i = 1; i <= 25; i++) {
System.out.println(i + "! = " + factorial(i));
}
}
}
/* output: incorrect results starting with 21!
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
21! = -4249290049419214848
22! = -1250660718674968576
23! = 8128291617894825984
24! = -7835185981329244160
25! = 7034535277573963776
*/