fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int gcd(int u, int v)
  6. {
  7. if (v==0)
  8. return u;
  9. else
  10. return gcd(v, u%v);
  11. }
  12.  
  13. int lcm(int u, int v)
  14. {
  15. // least common multiplier is the product of the two
  16. // numbers divided by the GCD (GCD would otherwise
  17. // be included twice, which is unnecessary); note that
  18. // I first divide one number by GCD, so that we don't
  19. // end up with a too large of a number when it is not
  20. // necessary
  21.  
  22. u /= gcd(u,v);
  23.  
  24. return u*v;
  25. }
  26.  
  27. int main()
  28. {
  29. int i;
  30. int result = 1;
  31.  
  32. // number that can be divided by 1 through 2 is
  33. // the least common multiplier of all the numbers,
  34. // which can be computed one number at a time
  35.  
  36. for (i=2; i<=20; i++)
  37. result = lcm(result, i);
  38.  
  39. cout << result << endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.02s 2680KB
stdin
Standard input is empty
stdout
232792560