Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly.
Given an integer n, return true if n is a perfect number, otherwise return false.
Example 1:
Input: num = 28 Output: true Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all divisors of 28.
Example 2:
Input: num = 7 Output: false
Constraints:
1 <= num <= 108Problem summary: A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. Given an integer n, return true if n is a perfect number, otherwise return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
28
7
self-dividing-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #507: Perfect Number
class Solution {
public boolean checkPerfectNumber(int num) {
if (num == 1) {
return false;
}
int s = 1;
for (int i = 2; i <= num / i; ++i) {
if (num % i == 0) {
s += i;
if (i != num / i) {
s += num / i;
}
}
}
return s == num;
}
}
// Accepted solution for LeetCode #507: Perfect Number
func checkPerfectNumber(num int) bool {
if num == 1 {
return false
}
s := 1
for i := 2; i <= num/i; i++ {
if num%i == 0 {
s += i
if j := num / i; i != j {
s += j
}
}
}
return s == num
}
# Accepted solution for LeetCode #507: Perfect Number
class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num == 1:
return False
s, i = 1, 2
while i <= num // i:
if num % i == 0:
s += i
if i != num // i:
s += num // i
i += 1
return s == num
// Accepted solution for LeetCode #507: Perfect Number
struct Solution;
impl Solution {
fn check_perfect_number(num: i32) -> bool {
if num == 1 {
return false;
}
let mut i = 2;
let mut sum = 1;
while i * i <= num {
if num % i == 0 {
sum += i;
sum += num / i;
}
i += 1;
}
sum == num
}
}
#[test]
fn test() {
assert_eq!(Solution::check_perfect_number(28), true);
}
// Accepted solution for LeetCode #507: Perfect Number
function checkPerfectNumber(num: number): boolean {
if (num <= 1) {
return false;
}
let s = 1;
for (let i = 2; i <= num / i; ++i) {
if (num % i === 0) {
s += i;
if (i * i !== num) {
s += num / i;
}
}
}
return s === num;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.