Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Divisible by 7 or 5. Not only 7.


You are thinking of the challenge on the front page. He's talking about the first of the three "guest challenges" you get if you click the link on the front page to see more challenges.

The front page challenges asked for the sum of the integers from 1 to 1000, excluding those divisible by 5 or 7. Here was my sort of smart-ass answer:

   function jsChallenge() { 
     var num = 1000;
     var t = num;
     var sum = t*(t+1)/2;
     t = Math.floor(num/5);
     sum -= 5*t*(t+1)/2;
     t = Math.floor(num/7);
     sum -= 7*t*(t+1)/2;
     t = Math.floor(num/35);
     sum += 35*t*(t+1)/2;
     return sum;
}

The first guest challenge asks for the sum of the integers from 1 to 1000, excluding those that contain the digit 7.


If you're going to be a smart-ass, then go all out.

  function jsChallenge() {
    function sum(n) {
      var k = Math.floor(1000 / n);
      return n * k * (k + 1) / 2;
    };
    return sum(1) - sum(5) - sum(7) + sum(35);
  }




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: