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

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 place.in 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 yet.if 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.