Skip to content

Instantly share code, notes, and snippets.

@atushi
Created July 12, 2013 09:23
Show Gist options
  • Save atushi/5983087 to your computer and use it in GitHub Desktop.
Save atushi/5983087 to your computer and use it in GitHub Desktop.
Project Euler . Problem 1 . Multiples of 3 and 5 : 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.
var TARGETNUM = 1000;
var i = 1;
var answer = 0;
while (true) {
flag = false;
if (TARGETNUM <= i) break;
if ((i%3==0) && (!flag)) { answer = answer + i; flag = true; }
if ((i%5==0) && (!flag)) { answer = answer + i; flag = true; }
i++;
}
console.log(answer);
@Lemoneey
Copy link

var sum = 0;
for (var i = 3; i < 1000; i++){
if ((i % 3) == 0) {
sum = sum + i;
} else if ((i % 5) == 0) {
sum = sum + i;
}
}
alert(sum);

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