Skip to content

Instantly share code, notes, and snippets.

@developersoom
Created September 22, 2018 16:14
Show Gist options
  • Save developersoom/40c8a776b029cafffadca035eaeacc25 to your computer and use it in GitHub Desktop.
Save developersoom/40c8a776b029cafffadca035eaeacc25 to your computer and use it in GitHub Desktop.
findGcdLcm
// 약수 찾기
function findDivisor(number) {
let divisors = [];
for (let i = 1; i <= number; i++) {
if (number % i === 0) {
divisors.push(i);
}
}
return divisors;
}
// 공약수 찾기
function findCommonDivisor(number1, number2) {
let aDivisors = findDivisor(number1);
let bDivisors = findDivisor(number2);
let combination = aDivisors.concat(bDivisors);
let commonDivisors = []
for (let i = 0; i < combination.length; i++) {
for (let j = i + 1; j < combination.length; j++) {
if (combination[i] === combination[j]) {
commonDivisors.push(combination[i])
}
}
}
return commonDivisors;
}
// 최대공약수 찾기
function findGcd(number1, number2) {
let commonDivisors = findCommonDivisor(number1, number2);
let gcd = commonDivisors[0];
for (let i = 0; i < commonDivisors.length; i++) {
if (commonDivisors[i] >= gcd) {
gcd = commonDivisors[i];
}
}
return gcd;
}
// 최소공배수 찾기 & main
function solution(number1, number2){
let gcd = findGcd(number1,number2);
let lcm = (number1 * number2)/gcd;
return [gcd, lcm];
}
@developersoom
Copy link
Author

......너무 긴듯?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment