Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Amicable Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring number properties is a fascinating journey. One interesting concept is that of amicable numbers.
Amicable numbers are two numbers that share a unique relationship: the sum of the proper divisors of each number is equal to the other number.
In simpler terms, the sum of the divisors of one number is the other number itself.
In this tutorial, we'll delve into a Python program designed to check whether two given numbers are amicable or not.
The program will identify and compare the sum of proper divisors to determine if the numbers form an amicable pair.
π Example
Let's take a look at the Python code that accomplishes this task.
# Function to calculate the sum of proper divisors of a number
def sum_of_divisors(num):
divisors_sum = 1 # Start with 1 as every number is divisible by 1
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
divisors_sum += i
if i != num // i:
divisors_sum += num // i
return divisors_sum
# Function to check if two numbers are amicable
def are_amicable(num1, num2):
return sum_of_divisors(num1) == num2 and sum_of_divisors(num2) == num1
# Driver program
if __name__ == "__main__":
# Replace these values with your desired numbers
number1 = 220
number2 = 284
# Check if the numbers are amicable
if are_amicable(number1, number2):
print(f"{number1} and {number2} are amicable numbers.")
else:
print(f"{number1} and {number2} are not amicable numbers.")
π» Testing the Program
To test the program with different numbers, replace the values of number1 and number2 in the if __name__ == "__main__": block.
220 and 284 are amicable numbers.
Run the script to check if the numbers form an amicable pair.
π§ How the Program Works
- The program defines a function sum_of_divisors to calculate the sum of proper divisors for a given number.
- Another function, are_amicable, checks whether two numbers are amicable by comparing the sums of their proper divisors.
- The if __name__ == "__main__": block tests the amicability of two numbers and prints the result.
π§ Understanding the Concept of Amicable Numbers
Amicable numbers exhibit a unique relationship where the sum of the proper divisors of each number is equal to the other number.
For example, the pair (220, 284) is amicable because the sum of divisors of 220 is 284, and the sum of divisors of 284 is 220.
π’ Optimizing the Program
While the provided program is effective, you may explore optimizations such as caching the results of the sum of divisors to enhance performance for repeated calculations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Amicable Number), please comment here. I will help you immediately.