- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Check If Two Numbers are Amicable Numbers
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. When it is required to check if two numbers are amicable numbers, a method can be defined that iterates over the number, and uses the modulus operator. Another method is defined that calls the previously defined function to determine if two numbers are amicable or not.
Below is the demonstration of the same −
import math def divided_sum_val(my_val) : res = 0 for i in range(2, int(math.sqrt(my_val)) + 1) : if (my_val % i == 0) : if (i == int(my_val / i)) : res = res + i else : res = res + (i + int(my_val / i)) return (res + 1) def check_amicable(x, y) : if (divided_sum_val(x) != y) : return False return (divided_sum_val(y) == x) first_num = 220 second_num = 288 print("The numbers are :") print(first_num) print(second_num) if (check_amicable(first_num, second_num)) : print ("The given numbers are amicable in nature") else : print ("The given numbers are not amicable in nature")
The numbers are : 220 288 The given numbers are not amicable in nature
A method named ‘divided_sum_val’ is defined that takes an integer has parameter.
It uses a ‘for’ loop to iterate through the value and checks the divisibility of the number.
If the iterator is equal to the value divided by the iterator, it is incremented by the iterator.
Otherwise, the entire divided number is added.
Another method named ‘check_amicable’ is defined, that takes two numbers.
It calls the ‘divided_sum_val’ and returns ‘True’ or ‘False’ depending on the computed value.
The two numbers are defined, and are displayed on the console.
The method is called by passing these two numbers.
Based on the output, relevant message is displayed on the console.
- Golang Program to check if two numbers are Amicable Numbers
- How to check if two numbers (m,n) are amicable or not using Python?
- Python program to check if binary representation of two numbers are anagram.
- Java program to check if binary representations of two numbers are anagram
- Check if binary representations of two numbers are anagram in Python
- Check if sum of divisors of two numbers are same in Python
- Python program to add two numbers
- C++ Program to check if given numbers are coprime or not
- Python Program to check if two given matrices are identical
- Check if list contains consecutive numbers in Python
- Program to find missing numbers from two list of numbers in Python
- C# program to check if two matrices are identical
- Java Program to check if two dates are equal
- Java Program to Check if two strings are anagram
- C++ Program to Add Two Numbers