Skip to content

Commit

Permalink
#11 22.05.30 > 최대공약수, 최소공배수 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed May 30, 2022
1 parent b472500 commit dba5ede
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/etc/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function solution(n, m) {
var answer = [];

// 최대공약수
function getGreatestCommonDivisor(n, m) {
let gcd = 1;
for(let i = 2; i <= Math.min(n, m); i++) {
if(n%i === 0 && m%i === 0) gcd = i;
}
return gcd;
}

// 최소공배수
function getLeastCommonMultiple(n, m) {
let lcm = 1;
while(1) {
if((lcm%n === 0) && (lcm%m) === 0) {
break;
}
lcm++;
}
return lcm;
}

let num1 = getGreatestCommonDivisor(n,m);
let num2 = getLeastCommonMultiple(n,m);

answer.push(num1);
answer.push(num2);

return answer;
}

console.log(solution(5, 10));
2 changes: 2 additions & 0 deletions src/inf/dfs,bfs/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


# List
|번호|정답여부|풀이|개념|
|:---:|:---:|:---:|:---:|
Expand Down
Binary file added theory/img/math_1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added theory/img/math_2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions theory/math.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
> 수학관련 내용 정리!
# 두 수의 최대공약수 구하기
- 최대공약수는 num1, num2의 **공통된 약수****가장 큰 정수**를 의미함
- 2부터 Math.min(num1, num2)까지 모든 정수로 나눠보면 됨

## 코드
```js
function getGreatestCommonDivisor(num1, num2) {
let gcd = 1;
for(let i = 2; i <= Math.min(num1, num2); i++) {
if(num1%i === 0 && num2&i ===0) gcd = i;
}
return gcd;
}

getGreatestCommonDivisor(5, 10);
```

## 풀이
<img src='./img/math_1.JPG' width="500px" alt="최대공약수" /> <br>


# 두 수의 최소공배수 구하기
- 최소공배수는 num1과 num2의 **공통인 배수****가장 작은 수**를 의미함
- lcm을 1부터 시작하여 lcm++을 하면서 num1과 num2로 lcm으로 나눴을 때, 나머지 값이 0인지 확인해보기

## 코드
```js
function getLeastCommonMultiple(num1, num2) {
let lcm = 1;
whlie(1) {
if((lcm%num1 === 0) || (lcm%num2 === 0)) break;
lcm++;
}
return lcm;
}

getLeastCommonMultiple(5, 10);
```

## 풀이
<img src='./img/math_2.JPG' width="500px" alt="최소공배수" />

0 comments on commit dba5ede

Please sign in to comment.