๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ณณ๊ฐ„์—์„œ ์ธ์‹ฌ๋‚œ๋‹ค/์Šคํ„ฐ๋””

[Kotlin] ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ & ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ๊ตฌํ•˜๊ธฐ

์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ GCD(Greatest Common Divisor)

์œ ํด๋ฆฌ๋“œํ˜ธ์ œ๋ฒ• ์ด์šฉ

 

fun gcd(num1: Int, num2: Int): Int {
    return if (num2 == 0) num1
    else gcd(num2, num1 % num2)
}

 

์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ LCM(Least Common Multiple)

์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ = ๋‘ ์ž์—ฐ์ˆ˜์˜ ๊ณฑ / ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜

fun lcm(num1: Int, num2: Int): Int {
    return num1 * num2 / gcd(num1, num2)
}