Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Amicable numbers.
#1
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).
Reply
#2
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.
Reply
#3
Thank You!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,369 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,935 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,053 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020