Project Euler #5 Javascript - Stack Overflow

This is the problem:2520 is the smallest number that can be divided by each of the numbers from 1 to 10

This is the problem:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Here is my code:

var calculation = function(){
        var result = 0;
        for(var i = 20; i == 10000000000000; i++){
                for(var e = 2; e == 20; e++){
                        if(i % e == 0){
                                result = i;
                        }
                }
        }
        alert(result);      
}
calculation();

The problem is that the program just outputs 0.

This is the problem:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Here is my code:

var calculation = function(){
        var result = 0;
        for(var i = 20; i == 10000000000000; i++){
                for(var e = 2; e == 20; e++){
                        if(i % e == 0){
                                result = i;
                        }
                }
        }
        alert(result);      
}
calculation();

The problem is that the program just outputs 0.

Share Improve this question edited Jan 22, 2015 at 11:20 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Jan 17, 2014 at 4:15 user2406223user2406223 492 silver badges7 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

There are more graceful ways to solve this problem, but to address your current issue, the 'condition' portion of the for-loop needs to return true when you want to continue the loop, not when you want to stop it. So your for loops should look like this:

for(var i = 20; i <= 10000000000000; i++){
    for(var e = 2; e <= 20; e++){
        ...
    }
}

Inside the loops, you've got another issue. You're setting result if i % e == 0 but you've never defined a stop-case. In other words result will be the last number you which passed that test, even if it failed all the other tests for i and e. You'd have to do something like this:

for(var i = 20; i <= 10000000000000; i++){
    bool found = true;
    for(var e = 2; e <= 20; e++){
        if (i % e != 0) {
            found = false;
            break; // stop testing other divisors
        }
    }
    if (found) {
        return i;
    }
}

In this line:

        for(var i = 20; i == 10000000000000; i++){

the i == 10000000000000 bit means that the loop should only run as long as i is equal to 10000000000000 — which it never is, since it's already not that to begin with.

I imagine you meant to write i <= 10000000000000.

(There are other problems with your code as well — I remend using a much smaller number for debugging, so you can figure them out without crashing your browser — but that should get you started.)

In JavaScript, Number.MAX_SAFE_INTEGER should be the largest possible number.

https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

// Kindly check this below JS program.

var count=0,num=1;

while(count<20){
    for(var i=1;i<=20;i++){
        if(num % i==0){
            count++;
        }
        else{
            count=0;
            break;
        }
    }
  if(count==20){
    console.log(num);
    break;
  }
else
  {
    num++;
  }
}

Yeah firstly, pick a language that you are fortable in for solving these problems. I'm pretty sure that in every single language out there a for loop condition == doesnt mean less than or equal to so perhaps study up on fundamentals of any programming language first.

Secondly assuming that you put in the <= in your for loop, your inner for loop logic is flawed. If you are new, then the most logical way to do this would be to write something to the effect of this:

if (i%2==0 && i%3==0 && i%4 == 0 && ... && i%20==0)
  alert(i);`

One way to achieve this is to use the help of a boolean and you can do something like this:

for (var i=20; i<=1000000000; i++) { //Your upper bound is wayy too high.
  var done = true;
  for (var e = 2; done && e <= 20; e++) { //Be more efficient.
    done = done && (i % e == 0);
  }
  if (done) {
    alert(i);
    return;
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637509a4637482.html

相关推荐

  • Project Euler #5 Javascript - Stack Overflow

    This is the problem:2520 is the smallest number that can be divided by each of the numbers from 1 to 10

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信