# Thursday Code Puzzler: Sum of Multiples

It's Thursday, so it's time for another code puzzler. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.

*Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer. It's all about the participation!*

**Find the Multiples **

This weeks task is to find all the sum of all multiples of 3 and 5 under 1,000. For example, the sum of multiples of 3 and 5 under 10 would be 23 (3 + 5 + 6 + 9).

Catch up on all our previous puzzlers here

Tags:

## Comments

## Dawid Solecki replied on Thu, 2013/06/13 - 6:27am

## Philip Livesey replied on Thu, 2013/06/13 - 6:38am

I came up with two methods - a simple one and then one using the fact that every 7 steps the pattern repeats itself (it is quicker than the brute force method on larger numbers):

## Frank Dietrich replied on Thu, 2013/06/13 - 8:34am

Java

edit:missed the "under 1000"## Murali Chevuri replied on Thu, 2013/06/13 - 7:54am

## Michael Berestov replied on Fri, 2013/06/14 - 5:38am

It's more a math task than programming task:

## sun east replied on Thu, 2013/06/13 - 7:44am

In python, version 1(1 line code):

In python, version 2(3 lines code)

## sun east replied on Thu, 2013/06/13 - 7:47am

In Scala (1 line code)

## Michael Berestov replied on Thu, 2013/06/13 - 8:11am in response to: sun east

I think correct answer is

## Christian Rubiales replied on Thu, 2013/06/13 - 8:41am

## sun east replied on Thu, 2013/06/13 - 8:54am in response to: Michael Berestov

Actually your think is wrong, your code is wrong also. It have calculated the multipliers of 15 twice.

So your should change it as:

## Michael Berestov replied on Thu, 2013/06/13 - 9:52am in response to: sun east

Ahh, thanks for pointing out! I got task wrong - thought it should have all values for both.

Anyway formulas are still correct, and all I need is just to exclude duplicates, i.e. like you said exclude sum for 15.

P.S. and yes, correct answer is

`233168`

## andy darlow replied on Fri, 2013/06/14 - 5:40am

Phew, that Java was a hard slog! Scala to the rescue. Here's the quick hack 1 liner....

(1 until 1000) filter(x=> x%3==0 || x%5==0) sum

## Paulo Ortolan replied on Thu, 2013/06/13 - 11:56am

Here's my code, a little verbose, but it works!

## Ondřej Smola replied on Thu, 2013/06/13 - 12:07pm

## Girish Kolanthra replied on Thu, 2013/06/13 - 1:22pm

Multiple of 3 and 5? or is it Multiples of 3 or 5. Assuming it is multiples of 3 or 5 (as per your example)

## Rafael Naufal replied on Thu, 2013/06/13 - 3:58pm

## James Smitley replied on Wed, 2013/06/19 - 8:11am

Late to the party, but have to include the Groovy solution:

`groovy:000> (1..999).findAll { (it % 3 == 0) || (it % 5 == 0) }.sum() ===> 233168`

## Web Gyver replied on Wed, 2013/06/19 - 10:34am

Plain JavaScript (no jQuery or any other libraries:

## Mark Fisher replied on Wed, 2013/06/19 - 10:11am

Clojure example (I'm fairly new to clojure, so don't shoot me if there's a better way)

Result: 233168

## Phil Campbell replied on Wed, 2013/06/19 - 10:15am

I think Gauss would have seen it differently

Take the sequence of multiples of 3 less than 1000 and then reverse and addso the total is 1002 * no of terms (i.e 999 /3 terms), but we added the sum twice

giving sum of multiples of 3 as 1002 *(999/3) /2

for multiples of 5 less than 1000 you get 1000 *(995 /5) / 2

for multiples of 15 less than 1000 gives you 1005*(990/15) /2

so total is (1002 * 999/3 + 1000* 995/5 - 1005 *990/15) / 2 which my trusty casio gives as 233168.

If this was to find the total of the numbers less than 500 billion, I don't think I would like to iterate.

... and yes I wrote the program first too and yes it was much more fun. :-)

## Jide Oladepo replied on Wed, 2013/06/19 - 10:18am

Haskell (Using list comprehension)

sum [x | x <- [1..999], (x `mod` 3 == 0 || x `mod` 5 == 0)]

## Mark Fisher replied on Wed, 2013/06/19 - 11:00am in response to: Phil Campbell

Ah yes, classic arithmetic progression.

Similarly it can be broken down into 2 sums of "1 to n" (= n*(n+1)/2) less the overlaps being (3*333*334 + 5*199*200 -15*66*67) / 2 = 233168

In effect a rearrangement of your own version.

(I don't like the threading in this forum, makes this look like a standard post?)

## Manas Nayak replied on Wed, 2013/06/19 - 11:22am

sum3 = 3(1+2+3+4+5+...)

similarly sum5 = 5(1+2+3+4+5+...)

duplicates: sum15 = 15(1+2+3+...) so total = sum3+sum5-sum15

## Frédéric Vergez replied on Wed, 2013/06/19 - 12:41pm

After all, somebody submitted Python code and even Scala, so my 2 cents of Haskell:

`Prelude> sum $ filter (\x-> mod x 3 == 0 || mod x 5 == 0) [1..999] 233168`

This is one of the reason i gave up Java after more than 10 years of a love-hate relationship...

## Miles Zarathustra replied on Wed, 2013/06/19 - 5:02pm in response to: James Smitley

(in reply to the previous groovy submission)

You're so verbose!

(1..<1000).findAll { !(it%3 && it%5) }.sum()

UPDATE:

Here is a more elegant version in J, from my friend Joey:

+/ I. *:/ * 3 5 |/ i.1000

http://www.jsoftware.com/

## Dan Zhuo replied on Wed, 2013/06/19 - 3:46pm

Given X = 3, Y = 5 and Max = 1000 (IF include Max, otherwise Max = Max -1)

Sum = { X * (1 + Max / X) * (Max / X) / 2 } + { Y * (1 + Max / Y) * (Max / Y) / 2 } = 266,333

IF allow include MAX, the Sum = 267,333

## Narendra Shah replied on Thu, 2013/06/20 - 12:00am

## Igwe Kalu replied on Thu, 2013/06/20 - 1:19am

There 2 different implementations below:

Run it:

## Mark Fisher replied on Thu, 2013/06/20 - 8:08am in response to: Dan Zhuo

surely in { X * ... * (Max/X) ...} the X cancels itself.

Also, you've doubled the cases where both 3 and 5 hit, i.e. removing multiples of 15 as they are double counted.

## Mark Fisher replied on Thu, 2013/06/20 - 2:50am in response to: Narendra Shah

you've missed the cases where i%5 == 0