title: Finding lcm from 1 through 20, gives error "at Main.gcd(Main.java:21)"
body: <p>I'm trying to find the lcm of numbers (1,2,3,4....20), this is a very simple java program to do so but it gives me an error. This error only appears when i run the loop to 20, but not when i run it till 10. The answer is 232792560 which is a 9 digit number, which should easily come in long data type so i doubt its an overflow problem. I cannot find the bug, please help.</p>
  
  <pre><code>static long gcd(long a, long b)
  {
      if(a==b) return a;
  
      if(a&gt;b)
      {
          return gcd(a-b , b);
      }
      else{
          return gcd(a, b-a);
      }
  }
  
  static long lcm(long a, long b)
  {
      return (a*b)/gcd(a, b);
  }   
  
  public static void main(String[] args) {
  
      long l=1;
      for(long i=1;i&lt;=20;i++)
      {
          l=lcm(l , i);
          System.out.println(l);
      }           
  }
  </code></pre>