How to find the sum of all multiples of 3 or 5 below 1000 in Python?

Not sure if I should have posted this on math.stackexchange instead, but it includes more programs, so I posted it here.

The question seems very simple, but I sat here for at least an hour without figuring it out. I tried different solutions and read mathematical formulas for it, etc., but this does not give me the correct answer when coding! I made two different decisions for him, and that gives me the wrong answer. The first solution gives me 265334, and the second gives me 232169. The answer is 233168, so the second solution is closer.

I must mention that this is a question from Project Euler, the first one to be exact.

Here is my code. Any ideas what's wrong?

nums = [3, 5] max = 999 result = 0 for num in nums: for i in range(1,max): if num*i < max: result += num*i print result result = 0 for i in range(0,max): if i%3 == 0 or i%5 == 0: result += i print result 
+7
source share
15 answers

range(k,max) does not include max , so you really check and include 998 (while 999 is a multiple of 3). Use range(1,1000) instead.

+4
source

You exaggerate things. You just need a list of numbers that are multiples of 3 or 5, which you can easily get from the list :

 >>> [i for i in range(1000) if i % 3 == 0 or i % 5 == 0] 

Then use sum to get the total:

 >>> sum([i for i in range(1000) if i % 3 == 0 or i % 5 == 0]) <<< 233168 

Or even better, use a generator expression:

 >>> sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0) 

Or better yet (courtesy of Exelian):

 >>> sum(set(list(range(0, 1000, 3)) + list(range(0, 1000, 5)))) 
+12
source

The problem with your first solution is that it doubles the number of multiples of 15 (because they are multiples of both 3 and 5).

The problem with your second solution is that it does not count 999 (a multiple of 3). Just set max = 1000 to fix it.

+2
source

I like the most:

 def divisibles(below, *divisors): return (n for n in xrange(below) if 0 in (n % d for d in divisors)) print sum(divisibles(1000, 3, 5)) 
+2
source
 max = 1000 # notice this here result = 0 for i in range(0,max): if i%3 == 0 or i%5 == 0: result += i print result 

This works, but use 1000 for max, so it also includes 999.

+1
source

I know this was 3 months ago, but as an experiment, because I am new to python. I decided to try and combine some other answers, and I came up with a method with which you can pass the maximum number and divisors as a list and returns the sum:

 def sum_of_divisors(below, divisors): return sum((n for n in xrange(below) if 0 in (n % d for d in divisors))) max = 1000 nums = [3, 5] print sum_of_divisors(max, nums) 
+1
source

result = 0

for i in the range (0,1000):

 if (i % 3 == 0 or i % 5 == 0): print i result = result + i 

print result


0

3

5

6

nine

. ,.

993

995

996

999

233168

+1
source

You can also use functional programming tools (filter):

 def f(x): return x % 3 == 0 or x % 5 == 0 filter(f, range(1,1000)) print(x) 

Or use two lists with a subtraction of a multiple of 15 (which appear in both lists):

 sum1 = [] for i in range(0,1000,3): sum1.append(i) sum2 = [] for n in range(0,1000,5): sum2.append(n) del sum2[::3] #delete every 3-rd element in list print(sum((sum1)+(sum2))) 

I like this solution, but I guess he needs some improvements ...

0
source

Here you go:

 count = 1000 m = [3, 5, 3*5] result = 0 Sum = 0 for j in m: result = 0 for i in range(count): if i*j < 1000: result = result + i*j elif i == (count - 1): if j < 15: Sum = result + Sum elif j == 15: Sum = Sum - result print Sum 
0
source

here is my solution:

 for n in range(100): if n % 5==0: if n % 3==0: print n, "Multiple of both 3 and 5" #if the number is a multiple of 5, is it a multiple of 3? if yes it has has both. elif n % 5==0: print n, "Multiple of 5" elif n % 3==0: print n, "Multiple of 3" else: print n "No multiples" 
0
source

There are multi-storey (999/3) multiples of 3, gender (999/5) multiples of 5 and gender (999/15), multiples of 15 less than 1000.

For 3 it is: 3 + 6 + 9 + 12 + ... + 999 = 3 * (1 + 2 + 3 + 4 + ... + 333)

= 3 * (333 * 334/2), since the sum of integers from 1 to k is equal to k * (k + 1) / 2.

Use the same logic for the sum of multiples of 5 and 15. This gives a permanent workaround. Summarize this for arbitrary inputs.

0
source

I know that this is from 6 years ago, but I just thought that id uses a solution that was found from a mathematical formula, which seemed to me to be interesting, since it eliminates the need to iterate over all numbers.

https://math.stackexchange.com/a/9305

 def sum_of_two_multiples(nums, maxN): "takes tuple returns multiples under maxN (max number - 1)" n1, n2 = nums = nums[:2] maxN -= 1 def k(maxN, kx): n = int(maxN / kx) return int(kx * (0.5 * n * (n+1))) return sum([k(maxN, n) for n in nums]) - k(maxN, n1*n2) 

Displays the following

 print(sum_of_two_multiples((3,5), 10)) # 23 print(sum_of_two_multiples((3,5), 1000)) # 233168 print(sum_of_two_multiples((3,5), 10**12)) # 233333333333166658682880 
0
source

this is my decision

 sum = 0 for i in range (1,1000): if (i%3)==0 or (i%5)==0: sum = sum + i print(sum) 
0
source
 count = 0 for i in range(0,1000): if i % 3 == 0 or i % 5 ==0: count = count + i print(count) 
0
source

I think that only the last few lines from your code are important. The or operator is the key operator in this code. In addition, to set the maximum value to 999, you must set it to 1000 so that it covers all values. Here is my code.

  ans=0 for i in range(1,1000): if(i%3==0 or i%5==0): ans += i print(ans) input('press enter key to continue');#this line is only so that the screen stays until you press a key 
0
source

All Articles