Volver a los desafíos
Fácil 0 resueltos 0% de aceptación

Classify a Number by Its Divisors

Classify a positive integer as perfect, abundant, or deficient.

Lo que aprenderás

  • Find all proper divisors of a number
  • Speed up divisor search with the square root
  • Classify results with conditions

Problema

Ver traducción

Add the positive divisors of n that are strictly smaller than n. If their sum equals n, the number is perfect. If the sum is larger, it is abundant; if smaller, it is deficient. The number 1 has no proper positive divisors, so its divisor sum is 0.

Implement perfect_number(n).

Input

n: The positive integer to classify.

Output

Return perfect, abundant, or deficient as lowercase text.

Restricciones

1 ≤ n ≤ 1,000,000. Zero and negative values are excluded.

Ejemplos

Ejemplo 1
Entrada:
n = 18

Salida esperada:
expected = "abundant"
Entrada:
long long n = 18;

Salida esperada:
std::string expected = "abundant";
Entrada:
long n = 18;

Salida esperada:
string expected = "abundant";

Explicación

The proper divisors are 1, 2, 3, 6, and 9. Their sum is 21, greater than 18.

Ejemplo 2
Entrada:
n = 28

Salida esperada:
expected = "perfect"
Entrada:
long long n = 28;

Salida esperada:
std::string expected = "perfect";
Entrada:
long n = 28;

Salida esperada:
string expected = "perfect";

Explicación

The proper divisors sum to 1 + 2 + 4 + 7 + 14 = 28, exactly the number itself.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Find the proper divisors of n and compare their sum with n to decide the class.

Pista 2

You only need to search divisors up to the square root, pairing each divisor with its companion.

Pista 3

Treat 1 carefully, since it has no proper positive divisors and its divisor sum is zero.

Complejidad esperada

O(√n) time, O(1) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.