Juni_Dev_log

(200209_TIL) "Perfect Numbers" 본문

CodingTest/Exercism

(200209_TIL) "Perfect Numbers"

Juni_K 2021. 2. 9. 15:03

▶ Perfect Numbers

 

Perfect Numbers

📌 Problem

Introduction

Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.

양의 정수에 대한 Nicomachus의 (60-120 CE) 분류 체계를 기반으로 숫자가 완전, 풍부 또는 부족인지 확인합니다.

 

The Greek mathematician Nicomachus devised a classification scheme for positive integers, identifying each as belonging uniquely to the categories of perfect, abundant, or deficient based on their aliquot sum.

: 그리스 수학자 Nicomachus는 양의 정수에 대한 분류 체계를 고안하여 각각의 분량 합계에 따라 완전, 풍부 또는 부족 범주에 고유하게 속하는 것으로 식별했습니다.

 

The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

: 알리 쿼트 합계는 숫자 자체를 포함하지 않는 숫자 요소의 합계로 정의됩니다. 예를 들어 15의 부분 표본 합계는 (1 + 3 + 5) = 9입니다.

 

  • Perfect: aliquot sum = number
    • 6 is a perfect number because (1 + 2 + 3) = 6
    • 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
  • Abundant: aliquot sum > number
    • 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
    • 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
  • Deficient: aliquot sum < number
    • 8 is a deficient number because (1 + 2 + 4) = 7
    • Prime numbers are deficient

Implement a way to determine whether a given number is perfect. Depending on your language track, you may also need to implement a way to determine whether a given number is abundant or deficient.

 

💯 Solution

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
32
33
34
35
 
export const classify = (num) => {
  // num이 0보다 작을 때 오류를 반환함.
  if(num <= 0){
    throw new Error('Classification is only possible for natural numbers.');
  }
 
  // num 을 나눌 수 있는 숫자들을 담은 배열 : factors
  var factors = [];
  // for문을 돌려서 나눠지는 값을 factors에 담음.
  for(let i = 0; i < num; i++){
    if(num % i === 0){
        factors.push(i);
    }
  }
 
  // reduce() 메소드를 통해서 factors 배열안에 값을 모두 더한 값을 factors에 담는다.
  var factors_Sum = factors.reduce(function(a,b){return a+b;},0);
 
  // factors_Sum의 합이 num의 합과 같을 때
  if(factors_Sum === num){
    return 'perfect';
  }
  // factors_Sum의 합이 num의 합보다 클 때
  else if(factors_Sum > num){
    return 'abundant';
  }
  // factors_Sum의 합이 num의 합보자 작을 때
  else{
    return 'deficienct';
}
};
 
cs

- 함수를 통해서 주어진 num 이 0보다 작거나 같으면 오류를 반환한다.

- factors 라는 배열을 만들어서, for문을 돌려서 num보다 작은 값들을 나누고 나누어 떨어지면 factors 배열에 push한다.

- reduce() 메소드를 통해서, factors 배열의 모든 값들을 더한 factors_Sum 에 담는다.

- if / else if / else 문을 통해서 factors_Sum 과 num 과의 값을 비교해서 상황에 맞게 return 하는 조건문을 만든다.

 

📚 참고

developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

 

Array.prototype.reduce() - JavaScript | MDN

reduce() 메서드는 배열의 각 요소에 대해 주어진 리듀서(reducer) 함수를 실행하고, 하나의 결과값을 반환합니다. The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to th

developer.mozilla.org

 

'CodingTest > Exercism' 카테고리의 다른 글

(200211_TIL) "Anagram"  (0) 2021.02.11
(200208_TIL) "Difference Of Squares"  (0) 2021.02.08
(200207_TIL) 'Word Count'  (0) 2021.02.07
(200205_TIL) 'Raindrops'  (0) 2021.02.05
(200204_TIL) 'Hamming'  (0) 2021.02.04
Comments