10 Things Every Java Programmer Should Know About Ruby

Ruby Code

def factorial(n)
  result = 1
  (2..n).each do |i|
    result *= i
  end
  result
end

puts factorial(20)
puts factorial(21)

Java Code

public 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));
  }
}

Output

2432902008176640000
51090942171709440000

Output

2432902008176640000
-4249290049419214848