DEV Community

Peter Kim Frank
Peter Kim Frank

Posted on • Updated on

Project Euler #1 - Multiples of 3 and 5

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!

Latest comments (40)

Collapse
 
jurituuti profile image
JuriTuuti

Julia:
sum([n for n=1:999 if n%5==0 || n%3==0])

Collapse
 
derfugu profile image
Daniel Bruynooghe • Edited

q)sum (&) 0=min mod/:[; 3 5](!)1000

Collapse
 
derfugu profile image
Daniel Bruynooghe • Edited

k) +/&0=min{x-y*x div y}/:[;3 5](!)1000

For a change my k solution is more wordy because the mod function is rather long. Said that, the q above has k in it as well. In pure q it would read

q)sum where 0=min mod/:[;3 5]til 1000

terse and somewhat obfuscated, though the main elements are recognizable, i.e. create the list up to 1000, compute the remainders when dividing by 3 and by 5 and sum across all where either remainder is 0.

Collapse
 
derfugu profile image
Daniel Bruynooghe • Edited

q){sum x where 0=min x mod/:(3,5)}til 1000
k) {+/ &: 0=min x {x-y*x div y}/:(3,5)}(!)1000

or mix and match without lambdas is even shorter and more obfuscated
q)sum (&:) 0=min ((!)1000) mod/: (3,5)

Collapse
 
rahyhub profile image
rahy-hub • Edited

/*Multiples of 3 and 5

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. */

include

using namespace std;

int main()
{
int sum=0;
for(int i=1;i<1000;i++)
{
if(i%3==0 ||i%5==0)
{cout<<i<<" ";
sum+=i;}

}
cout<<"\n\n"<<"the sum of all the multiples of 3 or 5 below 1000 = "<<sum<<"\n";

return 0;
}

C++ >> Output >> 233168

Collapse
 
sawankrr profile image
sawankrr

Java

 Long sum = LongStream.range(1, 100)
                    .filter(i -> i % 3 == 0 || i % 5 == 0)
                    .sum();
    System.out.println(sum);
Collapse
 
bigzude profile image
Big-Zude

function multiple(){
var a=Math.trunc(999/3)
var b=Math.trunc(999/5)
var c=Math.trunc(999/15)

var multi3=((3*a)(a+1))/2
var multi5=((5*b)
(b+1))/2
var multi15=((15*c)*(c+1))/2

var sum=(multi3+multi5)-multi15
return sum
}
console.log(multiple())

Collapse
 
gabriela profile image
Gabi • Edited

Hi, I am adding my first thought on this in Java.

private int sum(int inputValue) {
int sum = 0;
for (int i = 1; i < inputValue; i++) {
if (i % 5 == 0 || i % 3 == 0) {
sum += i;
}
}
return sum;
}

Collapse
 
aspittel profile image
Ali Spittel
def sum_natural_numbers_below_n(n):
    # O(N) complexity
    mult_3 = range(3, n, 3)
    mult_5 = range(5, n, 5)
    all_multiples = set(mult_3 + mult_5)
    return sum(all_multiples)

print(sum_natural_numbers_below_n(1000))
Collapse
 
ronaldoperes profile image
Ronaldo Peres

Hi,
I am trying to solve this one, and already solved.

I am going to ask, what is the sum of all the multiples of 3 or 5 below 100000??

I am trying to do this but is giving me a timeout, when using loops.

Collapse
 
hanachin profile image
Seiei Miyagi

Ruby✨💎✨

require "set"

multiples_of_3 = Enumerator.new {|y| x = 0; y << x while x += 3 }
multiples_of_5 = Enumerator.new {|y| x = 0; y << x while x += 5 }

puts [multiples_of_3, multiples_of_5].each_with_object(Set.new) { |e, s|
  s.merge(e.take_while(&1000.method(:>)))
}.sum
Collapse
 
sait profile image
Sai gowtham • Edited
let sum=0;
let start=999;

 while(start){
    if(start % 3 === 0 || start % 5===0){
       sum +=start;
    }  
   start--;  
 }

console.log(sum);
Collapse
 
erhankilic profile image
Erhan Kılıç

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

Collapse
 
blackbird profile image
Omkar Ajnadkar • Edited

Python

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

or Python One-Liner

print(sum(i for i in range(1000) if i % 3 == 0 or i % 5 == 0))
Collapse
 
srleyva profile image
Stephen Leyva (He/Him)

Python implementation that runs in Θ(1) time

def solve(n):
    three = 3 * sum_all(math.floor(n/3))
    fives = 5 * sum_all(math.floor(n/5))
    sames = 15 * sum_all(math.floor(n/15))
    return three + fives - sames


def sum_all(n):
    return n*(n+1)/2

Some comments may only be visible to logged-in visitors. Sign in to view all comments.