Show problems as
Classic problem #507EasyMath

Perfect Number

Real-world version: Balanced Batch Size
Tracked in this browser only
Read Perfect Number on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A fastener plant describes a batch size as balanced when the batch can be rebuilt exactly from its own smaller shares.

A share of num is a positive whole number, strictly less than num, that goes into num leaving nothing over. Add up every share of num. When that running total lands on num itself, the batch size is balanced.

Given the batch size num, return true when it is balanced and false when it is not.

Examples

Example 1

Input
num = 496
Output
true

The shares of 496 are 1, 2, 4, 8, 16, 31, 62, 124 and 248, and they add up to 496, so the batch size is balanced.

Example 2

Input
num = 500
Output
false

The shares of 500 are 1, 2, 4, 5, 10, 20, 25, 50, 100, 125 and 250, which add up to 592. That total is not 500.

Example 3

Input
num = 1
Output
false

1 has no share at all, since a share must be a positive whole number below 1. An empty total is 0, which is not 1.

Constraints

  • 1 <= num <= 10^8

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def check_perfect_number(num: int) -> bool:
Java
public boolean checkPerfectNumber(int num)