- (! means factorial) The factorial function (symbol: !) means to multiply a series of descending natural numbers. Can be used to find permutations and combinations, especially of large numbers
- Here first 20 factorials:
- void setup() {
for(int i=0;i<20;i++) {
long N = i;
println(i+ " :" +factorial(N));
}
}
long factorial(long n) {
if (n < 0) throw new RuntimeException("Underflow error in factorial");
else if (n > 20) throw new RuntimeException("Overflow error in factorial");
else if (n == 0) return 1;
else return n * factorial(n-1);
}
//Processing variation adapted from http://introcs.cs.princeton.edu/java/23recursion/Factorial.java.html
- Output from code:
0 :1
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
No comments:
Post a Comment