Amicable numbers. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Amicable numbers. (/thread-16958.html) |
Amicable numbers. - pawlo392 - Mar-21-2019 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). RE: Amicable numbers. - nilamo - Mar-21-2019 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. RE: Amicable numbers. - pawlo392 - Mar-21-2019 Thank You! |