Hi guys and gals
I wrote someting like this:

s1=s2=0

p=[]

for x in range(200,250,1):

	for d in range(2,x):

		if x%d==0:

			s1=s1+x/d

	for d in range(2,s1+1,1):

		if (s1+1)%d==0:

			s2=s2+(s1+1)/d

	if (s2+1)==x:

		p1.append((x,s2+1))
print p

but when i interpret\compile it,it prints an empty list
thanks in advance

Dani AI

Generated

Quick diagnosis and where the original code went wrong (building on and ): the empty list was mainly caused by two small state bugs — appending to the wrong list (p1 vs p) and never resetting the running sums for each candidate number so the sums accumulated across iterations. The divisor loops also excluded 1 and then tried to patch that with +1, which made the logic fragile. ’s advice to print intermediate sums is exactly the right debugging step for this class of bug.

A clearer, safer approach is: compute the sum of proper divisors for a single n (include 1 for n>1), use that to look up the partner, and avoid repeated work. The common fast method iterates up to sqrt(n) and adds both divisors when found:

def sum_proper_divisors(n):
    if n <= 1:
        return 0
    total = 1
    i = 2
    while i * i <= n:
        if n % i == 0:
            total += i
            if i != n // i:
                total += n // i
        i += 1
    return total

Memoize sums and only record an amicable pair once (for example require a < b). Exclude perfect numbers by checking b != a:

memo = {}
def s(n):
    if n not in memo:
        memo[n] = sum_proper_divisors(n)
    return memo[n]

pairs = []
for a in range(start, end + 1):
    b = s(a)
    if b != a and b >= start and s(b) == a and a < b:
        pairs.append((a, b))

Troubleshooting notes: test with known cases such as (220, 284) and perfect numbers like 6, 28, 496. If running under Python 3 be careful: use // for integer division when needed (the code above already uses //). For large ranges consider a sieve-style precomputation of divisor sums for O(n log n) performance.

Recommended Answers

All 6 Replies

Perhaps because you are appending to p1 instead of p ?

although even after replacing p1 with p the list shows up empty... what exactly are you trying to do here?

I have a feeling it's an indentation error...

I would also suggest that you print s1 and s2 at the appropriate points so you can see what is going on. For example in the following snippet, what is the value of s1 if x%d does not equal zero and how does that effect s2?

if x%d==0:
			s1=s1+x/d

thanks jlm and woo
in fact in the main program i wrote p instead of p1(to the jlm)
have heard about amicble number?
i worte this program to find them.
as you guess they are pair of nubers that sum of one's proper divisors(except itself)equals the other.
what do you mean woooee?
and thanks again
i must tell you that u found the code (in a very strange fact when i was searching about amicable numbers in wikipedia!)
thanks again

as i told you i found the code
here it is:

# Definition of the function

def amicable_numbers(x,y):

    # Sum all values i in [1,x) where i divides x

    sum_x = sum(i for i in xrange(1, x) if x % i == 0)

    sum_y = sum(i for i in xrange(1, y) if y % i == 0)

    return (sum_x == y) and (sum_y == x)

 
# Program body

n_1=int(raw_input('Enter nº 1: '))

n_2=int(raw_input('Enter nº 2: '))


if amicable_numbers(n_1,n_2):

    print 'Amicable! :)'

else:

    print 'Not Amicable :('

and i changed it to:

# Definition of the function

def amicable_numbers(start,end):

	a=[]

	for x in xrange(start,end):

		# Sum all values i in [1,x) where i divides x

		sum_x = sum(i for i in xrange(1, x) if x % i == 0)

		sum_y = sum(i for i in xrange(1, sum_x) if sum_x % i == 0)

		if(sum_y == x):

			a.append((x,sum_x))

	print a	
 

# Program body

start=int(raw_input("Enter the beginning number : "))

end=int(raw_input("Enter the end number : "))

amicable_numbers(start,end)

but it has a problem you run it you will see pairs that its numbers are the same.
please help again
thanks

boy oh boy.:icon_lol:i'm really excited.:icon_lol:because i discovered something exciting.
the thing that i called it "problem" in the last post is not what i tought.
in fact i discovered that some of amicable pairs has same members lik 496
hey men try it
it's really exciting

i found that numbers like 496 that the sum of theirs proper divisors except themselves will be them are not amicable pairs with the same members.in fact theu won't make pairs.they are perfect numbers.
so i released the last version of my program that finds amicable pairs and perfect numbers seperately
it's here:

# Definition of the function
def
amicable_numbers(start,end):

	a=[]

	p=[]

	for x in xrange(start,end+1):

		# Sum all values i in [1,x) where i divides x

		sum_x = sum(i for i in xrange(1, x) if x % i == 0)

		sum_y = sum(i for i in xrange(1, sum_x) if sum_x % i == 0)

		if(sum_y == x):

			if not((sum_x,x) in a):	

				 if sum_x!=x:

					a.append((x,sum_x))

				else:

					p.append(x)

	print "Amicable pairs : \n",a,"\n","Perfect numbers : \n",p,"\n"

 
# Program body

start=int(raw_input("Enter the beginning number : "))

end=int(raw_input("Enter the end number : "))

amicable_numbers(start,end)

but i still want to know what is wrong with the code i wrote in the first post.
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.