Tất cả thử thách
Dễ 1 đã giải Tỷ lệ chấp nhận 100.0%

Classify a Number by Its Divisors

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

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

Ví dụ

Ví dụ 1
Đầu vào:
n = 18

Kết quả mong đợi:
expected = "abundant"
Đầu vào:
long long n = 18;

Kết quả mong đợi:
std::string expected = "abundant";
Đầu vào:
long n = 18;

Kết quả mong đợi:
string expected = "abundant";

Giải thích

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

Ví dụ 2
Đầu vào:
n = 28

Kết quả mong đợi:
expected = "perfect"
Đầu vào:
long long n = 28;

Kết quả mong đợi:
std::string expected = "perfect";
Đầu vào:
long n = 28;

Kết quả mong đợi:
string expected = "perfect";

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 KKKaneki Ken Python 1,167 ms - 4/5 Xem code

Học hỏi từ người khác

Giải pháp cộng đồng

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.