본문 바로가기
JavaScript/실습

[javascript] 최대공약수, 최소공배수

by Angry Stock 2023. 12. 7.
반응형
1.최대공약수

 

function gcd(a,b){
	while(b != 0){
		let temp = b;
		b = a % b;
		a = temp;
	}
	return a;
}

 

function gcd(a, b) {
  let temp;
  for (; b != 0; temp = b, b = a % b, a = temp) {}
  return a;
}

 

function gcd(a, b) {
  while (true) {
    let temp = a % b;
    if (!temp) {
      break;
    }
    a = b;
    b = temp;
  }
  return b;
}

 

function gcd(a, b) {
  let temp;
  for (; (temp = a % b); a = b, b = temp) {}
  return b;
}

 

2.최소공배수

 

    function gcd(a,b){
        while(b != 0){
            let temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
    
    function lcm(a,b){
        return (a*b)/gcd(a,b)
    }

 

function gcdlcm(a, b) {
  let ab = a * b;
  while (true) {
    let temp = a % b;
    if (!temp) {
      break;
    }
    a = b;
    b = temp;
  }
  return [b, ab / b];
}

 

function gcdlcm(a, b) {
  let temp;
  for (var ab = a * b; (temp = a % b); a = b, b = temp) {}
  return [b, ab / b];
}
반응형

댓글