Find Amicable numbers using Python

Amicable numbers are two different numbers related in such a way that the sum of the proper divisors of each is equal to the other number.

The smallest pair of amicable numbers is (220284). They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.)


Below Python code can be used to identify Amicable numbers from a list.


def sumofdivident(num):
sum = 0
for i in range(1,num):
if num % i == 0:
sum += i
return sum

def isamicable(num1 , num2):
return sumofdivident(num1) == num2 and sumofdivident(num2) == num1

def findpairs(mylist):
pairs = []
for i in range(len(mylist)):
for j in range(i + 1 , len(mylist)):
if isamicable(mylist[i],mylist[j]):
pairs.append((mylist[i],mylist[j]))

return pairs


arr2 = [2620, 2924, 5020, 5564, 6232, 6368]

print(findpairs(arr2))

Comments

  1. I'm not sure exactly why but this web site is loading extremely slow for me. Is anyone else having this problem or is it a issue on my end? I'll check back later and see if the problem still exists.soft commodities outlook

    ReplyDelete

Post a Comment

Popular posts from this blog

SCD 2 Using Merge statement in SQL server

Flatten / Parsing XML using Azure ADF (Azure data factory)

Integer division to float division in SQL server