# Project Euler #1 - Multiples of 3 and 5

### Peter Kim Frank Jun 11

I thought it would be fun to create a thread where the community could solve a problem from Project Euler. This is **Problem #1**:

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.

Look forward to seeing solutions in your language of choice!

Did you find this post useful? Show some love!

dev.to is where software developers stay in the loop and avoid career stagnation.
Signing up (for free!) is the first step.

Classic DEV Post from May 4

More from @peter

Assigned seats + missing ticket puzzle
#challenge

Project Euler #3 - Largest Prime Factor
#projecteuler
#challenge

Trending on dev.to

Who's looking for open source contributors? (August 13 edition)
#discuss
#opensource

Dev.To Discord Channel?
#discuss

How Do You Really Get Hired?
#careers
#beginners
#discuss

Being average in > 1 things is easier and more valuable than being excellent at 1 thing.
#career
#learning

Explain Hashing + salting Like I'm Five
#explainlikeimfive
#webdev

Tell me a good IT joke
#discuss
#jokes
#fun

What productivity tools/hacks do you find most effective for your day-to-day?
#discuss
#productivity

How to Get Un-Stuck
#debugging
#beginners
#juniordev
#career

I'll take a mathematical approach.

The sum of the first

npositive integers isn*(n+ 1)/2. Multiply byk, and you get the sum of the firstnmultiples ofk.There are 333 multiples of 3 below 1000, and 199 multiples of 5, so we get 166833 and 99500, respectively. But we can't just sum them, as we'd count the multiples of 15 twice (15 is their least common multiple), so we have to subtract those once (total: 33165).

Result: 233168

In general, let's use JavaScript for example:

There, no iterations, pure math π And

blazing fastβ‘Using JavaScript's new support of

`BigInt`

, it's immediate even with huge numbers like 123456789012345678901234567890: it's 3556368375755728575115582031196717989244271707186887161545.This is very clever and very nice!

I did this in LOLCODE (first program that I'm writing with it, probably not the best implementation)

πππππ

You can try it here

## Ruby

## JavaScript

It's a shame getting a range and a sum isn't quite as easy in JS.

Or generate the range with the es6 spread operator:

There's also a nasty (but shorter)

`eval`

hack to use in place of reduce. You can use`.join(+)`

to convert the array into a string containing each number joined by the '+' sign, then evaluate that string as if it's a JavaScript expression to get the sum:It's a bad practice to use eval, of course, but useful for JS code golf.

Or with lodash:

Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on dev.to. It literally takes a few seconds and

. β€οΈwe'd appreciate the support so muchPlus, no fake comments when you're signed in. π

Answer in C# using LINQ.

Explanation

`Enumerable.Range`

generates a number between 1 & 1000`Where`

filters records that matches a condition (It's like`filter`

in JS)`Sum`

is a convenience method for summing up a sequence (instead of doing`Aggregate((acc, n) => acc + n))`

, which is equivalent to`reduce`

in JS)Source & Tests on Github.

short AND legible!

Thanks Joe :)

At last count I did that exercise in 11 different languages, so maybe I'll post some of the less common ones.

Clojure:

Haskell (boring):

Haskell (a bit more interesting, but wasteful):

Potion:

GNU Smalltalk:

Here's one in Haskell using list comprehension:

Here it is Β―\_(γ)_/Β―

I'm looking forward for feedbacks

Smallest nitpicking ever: declare

`i`

inside the`for`

loop.It will prevent it from leaking outside the loop.

I like coding to follow the original problem definition. And then define these top-level functions in terms of smaller ones. E.g., Haskell:

Ruby:

Python

For fun: A shorter solution.

Great solution. Since I lost touch of python it was a little difficult. Now I understand.

Rubyβ¨πβ¨

Python implementation that runs in Ξ(1) time

In Java

## Python

or Python One-Liner

Scala:

Here it is in Go:

github.com/jvarness/euler/blob/mas...

I did that a while ago in C++

I started to solve problems but, well, you know little time so I couldn't continue :) I created a repository at github.

github.com/erhankilic/project-eule...