python - Adding up contents of list - project euler 1 -


i'm trying project euler problem 1 in python (http://projecteuler.net./problem=1) , i'm using while loop loop 1000:

from collections import counter  x = 0 target = 1000 correctmultiples = list() while x < target:     x += 1     if x % 3 == 0 or x % 5 == 0:         correctmultiples.append(x)  print(str(correctmultiples) + ' multiples of 3 or 5')  print('the sum of multiples of 3 or 5 under 1000 is, ' + str(sum(correctmultiples))) # reason, 1000 over, answer 233168 not 234168 

this works answer i'm getting 1000 over. 234168 instead of 233168.

i've tried checking duplicates: (following how find duplicate elements in array using loop in python?)

duplicates = counter(correctmultiples) print([i in duplicates if duplicates[i] > 1]) 

but don't think there can duplicates can they? becuase i'm using if x % 3 or ...

i know isn't efficient method, still... why doesn't work? can me find why answer 1000 over? thanks

you include 1000 in loop, while question ask numbers below 1000.

here increment after doing boundary check, when x==999 still run loop:

while x < target:     x += 1 

this easier for loop:

for x in range(1000): 

range not include last element.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -