วันจันทร์ที่ 2 กันยายน พ.ศ. 2556

การหา ห.ร.ม. แบบใหม่

Han Ruam Mak

Processing Code

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);
  }
}
void setup() {
  println(gcd(5, 10));
}

เทียบกับในการออกแบบภาษาจาวา โดยใช้โปรแกรม Terminal

class GCD{
public static 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);
  }
}
public static void main(String [] arg) {
  System.out.println(gcd(5,90));
}
}

Canvas


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

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