Tutte le sfide
Facile 1 risolta 100.0% di accettazione

Classify a Number by Its Divisors

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

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

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

Esempi

Esempio 1
Input:
n = 18

Output atteso:
expected = "abundant"
Input:
long long n = 18;

Output atteso:
std::string expected = "abundant";
Input:
long n = 18;

Output atteso:
string expected = "abundant";

Spiegazione

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

Esempio 2
Input:
n = 28

Output atteso:
expected = "perfect"
Input:
long long n = 28;

Output atteso:
std::string expected = "perfect";
Input:
long n = 28;

Output atteso:
string expected = "perfect";

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 KKKaneki Ken Python 1,167 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le recensioni AI. Accedi e risolvi la sfida per partecipare alla discussione.