Foundations Working with digits and divisors

32. Perfect Number

Easy

Given a positive integer n, return true if n is a perfect number, and false otherwise.

A proper divisor of n is a positive whole number smaller than n that divides n with no remainder. n is perfect when its proper divisors add up to exactly n.

For example, the proper divisors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6. So 6 is a perfect number.

Example 1

Input: n = 496

Output: true

Explanation: The proper divisors of 496 are 1, 2, 4, 8, 16, 31, 62, 124 and 248, and they add up to 496.

Example 2

Input: n = 36

Output: false

Explanation: The proper divisors of 36 are 1, 2, 3, 4, 6, 9, 12 and 18. They add up to 55, which is more than 36.

Example 3

Input: n = 137438691328

Output: true

Check your understanding

Work out the answer for this input before you write any code.

n = 8128

Constraints

  • 1 ≤ n ≤ 1012

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.