Skip to content

Instantly share code, notes, and snippets.

@atushi
Created July 12, 2013 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
@atushi
Copy link
Author

atushi commented Jul 12, 2013

var PRJ1 = function (checkNum, i) {
if ((i%checkNum)==0 && i>0) {
    return i;
 } else {
    return 0
  }
};

var TARGETNUM = 1000;
var CHECKNUM = [3,5];
var i = 1;
var answer = 0;
while (true) {
  flag = false;
  if (TARGETNUM <= i) break;
  for (var x=0; x<array.length; x++) {
    tmp = PRJ1(CHECKNUM[x], i);
    if (!flag && tmp>0) { answer = answer + tmp; 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