Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Perfect Number Program

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Perfect Number Program. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A Luxoft India diagnostics utility needs to identify mathematically significant values in a range. Given two integers, return every perfect number between them, inclusive, in ascending order.

A positive integer is perfect when it equals the sum of its proper positive divisors, excluding the number itself. For example, 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28.

Formal Specification

Implement perfect_numbers(left, right).

  • Input: Two integers left and right, representing an inclusive range. Assume left <= right.
  • Output: A list of all perfect numbers in [left, right], sorted in ascending order. Return an empty list when the range contains none.

For each candidate, calculate its proper-divisor sum efficiently. Do not test every possible divisor up to the candidate when a square-root divisor-pair approach can be used.

Constraints

  • 1 <= left <= right <= 10^9
  • right - left <= 10^5
  • Return all matching values in ascending order
  • A perfect number is defined using proper positive divisors only

Function Signature

def perfect_numbers(left, right):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output