Perfect Number Finder in a Range

Hard ⏱ 18 min 34% acceptance ★★★★☆ 4.4
Write a function perfect_numbers_in_range(start, end) that returns a list of all perfect numbers between start and end inclusive. A perfect number equals the sum of its proper divisors (divisors excluding itself), for example 6 = 1 + 2 + 3. Use nested loops: an outer loop over candidates and an inner loop to sum divisors.

Examples

Example 1
Input
perfect_numbers_in_range(1, 30)
Output
[6, 28]
Explanation

6 and 28 are the only perfect numbers in that range.

Example 2
Input
perfect_numbers_in_range(1, 5)
Output
[]
Explanation

No perfect numbers that small.

Constraints

  • 1 <= start <= end.

Topics

Loopsnested loops

Companies

DeloitteAccenture

Hints

Hint 1

For each candidate number, sum all divisors from 1 up to candidate - 1 that evenly divide it.

Hint 2

A candidate is perfect if that sum equals the candidate itself (and the candidate is greater than 0).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.