<br><br><div class="gmail_quote">On Mon, Jan 12, 2009 at 5:23 PM, WM. <span dir="ltr"><<a href="mailto:wferguson1@socal.rr.com" target="_blank">wferguson1@socal.rr.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
# The natural numbers(natnum), under 1000, divisible by 3 or by 5 are to be added together.<br>
natnum = 0<br>
num3 = 0<br>
num5 = 0<br>
cume = 0<br>
# The 'and' is the 15 filter; the 'or' is the 3 or 5 filter.<br>
while natnum <= 999:<br>
num3 = natnum/3<br>
num5 = natnum/5<br>
if natnum - (num3 * 3) == 0 and natnum - (num5 * 5) == 0:<br>
cume = cume + natnum<br>
elif natnum - (num3 * 3) == 0 or natnum - (num5 * 5) == 0:<br>
if natnum - (num3 * 3) == 0:<br>
cume = cume + natnum<br>
elif natnum - (num5 * 5) == 0:<br>
cume = cume + natnum<br>
natnum = natnum + 1<br>
print cume<br>
<br>
<br>
This problem was kicked around last month and I did not understand any of the scripts. So I tried to recall the BASIC ifs and loops. The project euler guys say it works, but how might it be made more Pythonic?<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br><br clear="all">I guess using a list comprehension will work<br>>>>x = [number for number in range(1000) if number%3 ==0 or number%2 ==0]<br>>>> sum(x)<br>333167<br>>>> <br>
<br>-- <br>لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي<br>"No victim has ever been more repressed and alienated than the truth"<br><br>Emad Soliman Nawfal<br>Indiana University, Bloomington<br>
<a href="http://emnawfal.googlepages.com" target="_blank">http://emnawfal.googlepages.com</a><br>--------------------------------------------------------<br>