Python Forum

Full Version: Amicable numbers.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to create a program, which will find pairs of amicable numbers lower than n.
I wrote a cod :
def sumdiv(n):
    Sum = 0
    for x in range(1, n // 2 + 1):
        if n % x == 0:
            Sum += x
    return Sum

def amicable(n):
    for x in range(1, n):
        if sumdiv(sumadiv(x)) == x and sumdiv(x) != x:
            print(x, "and", sumdiv(x), "are amicable")



m=int(input("Any natural number:"))
print(amicable(m))
I have problem, because I don't know why I get double pairs, for example (220,284) and (284,220).
You could make sure that the other part of the pair is always larger: and sumdiv(x) > x
Or you could keep a list of each matching number, and check that any future matches aren't already in that list.
Thank You!