View Single Post
  #2 (permalink)  
Old 05-16-2008, 12:10 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,445
Last Blog:
CherryPy(thon)
Rep Power: 27
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Amicable numbers

Well, you have a function for checking whether two numbers are amicable or not. Now you only need to test all the numbers between 1 and 10000, which can be done using two simple loops.

I'm a little confused about your question actually, because you say all the amicable numbers between 1 and 10000, but to find amicable numbers you need two numbers.

Well, here you have some code that will find all the amicable numbers where both the first number will go from 1 to 10000, and also the second number.
Code:
def amicable_numbers(x,y):
    sum_x=0
    sum_y=0
    for i in range(1,x):
        if x%i==0:
            sum_x+=i
 
    for k in range(1,y):
        if y%k==0:
            sum_y+=k
 
    return sum_x==y and sum_y==x

for first_number in range(1, 10000+1):
    for second_number in range(1, 10000+1):
        if amicable_numbers(first_number, second_number):
            print "Amicable numbers: %5d and %5d\n" % (first_number, second_number)
You'll have to be patient, though. It will take a long time for it to complete, because it will have to check so many numbers.
Reply With Quote