0

Not Yet Answered # Problems on my first project

jlm699 320 woooee 813 Discussion Starter pywriter 3 Discussion Starter pywriter 3 Discussion Starter pywriter 3 Discussion Starter pywriter 3 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

0

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...

0

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
```

0

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

0

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

0

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

0

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

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...