Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70425 > unrolled thread
| Started by | Ivan Ivanivich <ivriabtsov@gmail.com> |
|---|---|
| First post | 2014-04-20 11:43 -0700 |
| Last post | 2014-04-21 23:43 +1000 |
| Articles | 9 — 6 participants |
Back to article view | Back to comp.lang.python
symple programming task Ivan Ivanivich <ivriabtsov@gmail.com> - 2014-04-20 11:43 -0700
Re: symple programming task Chris Angelico <rosuav@gmail.com> - 2014-04-21 05:02 +1000
Re: symple programming task Peter Otten <__peter__@web.de> - 2014-04-20 21:11 +0200
Re: symple programming task Joel Goldstick <joel.goldstick@gmail.com> - 2014-04-20 15:15 -0400
Re: symple programming task Ivan Ivanivich <ivriabtsov@gmail.com> - 2014-04-20 12:27 -0700
Re: symple programming task Joshua Landau <joshua@landau.ws> - 2014-04-21 12:43 +0100
Re: symple programming task Ivan Ivanivich <ivriabtsov@gmail.com> - 2014-04-21 06:21 -0700
Re: symple programming task Tim Chase <python.list@tim.thechases.com> - 2014-04-21 08:39 -0500
Re: symple programming task Chris Angelico <rosuav@gmail.com> - 2014-04-21 23:43 +1000
| From | Ivan Ivanivich <ivriabtsov@gmail.com> |
|---|---|
| Date | 2014-04-20 11:43 -0700 |
| Subject | symple programming task |
| Message-ID | <cc416b4b-e19d-49b9-abc2-86bcff455f9c@googlegroups.com> |
hi all, i have simple programming task: [quot] If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. [/quote] this task from http://projecteuler.net/ site I wrote a solution in python http://pastebin.com/QXtNuRWU this script returned correctly result if "basis < 10", but if "basis < 1000" result is 266333 and it is not correctly answer on site http://projecteuler.net it is my script problem or site not working correctly? thanks sorry for my english
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-04-21 05:02 +1000 |
| Message-ID | <mailman.9384.1398020548.18130.python-list@python.org> |
| In reply to | #70425 |
On Mon, Apr 21, 2014 at 4:43 AM, Ivan Ivanivich <ivriabtsov@gmail.com> wrote: > [quot] > If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. > > Find the sum of all the multiples of 3 or 5 below 1000. > [/quote] > > this task from http://projecteuler.net/ site > > I wrote a solution in python > > http://pastebin.com/QXtNuRWU > > this script returned correctly result if "basis < 10", but if "basis < 1000" result is 266333 and it is not correctly answer on site http://projecteuler.net Try listing the actual numbers you're summing, and check if there's a problem there. Are all the numbers you expect appearing? Are any you don't want there? (I can see exactly what your problem is, but I'd rather give hints rather than simply tell you outright what's wrong.) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-04-20 21:11 +0200 |
| Message-ID | <mailman.9385.1398021090.18130.python-list@python.org> |
| In reply to | #70425 |
Ivan Ivanivich wrote:
> hi all, i have simple programming task:
>
> [quot]
> If we list all the natural numbers below 10 that are multiples of 3 or 5,
> we get 3, 5, 6 and 9. The sum of these multiples is 23.
>
> Find the sum of all the multiples of 3 or 5 below 1000.
> [/quote]
>
> this task from http://projecteuler.net/ site
>
> I wrote a solution in python
>
> http://pastebin.com/QXtNuRWU
[for small scripts it is fine to include them directly in your post]
> #!/usr/bin/env python3.2
>
> total = 0
> for divider in 3, 5:
> basis=divider
> while basis < 1000:
> mod = basis % divider
> if mod == 0:
> total = total + basis
> print("total = ", total, "basis = ", basis)
>
> basis += 1
>
> print("total", total)
> this script returned correctly result if "basis < 10", but if "basis <
> 1000" result is 266333 and it is not correctly answer on site
> http://projecteuler.net
>
> it is my script problem or site not working correctly?
Your script. Try it for the numbers below 20, say, and then compare to a
result you calculated with pen and paper.
Or look closely at the output produced by the line
> print("total = ", total, "basis = ", basis)
in your code.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2014-04-20 15:15 -0400 |
| Message-ID | <mailman.9386.1398021329.18130.python-list@python.org> |
| In reply to | #70425 |
[Multipart message — attachments visible in raw view] — view raw
On Sun, Apr 20, 2014 at 3:02 PM, Chris Angelico <rosuav@gmail.com> wrote: > On Mon, Apr 21, 2014 at 4:43 AM, Ivan Ivanivich <ivriabtsov@gmail.com> > wrote: > > [quot] > > If we list all the natural numbers below 10 that are multiples of 3 or > 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. > > > > Find the sum of all the multiples of 3 or 5 below 1000. > > [/quote] > > > > this task from http://projecteuler.net/ site > > > > I wrote a solution in python > > > > http://pastebin.com/QXtNuRWU > > > > this script returned correctly result if "basis < 10", but if "basis < > 1000" result is 266333 and it is not correctly answer on site > http://projecteuler.net > > Try listing the actual numbers you're summing, and check if there's a > problem there. Are all the numbers you expect appearing? Are any you > don't want there? > > (I can see exactly what your problem is, but I'd rather give hints > rather than simply tell you outright what's wrong.) > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > I second Chris's comments. Also, better to paste your code in email. More people will look at it, especially since it is short. It looks like you are looping thru all the numbers twice -- once for each divisor. You don't need to do that. You can loop thru and test for each divisor on each loop. You might want to ditch the while loop and use a for loop over a range: for i in range(1000): Its a more pythonic idiom. Also use 4 spaces, not 8 for indents. Minor points. Print() is your friend -- Joel Goldstick http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Ivan Ivanivich <ivriabtsov@gmail.com> |
|---|---|
| Date | 2014-04-20 12:27 -0700 |
| Message-ID | <313537d1-28f6-47fe-82d9-20e4cba41f73@googlegroups.com> |
| In reply to | #70425 |
On Sunday, April 20, 2014 10:43:37 PM UTC+4, Ivan Ivanivich wrote: > hi all, i have simple programming task: > > > > [quot] > > If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. > > > > Find the sum of all the multiples of 3 or 5 below 1000. > > [/quote] > > > > this task from http://projecteuler.net/ site > > > > I wrote a solution in python > > > > http://pastebin.com/QXtNuRWU > > > > this script returned correctly result if "basis < 10", but if "basis < 1000" result is 266333 and it is not correctly answer on site http://projecteuler.net > > > > it is my script problem or site not working correctly? > > > > thanks > > > > sorry for my english thanks, i found the bag
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2014-04-21 12:43 +0100 |
| Message-ID | <mailman.9401.1398080632.18130.python-list@python.org> |
| In reply to | #70429 |
On 20 April 2014 20:27, Ivan Ivanivich <ivriabtsov@gmail.com> wrote: > thanks, i found the bag G'day. This [https://xkcd.com/979/] applies to threads ending in "nvm, solved it" too. I know the problem in your case isn't likely to be widely useful, but there are other benefits of pointing out what you've done. For example the list members can tell you if your solution misses anything.
[toc] | [prev] | [next] | [standalone]
| From | Ivan Ivanivich <ivriabtsov@gmail.com> |
|---|---|
| Date | 2014-04-21 06:21 -0700 |
| Message-ID | <7f496a70-6b85-4bc9-bf33-7aeaad813d11@googlegroups.com> |
| In reply to | #70425 |
On Sunday, April 20, 2014 10:43:37 PM UTC+4, Ivan Ivanivich wrote:
> hi all, i have simple programming task:
>
>
>
> [quot]
>
> If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
>
>
>
> Find the sum of all the multiples of 3 or 5 below 1000.
>
> [/quote]
>
>
>
> this task from http://projecteuler.net/ site
>
>
>
> I wrote a solution in python
>
>
>
> http://pastebin.com/QXtNuRWU
>
>
>
> this script returned correctly result if "basis < 10", but if "basis < 1000" result is 266333 and it is not correctly answer on site http://projecteuler.net
>
>
>
> it is my script problem or site not working correctly?
>
>
>
> thanks
>
>
>
> sorry for my english
my bag is: Adding twice the same elements to the total
for exemple:
for divider in 3, 5:
basis=divider
while basis < 1000:
mod = basis % divider
if mod == 0:
total = total + basis
if "basis" is 15, then "mod" == 0 twice - when the "divider" is 3 and 15
my new version of script:
total = 0
div1 = 3
div2 = 5
for basis in range(0, 1000):
mod = basis % div1
if mod == 0:
total = total + basis
continue
mod = basis % div2
if mod == 0:
total = total + basis
continue
print("total = ", total)
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-04-21 08:39 -0500 |
| Message-ID | <mailman.9406.1398087564.18130.python-list@python.org> |
| In reply to | #70459 |
On 2014-04-21 06:21, Ivan Ivanivich wrote:
> > Find the sum of all the multiples of 3 or 5 below 1000.
> my new version of script:
>
> total = 0
> div1 = 3
> div2 = 5
> for basis in range(0, 1000):
> mod = basis % div1
> if mod == 0:
> total = total + basis
> continue
> mod = basis % div2
> if mod == 0:
> total = total + basis
> continue
>
>
>
> print("total = ", total)
Now that you have a working solution, I don't mind giving my more
pythonic solution:
sum(dividend for dividend in range(1000)
if any(dividend % divisor == 0 for divisor in (3, 5)))
which succinctly states the problem and makes it easy to
add/remove/change the divisors in one place rather than having to
define multiple variables to hold them and "if" statements to
evaluate them.
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-04-21 23:43 +1000 |
| Message-ID | <mailman.9407.1398088190.18130.python-list@python.org> |
| In reply to | #70459 |
On Mon, Apr 21, 2014 at 11:21 PM, Ivan Ivanivich <ivriabtsov@gmail.com> wrote: > if "basis" is 15, then "mod" == 0 twice - when the "divider" is 3 and 15 Good! Yes, you worked out exactly what the problem is. :) There are ways to simplify your code, but it's now giving the correct result, so that's the most important thing. ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web