Skip to content

Instantly share code, notes, and snippets.

@nefarioustim
Created June 5, 2011 12:37
Show Gist options
  • Select an option

  • Save nefarioustim/1008926 to your computer and use it in GitHub Desktop.

Select an option

Save nefarioustim/1008926 to your computer and use it in GitHub Desktop.
Find the sum of all the multiples of 3 or 5 below 1000.
// Do the maths
for(
var sum = 0, i = 1;
i < 1000;
!(i % 3 && i % 5) && (sum += i), i++
);
// Log the result
console.log(sum);
// Prep the array
for(
var arr = [], i = 1;
i < 1000;
!(i % 3 && i % 5) && arr.push(i), i++
);
// Sum the array
console.log(
arr.reduce(
function(prev, current) {
return prev + current
}
)
);
print sum([i for i in range(1000) if not (i % 3 and i % 5)])
puts (1...1000).select{ |n| n % 3 == 0 || n % 5 == 0 }.reduce(:+)
@mikewest

mikewest commented Jun 5, 2011

Copy link
Copy Markdown

I think you've made the JS implementation more complicated than it needs to be. How about;

var sum = 0;
for (var i = 0; i < 1000; i++) {
  if (i % 3 === 0 || i % 5 === 0) {
    sum += i;
  }
}
console.log('Sum: %d', sum);

@mikewest

mikewest commented Jun 5, 2011

Copy link
Copy Markdown

Or was the point to play with reduce? :)

@nefarioustim

Copy link
Copy Markdown
Author

Actually, I hadn't thought of it that way because I'd come from doing it in Ruby and Python. :)

@czottmann

Copy link
Copy Markdown

Euler Project!

@nefarioustim

Copy link
Copy Markdown
Author

Note the filenames, Carlo. :D

@czottmann

Copy link
Copy Markdown

I know, I got overly enthusiastic about it is all.

@mikewest

mikewest commented Jun 5, 2011

Copy link
Copy Markdown

Since you're already making your for loop unreadable, you might as well go all-out: stick the postincrement inside the assignment. :)

sum += i++

@mikewest

mikewest commented Jun 5, 2011

Copy link
Copy Markdown

And what is this Euler Project? Am I doing your homework for you? Have I missed out on opportunities to win huge prizes and fame?

@nefarioustim

Copy link
Copy Markdown
Author

Congratulations, you've given birth to a healthy infinite loop.

And it's not unreadable, it's elegant and lovely.

@dhwthompson

Copy link
Copy Markdown

Unless I’m misreading it, your Python version includes 1000 in the sum while the other versions don’t.

Also, for added goodness, you can omit the square brackets in the list comprehension and it’ll do it as a generator, saving you the massive overhead of storying a 200-item array ;-)

@dhwthompson

Copy link
Copy Markdown

Oh, and my candidate for “Most pedantic code review point ever”:

You don’t need to use range(1, 1000): you can just as happily use range(1000), because no-one cares if you add zero to your total.

@nefarioustim

Copy link
Copy Markdown
Author

Both highly valid points, Dave. I shall update.

@czottmann

Copy link
Copy Markdown

Comments welcome: euler-problem-1.sh

@spjwebster

Copy link
Copy Markdown

This is as short as I can make it whilst keeping it legible:

var sum=0, i=2;
while(i++,i<1000) { sum += (i % 3 && i % 5) ? 0 : i; } 
console.log(sum);

@nefarioustim

Copy link
Copy Markdown
Author

Starting from 2 to save some cycles. Interesting.

@davepkennedy

Copy link
Copy Markdown

Do I need to explain my working?

def sum_under (val, step):
    pairs = val / step
    return (pairs*((pairs*step)+step)/2)

sum_under (999,3) + sum_under(999,5) - sum_under(999,15)

@nefarioustim

Copy link
Copy Markdown
Author

Nah, that's the algebraic solution to the problem. I remember doing that at school. :D

@nefarioustim

Copy link
Copy Markdown
Author

Actually Dave, can you do it again using only 1000, 3, and 5 as parameters?

Your maths is wrong 'cos whilst 999 is valid for 3, it should be 995 for 5 and… uh… 990 for 15, I believe.

@nefarioustim

Copy link
Copy Markdown
Author

For those that are interested, problem 2 solved with JS here: https://gist.github.com/1009018

@davepkennedy

Copy link
Copy Markdown

(999 / 15) * 15 = 990
(999 / 15.0) * 15 = 998.9999
Integer truncation is my friend

@kayslay

kayslay commented Sep 20, 2017

Copy link
Copy Markdown

Here is one that solves the sum of multiples of numbers in an array less than a given limit.
Find the codein JS HERE

//test: find the sum of the multiples of 3 and 5 less than 1000
console.log(sumOfMultplesOfNumbersLessThan([3,5], 1000));

@dwikipedia

Copy link
Copy Markdown

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

newbie rules!

@jpvajda

jpvajda commented Sep 14, 2018

Copy link
Copy Markdown

Super useful. thanks for posting this with the different code options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment