[Tutor] Add all natural numbers that are multiples of 3 and 5

Kent Johnson kent37 at tds.net
Sun Jan 4 04:43:08 CET 2009


On Sat, Jan 3, 2009 at 9:43 PM, Benjamin Serrato
<benjamin.serrato at gmail.com> wrote:
> I changed it to this:
>
> #!/usr/local/bin/python3.0
> #problem1.py
>
> """Lists the sum of all multiples of 3 and 5 less than 1000."""
>
> def sumTotal(multipleInitial, multiple):    # This awkward solution because
>    total = 0                                          #
> mulitipleInitial = multiple just points
>    n = 2                                              # a new
> variable to the same memory

You don't have to pass the duplicate arguments. It's true that
  multipleInitial=multiple
makes multipleInitial reference the same value as multiple, but
  multiple = multipleInitial * n
make mulltiple refer to a new value while leaving multipleInitial alone.

This might help:
http://personalpages.tds.net/~kent37/kk/00012.html

>    while multiple < 1000:                      # space
>        total = total + multiple
>        multiple = multipleInitial * n
>        n = n + 1
>    return total
>
> print(sumTotal (3, 3) + sumTotal(5,5))
>
> -----
> I think it does what I wanted it to do, but Kent pointed out I wanted
> it to do was a false solution.  So, I can create a list of all common
> multiples below 1000, sum them, subtract them from 266 333.  Or, what
> I prefer, create a list of all multiples, checking against that list
> for a multiple before adding a new multiple.  But, I don't know how to
> work with lists so I'll be back in a day or two.

You might want to look at the sum() and range() functions.

Kent

PS Please subscribe to the list.


More information about the Tutor mailing list