symple programming task

I

Ivan Ivanivich

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
 
C

Chris Angelico

[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.

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[/QUOTE]

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
 
P

Peter Otten

Ivan said:
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.

this task from http://projecteuler.net/ site

I wrote a solution in python

http://pastebin.com/QXtNuRWU[/QUOTE]

[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.
 
J

Joel Goldstick

[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.

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[/QUOTE]

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
 
I

Ivan Ivanivich

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.



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[/QUOTE]

thanks, i found the bag
 
J

Joshua Landau

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.
 
I

Ivan Ivanivich

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.



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[/QUOTE]

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)
 
T

Tim Chase

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
 
C

Chris Angelico

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top