10 Things Every Java Programmer Should Know About Ruby | [ Prev | Home | Next ] |
Ruby Codedef factorial(n)
result = 1
(2..n).each do |i|
result *= i
end
result
end
puts factorial(20)
puts factorial(21)
|
Java Codepublic class Fact {
static long factorial(long n) {
long result = 1;
for (long i=2; i<=n; i++)
result *= i;
return result;
}
public static
void main (String args[]) {
System.out.println(factorial(20));
System.out.println(factorial(21));
}
}
|
Output2432902008176640000 51090942171709440000
|
Output2432902008176640000 -4249290049419214848
|
O'REILLY® OSCON 2005 | Copyright 2005 by Jim Weirich (All Rights Reserved) |