Amicable Numbers in Python
In this tutorial, we will learn how to get amicable numbers in Python. First, we will understand what amicable numbers are and how they can be used.
Amicable numbers are two different numbers so related that the sum of the proper divisor of each is equal to the other number.
In other words, the sum of the all proper divisor of x and the sum of all proper divisor y should be equal to the opposite number.
Suppose we have two numbers, 123 and 426, we need to find all the proper divisors of 123 and 426; then we do the sum of the proper divisor of 123 and the same with the 426.
The sum of the proper divisor of 123 should equal 426, and the proper divisor of 426 should equal 123.
How to check if two numbers are amicable in Python
Following are the steps to find whether two numbers are amicable or not.
Enter first number : 220 Enter second number : 284 Given numbers are Amicable!
Let's see another output.
Enter first number : 365 Enter second number : 456 Given numbers are not Amicable!
In the above example, we take the user input and store it in separate variables. We used for loop and if statement to find the proper divisor of both the numbers. Then we find the sum of the proper divisors of both the numbers. Using the if statement, we checked if the sum of the proper divisor of the numbers equals the other number and vice-versa.
How to get count of pair of amicable pairs from the list
We have two lists of integer number, where we will pick each number from both lists and compare them. If the numbers are amicable, increment the count and return. Let's understand the following example.
This is a simple way to get the count of amicable number pair. We traverse each pair and check if they form an amicable pair.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week