max baseman wrote:
> thanks much you really helped me if anyone wants here is the program:
Hmmm...this is quite wordy. Some suggestions below:
>
> n2=2
> n3=3
> n4=4
> n5=5
> n6=6
> n7=7
> l2=[]
> l3=[]
> l4=[]
> l5=[]
> l6=[]
> l7=[]
> while n2 < 1000:
> l2.append(n2)
> n2=n2+2
This can be written
l2 = range(2, 1000, 2)
> while n3 < 1000:
> l3.append(n3)
> n3=n3+3
> while n4 < 1000:
> l4.append(n4)
> n4=n4+4
> while n5 < 1000:
> l5.append(n5)
> n5=n5+5
> while n6 < 1000:
> l6.append(n6)
> n6=n6+6
> while n7<1000:
> l7.append(n7)
> n7=n7+7
> possible=[]
> for num in l2:
> if num in l3 and num in l4 and num in l5 and num in l6:
> possible.append(num)
Testing for membership is better done with sets or dicts than lists. In
this case you want the intersection of l2,...,l6. Sets support
intersection directly. Also l2 and l3 are redundant; any number in l6
will also be in l2 and l3. So possible can be created as a set like this:
possible = set(xrange(4, 1000, 4)).intersection(xrange(5, 1000, 5)) \
.intersection(xrange(6, 1000, 6))
> for a in possible:
> if a+1 in l7:
> print a+1
Here you are checking if a is divisible by 7. You could do it directly
instead of creating a helper:
for a in possible:
if (a+1) % 7 == 0:
print a+1
Or you could apply a little more number theory and realize that to be a
multiple of 4, 5 and 6 it is necessary and sufficient to be a multiple
of 60 and write the whole thing in one loop:
for a in xrange(60, 1000, 60):
if (a+1) % 7 == 0:
print a+1
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor