9 Replies - 417 Views - Last Post: 17 February 2014 - 05:30 PM

#1 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Having Issues with a Loop (I think)

Posted 17 February 2014 - 02:54 PM

This is a pretty basic problem, but I'm having issues getting it to work. It's supposed to find the smallest number divisible by each number in the range of 1-20. The divisibleByAll function is working fine, so the issue has to be somewhere in the while loop. Can anyone tell me what's going on? Or is there a better way to do this? Thanks.

    var num = 1;
    var result;

    while (!result) {
      if ( divisibleByAll(num) ) {
        result = num;
      } else {
        num++;
      }
    };

    function divisibleByAll(number){
      if (number ==0) return false;
      for ( var i  = 1; i < 21; i++){
        if ( number % i != 0) return false;
      }
      return true;
    } 

    return result;




Is This A Good Question/Topic? 0
  • +

Replies To: Having Issues with a Loop (I think)

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,323
  • Joined: 15-January 14

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 03:07 PM

What happens when you run that? Do you know what number it is supposed to find? The only one I'm aware of is 2432902008176640000, which needless to say is going to take the browser quite a while to find. That's a lot of calculations. I'm not sure if there's a smaller number which is evenly divisible by 1 through 20 other than 20 factorial.

Never mind, my browser finally stopped at 232792560. That's still a lot of number crunching. I used the same code but removed the return result line and had it console.log result instead. That return statement is only valid if that code is inside a function, is it in a function?

This post has been edited by ArtificialSoldier: 17 February 2014 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,170
  • Joined: 08-June 10

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 04:07 PM

proof of your number:
Num.  Factorisation
 1
 2    2
 3    3
 4    2*2
 5    5
 6    2*3
 7    7
 8    2*2*2
 9    3*3
10    2*5
11    11
12    2*2*3
13    13
14    2*7
15    3*5
16    2*2*2*2
17    17
18    2*3*3
19    19
20    2*2*5
===========
=> 2*2*2*2*3*3*5*7*11*13*17*19
= 232,792,560


This post has been edited by Dormilich: 17 February 2014 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,323
  • Joined: 15-January 14

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 04:15 PM

Normally I'm pretty good at picking out a pattern, but why does the final equation have only four 2s, two 3s, one 5, and one 7? I took a lot of math in college but I spent a fair amount of time using alcohol to reformat my brain.
Was This Post Helpful? 0
  • +
  • -

#5 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 04:46 PM

View PostArtificialSoldier, on 17 February 2014 - 03:07 PM, said:

What happens when you run that? Do you know what number it is supposed to find? The only one I'm aware of is 2432902008176640000, which needless to say is going to take the browser quite a while to find. That's a lot of calculations. I'm not sure if there's a smaller number which is evenly divisible by 1 through 20 other than 20 factorial.

Never mind, my browser finally stopped at 232792560. That's still a lot of number crunching. I used the same code but removed the return result line and had it console.log result instead. That return statement is only valid if that code is inside a function, is it in a function?


I've tried just logging it as well. How long did it take for your browser to do the calculation? After I hit enter, the console doesn't return anything and moves the cursor to a new line.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,170
  • Joined: 08-June 10

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 05:03 PM

View PostArtificialSoldier, on 18 February 2014 - 12:15 AM, said:

Normally I'm pretty good at picking out a pattern, but why does the final equation have only four 2s, two 3s, one 5, and one 7? I took a lot of math in college but I spent a fair amount of time using alcohol to reformat my brain.

02 - 2*2*2*2*3*3*5*7*11*13*17*19
03 - 2*2*2*2*3*3*5*7*11*13*17*19
04 - 2*2*2*2*3*3*5*7*11*13*17*19
05 - 2*2*2*2*3*3*5*7*11*13*17*19
06 - 2*2*2*2*3*3*5*7*11*13*17*19
07 - 2*2*2*2*3*3*5*7*11*13*17*19
08 - 2*2*2*2*3*3*5*7*11*13*17*19
09 - 2*2*2*2*3*3*5*7*11*13*17*19
10 - 2*2*2*2*3*3*5*7*11*13*17*19
11 - 2*2*2*2*3*3*5*7*11*13*17*19
12 - 2*2*2*2*3*3*5*7*11*13*17*19
13 - 2*2*2*2*3*3*5*7*11*13*17*19
14 - 2*2*2*2*3*3*5*7*11*13*17*19
15 - 2*2*2*2*3*3*5*7*11*13*17*19
16 - 2*2*2*2*3*3*5*7*11*13*17*19
17 - 2*2*2*2*3*3*5*7*11*13*17*19
18 - 2*2*2*2*3*3*5*7*11*13*17*19
19 - 2*2*2*2*3*3*5*7*11*13*17*19
20 - 2*2*2*2*3*3*5*7*11*13*17*19
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,323
  • Joined: 15-January 14

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 05:13 PM

Quote

How long did it take for your browser to do the calculation?

Several minutes, probably less than 10 though.

Quote

After I hit enter, the console doesn't return anything and moves the cursor to a new line.

Note that you probably have to press the Run button or something similar, just pressing enter will go to the next line of code. I used Firebug and pasted into the section on the console where it allows you to type code with buttons for Run, Clear, Copy, and History. This is the code I used:

var num = 1;
var result;

while (!result) {
  if ( divisibleByAll(num) ) {
    result = num;
  } else {
    num++;
  }
};

console.log(result);

function divisibleByAll(number){
  if (number ==0) return false;
  for ( var i  = 1; i < 21; i++){
    if ( number % i != 0) return false;
  }
  return true;
} 


That is certainly a CPU-bound algorithm, so the time it takes is going to depend on how fast your CPU is. Make sure you're also using a modern browser, not something like Firefox 3.6.

I see Dormilich, that makes sense.
Was This Post Helpful? 0
  • +
  • -

#8 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 05:15 PM

Chrome finally spit out the answer. I don't know how much time I spent trying to fix this today when all I had to do was wait a few minutes. Lesson learned, I suppose.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,170
  • Joined: 08-June 10

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 05:26 PM

this is a good example, where you would use a WebWorker.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3359
  • View blog
  • Posts: 11,374
  • Joined: 12-December 12

Re: Having Issues with a Loop (I think)

Posted 17 February 2014 - 05:30 PM

View Posti_am_become_time, on 18 February 2014 - 12:15 AM, said:

Chrome finally spit out the answer. I don't know how much time I spent trying to fix this today when all I had to do was wait a few minutes. Lesson learned, I suppose.

Should have tested with numbers smaller than 20 first ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1