Math if fun

  1. Greatest Common Divisor, gcd(a, b)
    • If b|a then gcd(a, b) = b.
    • If a = bt + r, for integers t and r, then gcd(a, b) = gcd(b, r).
      • Indeed, every common divisor of a and b also divides r. Thus gcd(a, b) divides r. But, of course,gcd(a, b)|b. Therefore, gcd(a, b) is a common divisor of b and r and hence gcd(a, b) ≤ gcd(b, r). The reverse is also true because every divisor of b and r also divides a.
         1 /**
         2  * Java method to calculate the greatest common divisor
         3  * @return the gcd of two integer
         4  */
         5 private static int gcd(int x, int y) {
         6     if(y == 0) {
         7         return x;
         8     }
         9     return gcd(y, x % y);
        10 }
  2. c
posted @ 2014-11-17 23:38  wxwcase  阅读(124)  评论(0编辑  收藏  举报