코드로 우주평화

Count the divisors of a number 본문

나는 이렇게 학습한다/Algorithm & SQL

Count the divisors of a number

daco2020 2022. 3. 12. 20:38
반응형

Description:

Count the number of divisors of a positive integer n.

Random tests go up to n = 500000.

Examples (input --> output)

4 --> 3 (1, 2, 4)
5 --> 2 (1, 5)
12 --> 6 (1, 2, 3, 4, 6, 12)
30 --> 8 (1, 2, 3, 5, 6, 10, 15, 30)

 

Solution:

1. Repeat 'n' to generate numbers one after the other.
2. Check whether the generated numbers are divisible by 'n'.
3. Count the number of divisible numbers.

 

function getDivisorsCnt(n){
  let arr = []
  for (let i=1;i<=n;i++) {
    n%i==0 ? arr.push(i) : {}
  }
  return arr.length
}

 

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

+1 Array  (0) 2022.03.15
A square of squares  (0) 2022.03.13
Maximum Length Difference  (0) 2022.03.11
Testing 1-2-3  (0) 2022.03.10
Find the middle element  (0) 2022.03.09