Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Amicable Pairs Coding Challenge

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

Your question is Amicable Pairs Coding Challenge. 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

Dynata data-quality checks may need to classify mathematical relationships between numeric values. Given two positive integers, determine whether they form an amicable pair.

Two distinct numbers a and b are amicable when the sum of the proper divisors of a equals b, and the sum of the proper divisors of b equals a. A proper divisor is a positive divisor smaller than the number itself.

Your solution must avoid scanning every integer from 1 through n. Use divisor-pair reasoning so that each number can be processed in O(sqrt(n)) time.

Formal Specification

Implement amicable_pair(a, b).

  • Input: Two positive integers a and b.
  • Output: Return True if they form an amicable pair, otherwise return False.
  • The numbers must be distinct. A perfect number paired with itself is not considered an amicable pair.

Constraints

  • 1 <= a, b <= 10^12
  • a and b are positive integers
  • The inputs must be distinct for the result to be True
  • Do not scan every integer from 1 through either input

Function Signature

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