[Tutor] HOW DO I PYTHONIZE A BASICALLY BASIC PROGRAM?

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Tue Jan 13 00:35:58 CET 2009


On Mon, Jan 12, 2009 at 5:23 PM, WM. <wferguson1 at socal.rr.com> wrote:

> # The natural numbers(natnum), under 1000, divisible by 3 or by 5 are to be
> added together.
> natnum = 0
> num3 = 0
> num5 = 0
> cume = 0
> # The 'and' is the 15 filter; the 'or' is the 3 or 5 filter.
> while natnum <= 999:
>    num3 = natnum/3
>    num5 = natnum/5
>    if natnum - (num3 * 3) == 0 and natnum - (num5 * 5) == 0:
>        cume = cume + natnum
>    elif natnum - (num3 * 3) == 0 or natnum - (num5 * 5) == 0:
>        if natnum - (num3 * 3) == 0:
>            cume = cume + natnum
>        elif natnum - (num5 * 5) == 0:
>            cume = cume + natnum
>    natnum = natnum + 1
> print cume
>
>
> 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?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


I guess using a list comprehension will work
>>>x = [number for number in range(1000) if number%3 ==0 or number%2 ==0]
>>> sum(x)
333167
>>>

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090112/1863f473/attachment.htm>


More information about the Tutor mailing list