--- title: 'Finding Divisors' author: '์ํ' date: 2024-10-17T15:40:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- Divisors: An integer that can divide another integer without a remainder. 1. Finding divisors by dividing all numbers Condition: Add the number if the remainder is 0 when divided. Skip if the condition is not met. Range of condition: The divisor can be at most the number itself, so set the loop to iterate up to the number. ```js let num = 8; // ์ฝ์๋ฅผ ์ฐพ๊ธฐ ์ํ ์ ์ ์ค์ let result = [] let index = 1; while (index <= num) { if (num % index === 0) result.push(index) index++ } console.log(result) // [ 1, 2, 4, 8 ] ``` 2. Checking only up to half of the given number Divisors, except for the number itself, cannot be larger than `num / 2`, so check only up to half. ```js let num = 8; let result = [] let index = 1; while (index <= num / 2) { if (num % index === 0) result.push(index) index++ } result = [...result, num] // ๋ณธ์ธ ๊ฐ ์ถ๊ฐ๊น์ง ์ถ๊ฐ console.log(result) // [ 1, 2, 4, 8 ] ``` 3. Using the square root (Math.sqrt) Find the divisors of num within the range of 1 to the square root of num. If num is 100, divide by numbers from 1 to 10 and find those that result in a remainder of 0. [1, 2, 4, 5, 10] Also, the result of dividing num by these divisors will be divisors of num. 100 / 1 = 100 100 / 2 = 50 100 / 4 = 25 100 / 5 = 20 100 / 10 = 10 โ duplicate [1, 2, 4, 5, 10, 10, 20, 25, 50, 100] **Remove duplicates** Exclude the divisor from the result if it equals num / divisor. [1, 2, 4, 5, 10, 20, 25, 50, 100] **Using Set** ```js let num = 100; let result = [] let index = 1; while (index <= Math.sqrt(num)) { if (num % index === 0) { result.push(index) if (num / index !== index) result.push(num / index) } index++ } result.sort((a, b) => a - b) console.log(result) // [ 1, 2, 4, 5, 10, 20, 25, 50, 100 ] ``` *Reference* <https://velog.io/@woody_/JS-%EC%95%BD%EC%88%98-%EA%B5%AC%ED%95%98%EA%B8%B0-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98>
ย
--- title: '์ฝ์ ๊ตฌํ๊ธฐ' author: '์ํ' date: 2024-10-17T15:40:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- ์ฝ์ : ์ด๋ค ์ ์๋ฅผ ๋๋จธ์ง ์์ด ๋๋ ์ ์๋ ์ ์ 1. ๋ชจ๋ ์๋ฅผ ๋๋ ์ ์ฝ์ ๊ตฌํ๊ธฐ ์กฐ๊ฑด : ๋๋์์๋ ๋๋จธ์ง์ ๊ฐ์ด 0์ด๋ฉด ์ถ๊ฐ ์กฐ๊ฑด์ ๋ง์กฑ ๋ชปํ์๋๋ ์ถ๊ฐ ์กฐ๊ฑด ๋ฒ์ : ์ฝ์๋ ์ต๋ ์๊ธฐ ์์ ๊น์ง ๋๊ธฐ ๋๋ฌธ์ ๋ฐ๋ณต๋ฌธ์์ ์๊ธฐ ์์ ๊ฐ ์ดํ๋ก ์ค์ ```js let num = 8; // ์ฝ์๋ฅผ ์ฐพ๊ธฐ ์ํ ์ ์ ์ค์ let result = [] let index = 1; while (index <= num) { if (num % index === 0) result.push(index) index++ } console.log(result) // [ 1, 2, 4, 8 ] ``` 2. ์ฃผ์ด์ง ์์ ์ ๋ฐ์ ๋์์ผ๋ก๋ง ํ์ธํ๊ธฐ ์ฝ์๋ ๋ณธ์ธ์ ์ ์ธํ๊ณ num/2 ๋ณด๋ค ํด ์ ์๊ธฐ ๋๋ฌธ์ ์ ๋ฐ ๊ฐ ๊น์ง๋ง ์ฒดํฌ ```js let num = 8; let result = [] let index = 1; while (index <= num / 2) { if (num % index === 0) result.push(index) index++ } result = [...result, num] // ๋ณธ์ธ ๊ฐ ์ถ๊ฐ๊น์ง ์ถ๊ฐ console.log(result) // [ 1, 2, 4, 8 ] ``` 3. ์ ๊ณฑ๊ทผ(Math.sqrt) ์ฌ์ฉํ๊ธฐ 1 ~ num์ ์ ๊ณฑ๊ทผ ๋ฒ์๋ก num์ ์ฝ์ ๊ตฌํด num์ด 100์ด๋ฉด 1~10 ๊น์ง ๋๋ ์ ๋๋จธ์ง๊ฐ 0 ์ธ ๊ฐ ๊ตฌํ๋ค. [1, 2, 4, 5, 10] num์ ์์ ์ฝ์๋ก ๋๋์์๋ ๊ฐ ์ญ์ num์ ์ฝ์ 100 / 1 = 100 100 / 2 = 50 100 / 4 = 25 100 / 5 = 20 100 / 10 = 10 โ ์ค๋ณต [1, 2, 4, 5, 10, 10, 20, 25, 50, 100] **์ค๋ณต์ ๊ฑฐ** num / 1๋ฒ์ ์ฝ์ === 1๋ฒ์ ์ฝ์ ์ธ๊ฑฐ ์ ์ธ [1, 2, 4, 5, 10, 20, 25, 50, 100] **Set ์ด์ฉ** ```js let num = 100; let result = [] let index = 1; while (index <= Math.sqrt(num)) { if (num % index === 0) { result.push(index) if (num / index !== index) result.push(num / index) } index++ } result.sort((a, b) => a - b) console.log(result) // [ 1, 2, 4, 5, 10, 20, 25, 50, 100 ] ``` *Reference* <https://velog.io/@woody_/JS-%EC%95%BD%EC%88%98-%EA%B5%AC%ED%95%98%EA%B8%B0-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98>