In this simple python program, we need to check the two numbers are amicable numbers. It is a number-based python program.
For a better understanding of this example, we always recommend you to learn the basic topics of Python programming listed below:
This python program example is for checking Amicable numbers, The numbers are said to be amicable numbers if the sum of proper divisors of one number will be equal to another number and vice versa.
For example, take two numbers 220 and 284, and check the numbers are amicable, so take the divisors of the first number 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110 and taking the sum of these numbers is 284. now check the divisors of 284 are 1, 2, 4, 71 and 142 and taking the sum of these divisors will give the result as 220. so these numbers are Amicable numbers.
In this simple python program, we accept the numbers from the user and we initialize two variables for the sum of two number's divisors. Then open a for loop to calculate all the divisors of the first number and open another
for loop to find all the divisors of other numbers. in both the loops we add the divisors with the respective sum variables using operators in python language.
Now use an if condition in a python programming language to check sum1 is equal to number 2, and sum 2 is equal to number 1. if so, print the numbers are Amicable. Else not amicable.
STEP 1: Accept the numbers from the user using the input function in python programming basics.
STEP 2: Initialize two different sum variables for calculating the sum of divisors of each number.
STEP 3: Open a
for loop from 0 to the number 1 to check all numbers for that divisors or not.
STEP 4: Using the
if condition and mod operator, check all numbers for any divisors and add the divisors to the sum.
STEP 5: Open another for loop for the number 2 and do the same as we do the number one like check for any divisor and add the divisor to the sum.
STEP 6: Using another
if condition in the python programming language to check the sum 1 is equal to number 2 and vice versa.
STEP 7: If the condition satisfies print, the numbers are amicable else not using print statements in python language.
x=int(input('Enter number 1: ')) y=int(input('Enter number 2: ')) sum1=0 sum2=0 for i in range(1,x): if x%i==0: sum1+=i for j in range(1,y): if y%j==0: sum2+=j if(sum1==y and sum2==x): print('numbers are amicable') else: print('numbers are not amicable')
Enter number 1: 220 Enter number 2: 284 numbers are amicable