Notice
Recent Posts
Recent Comments
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

JavaScript: Greatest Common Divisor (์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜), Least Common Multiple (์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜) ๋ณธ๋ฌธ

๐Ÿ’ปBootcamp Self-Study Revisionโœจ/JavaScript, jQuery, Ajax

JavaScript: Greatest Common Divisor (์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜), Least Common Multiple (์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜)

yjyuwisely 2023. 6. 29. 15:39

 

์ •์ˆ˜๋ก  (1) - ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜, ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•

์•ˆ๋…•ํ•˜์„ธ์š”, Dimen์ž…๋‹ˆ๋‹ค! ์˜ค๋Š˜๋ถ€ํ„ฐ ์ •์ˆ˜๋ก ์— ๋Œ€ํ•œ ๊ธ€์„ ์จ๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜๋ก ์€ ์ •๊ทœ ์ˆ˜ํ•™ ๊ต์œก๊ณผ์ •์—์„œ ์ž˜ ๋‹ค๋ฃจ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ๋ถ„๋“ค์—๊ฒŒ ์ƒ์†Œํ•œ ๋ถ„์•ผ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋งŒํผ ๋งŽ์€ ๋ถ„๋“ค์—๊ฒŒ

dimenchoi.tistory.com


Greatest Common Divisor (์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜)
:the greatest common divisor of two or more integers is the largest positive integer that divides each of the given integers without leaving a remainder.

function GCD(x, y) {
  return !x ? y : GCD(y, x % y);
}

!b: to check if the value of b is equal to 0. (! = (logical NOT))
If it is, the function returns a as the greatest common divisor.

If b is not equal to 0, the function calls itself recursively, passing b as the new value of a and the remainder of a divided by b (a % b) as the new value of b. 
This process continues until b becomes 0, at which point the function returns a as the greatest common divisor.


Least Common Multiple (์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜)
The GCD represents the largest positive integer that divides both x and y without leaving a remainder. 

  function LCM(x, y) {
    return (x * y) / GCD(x, y);
  }

1. The GCD(x, y) function is called to calculate the GCD of x and y. 
2. The product of x and y is divided by the GCD using the division operator /.
3. The result is the LCM of x and y.


728x90
๋ฐ˜์‘ํ˜•
Comments