Loading

Paste #p77ob0ltw

  1. /**
  2.  * Compute least common multiple (lcm) of arguments \a a and \a b, the smallest
  3.  * integer value that is a multiple of both \a a and \a b.
  4.  * @param a First number.
  5.  * @param b second number.
  6.  * @return Least common multiple of values \a a and \a b.
  7.  *
  8.  * @note This function only works for non-negative values of \a a and \a b.
  9.  */
  10. int LeastCommonMultiple(int a, int b)
  11. {
  12.     if (a == 0 || b == 0) return 0; // By definition.
  13.     if (a == 1 || a == b) return b;
  14.     if (b == 1) return a;
  15.  
  16.     return a * b / GreatestCommonDivisor(a, b);
  17. }
  18.  
  19. /**
  20.  * Compute greatest common divisor (gcd) of \a a and \a b.
  21.  * @param a First number.
  22.  * @param b second number.
  23.  * @return Greatest common divisor of \a a and \a b.
  24.  */
  25. int GreatestCommonDivisor(int a, int b)
  26. {
  27.     while (b != 0) {
  28.         int t = b;
  29.         b = a % b;
  30.         a = t;
  31.     }
  32.     return a;
  33.  
  34. }
  35.  
  36. int CommonWhatever(int a) {
  37.     int b = 1;
  38.     for (int i = 1; i <= 4; i++) {
  39.         int c = LeastCommonMultiple(i, a);
  40.         b = c > b ? c : b;
  41.     }
  42.     return b;
  43. }

Comments