Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
c++
python
amicable
numbers

What are amicable numbers?

Anusheh Zohair Mustafeez

Amicable numbers or friendly numbers are a pair of numbers whose sum of proper divisors equals the other.

svg viewer

Let’s go through an example to understand this better. Together, 220220 and 284284 make up the smallest pair of amicable numbers. The proper divisors of 220 are 11, 22, 44, 55, 1010, 1111, 2020, 2222, 4444, 5555, and 110110. These proper divisors add up to 284284. Similarly, the proper divisors of 284284 are 11, 22, 44, 7171, and 142142, which sum up to 220220.

Implementation

Below is a program that checks whether or not two numbers form an amicable pair. Feel free to change num1 and num2 to check if a pair is amicable.

# A function that takes an integer and returns the sum of its proper divisors. 
# E.g. proper divisors of 8 are 1, 2 and 4 so the function will return 
# 7 (1 + 2 + 4) when 8 is given as input 
def sum_of_proper_divisors(num):
    divisor_sum = 0
    for i in range(1, num):
        if (num%i == 0):
            divisor_sum += i
    return divisor_sum


# A function that takes two integers as input and returns true if the two
# form an amicable pair and false if not
def form_amicable_pair(num1, num2):
    if (sum_of_proper_divisors(num1) == num2) and (sum_of_proper_divisors(num2) == num1):
        return True
    else:
        return False

# Two integers being tested
num1 = 220
num2 = 284

if form_amicable_pair(num1, num2):
    print(str(num1)+" and "+str(num2)+" are amicable numbers!")
else:
    print(str(num1)+" and "+str(num2)+" are not amicable numbers!")

RELATED TAGS

java
c++
python
amicable
numbers

CONTRIBUTOR

Anusheh Zohair Mustafeez
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring