วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

G.C.D. & L.C.D.



void setup() {
  size(100, 70);
  background (204, 255, 0);
  fill (51, 0, 0);
  text ("G.C.D. is "+gcd(45, 81), 20, 30);
  text ("L.C.D. is "+lcd(45, 81), 15, 50);
}
int gcd(int x, int y) {
  if (x==0) {
    return y;
  }
  if (y==0) {
    return x;
  }
  if (x>y) {
    return gcd(y, x%y);
  }
  else {
    return gcd (x, y%x);
  }
}
int lcd (int x, int y) {
  int lcd = (x*y)/gcd(x, y);
  return lcd;
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น