프로그래머스/level 2
[프로그래머스] N개의 최소공배수
binaryJournalist
2021. 8. 10. 01:30
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
갓택오버플로우
Least Common Multiple of an array values using Euclidean Algorithm
I want to calculate the least common multiple of an array of values, using Euclideans algorithm I am using this pseudocode implementation: found on wikipedia function gcd(a, b) while b ≠ 0 ...
stackoverflow.com
** Javascript
const gcd = (a, b) => a ? gcd(b % a, a) : b;
const lcm = (a, b) => a * b / gcd(a, b);
function solution(arr) {
return arr.reduce(lcm);;
}
** Python
def gcd(a, b):
return gcd(b % a, a) if a != 0 else b
def solution(arr):
answer = arr[0]
for a in arr:
answer = a * answer / gcd(a, answer)
return answer
반응형