user2406223
user2406223

Reputation: 49

Project Euler #5 Javascript

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.

Upvotes: 1

Views: 6109

Answers (5)

Sameer Safi
Sameer Safi

Reputation: 1

// 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++;
  }
}

Upvotes: 0

Jongmin Kim
Jongmin Kim

Reputation: 21

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

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

Upvotes: 0

Sanchit
Sanchit

Reputation: 2260

Yeah firstly, pick a language that you are comfortable 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;
  }
}

Upvotes: -1

p.s.w.g
p.s.w.g

Reputation: 149020

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;
    }
}

Upvotes: 3

ruakh
ruakh

Reputation: 183391

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 recommend using a much smaller number for debugging, so you can figure them out without crashing your browser — but that should get you started.)

Upvotes: 1

Related Questions