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.

Let’s go through an example to understand this better. Together, $220$ and $284$ make up the smallest pair of amicable numbers. The proper divisors of 220 are $1$, $2$, $4$, $5$, $10$, $11$, $20$, $22$, $44$, $55$, and $110$. These proper divisors add up to $284$. Similarly, the proper divisors of $284$ are $1$, $2$, $4$, $71$, and $142$, which sum up to $220$.

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