Created
September 22, 2018 16:14
-
-
Save developersoom/40c8a776b029cafffadca035eaeacc25 to your computer and use it in GitHub Desktop.
findGcdLcm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 약수 찾기 | |
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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
......너무 긴듯?