FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

math problem with javascript

 


alalex
I am trying to solve a math problem with javascript. It shouldn't be too difficult, but something is not working...
Here is the problem:
There are 1000 light bulbs in a row, each one with a switch in front of it. Everytime you pull a switch, its light bulb goes off if it was on, or on if it was off. So, beggining with all the bulbs off, you pull them all, so they are on. Then you pull all the bulbs starting by two, 2 by 2 (so 2,4,6,8...), then the 3's, 4's, 5's up till 1000. In the end, how many bulbs are on, and what numbers?
And I have this code:
Code:

<script language="javascript" type="text/javascript">
<!-- //
//Create 1000 images
{
if(document.images)
   new Image().src = "bulb.gif";
   new Image().src = "bulb_on.gif";
      for(i=1; i<=1000; i++)
      {
      //Display 1000 images
      var content = "<img name=" + i + "\" src=\"bulb.gif\" height=\"10\" width=\"10\">";
      document.write(content);
      }
   //here goes the r looping
      for(r=1; r<=1000; r++){
      //here I do all the multiples of r numbers
         for(n=0; n<=1000/r; n+r=n){
         //change status of bulb
         var location_image = document.n.src;
            if(location_image == "images/bulb.gif"){
            location_image = "bulb_on.gif";
            }else{
            location_image = "bulb.gif";
            }
         }
      }
   }
}

window.status = "Light bulbs problem...";
// -->
</script>

but it doesn't work!!
color coded in this url: pastebin
Check it in this link:
click here

These are the images:
images/bulb.gif ->
images/bulb_off.gif - >


Last edited by alalex on Mon Jun 05, 2006 2:46 am; edited 1 time in total
alalex
new try, now with check boxes...
Code:
<script language="javascript" type="text/javascript">
new Image().src = "images/bulb.gif";
new Image().src = "images/bulb_on";
for(i=0; i<=1000; i++){
//display table with number and bulb
document.write( "<table width=\"20\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"16\">" + i + "</td></tr><tr><td><input type=\"checkbox\" name=\"checkbox\" value=\"checkbox\"></td></tr></table>");
}

</script>

This works by the moment, but how can I check or uncheck them automatically??
see here:
here
alalex
New code, now at least works pretty well...
Still have a problem, the images change from images/bulb.gif to images/bulb_on.gif , but they don't change back...
Here is the web: Second try...
here is the new code:
Code:
<table border="1" cellspacing="0" cellpadding="0">
<script language="javascript" type="text/javascript">
<!--
//load images, it will finish faster
new Image().src = "images/bulb.gif";
new Image().src = "images/bulb_on.gif";
for(t=1; t<=1000; t++){
//display table with number and bulb
document.write( "<form name=\"bulbs\"><table width=\"20\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"16\">" + t + "</td></tr><tr><td><img name=\"" + t + "\" src=\"images/bulb.gif\"></td></tr></table></form>");
}
alert("Table done, click to continue");
// -->
<!--
var n = 1;
//create numbers from 1 to 1000,
//then create numbers to multiply by.

alert("To start process, scroll down and click on the button.");
window.status = 'Please click on the button at the bottom of the page.';

function switch_on_off(){
   var con = confirm("Are you sure you want to start this, it may slow your computer down several minutes!");
   if(con == true)
   {
      alert("Remember, once you click here, your computer will slow down several minutes!");
      for(i=1; i<=1000; i++)
      {
         while(n<=1000)
         {
            var multiples = i * n - 1;
            var location = "document."+multiples+".src";
            if(multiples > 1000){
               break;
            }else{
               if(location='images/bulb.gif')
               {
                  location='images/bulb_on.gif';
               }else{
                  location='images/bulb.gif';
               }
               window.status = 'Working on bulb number:' + multiples + '. Please wait...';
               n++;
            }
         }
      }
      alert("Mission finished!");
      window.status = 'Done, to restart, first refresh!';
   }else{
   alert("Mission aborted!");
   }
}
// -->
</script>
</table>
<input type="submit" name="Submit" value="Start switching on/off" onClick="switch_on_off()">
<table border="1" cellspacing="0" cellpadding="0">
</table>
alalex
I will donate 100 $frihs if someone tells me the answer and it works!! Very Happy
(If I get it first I will not donate any $frih...)

Thanks!!! Very Happy
Kaneda
Here's one that should work - and without slowing the computer down (much). It needs to be in the same directory as bulb_on.gif and bulb_off.gif, or you should change the URLs assigned to b.src. Change DELAY if you want the updates to be faster or slower. At 200, the "mission" should be done after 0.2 * 1000 = 200 seconds.

Code:
<html>
<head>
  <title>Bulbs</title>
  <script language="javascript" type="text/javascript">
var counterEle;
var current = 1;
var bulbs_on = 0;
var DELAY = 200;

function run() {
  var bulbsele = document.getElementById("bulbs");
  var outstr = "";
  for(t=1; t<=1000; t++)
  {
    outstr += '<img id="bulb' + t + '" src="bulb_off.gif" /> ';
    if (t % 50 == 0) outstr += '<br />';
  }
  bulbsele.innerHTML = outstr;
  counterEle = document.getElementById("counter");
}

function doNext() {
  for(i=current; i<=1000; i+= current)
  {
    var b = document.getElementById("bulb" + i);
    b.on = !b.on;
    if(b.on)
    {
      b.src = 'bulb_on.gif';
      bulbs_on++;
    }
    else
    {
      b.src = 'bulb_off.gif';
      bulbs_on--;
    }
  }
  counterEle.innerHTML = current + " iterations, " + bulbs_on + " bulbs       are on.";
  current += 1;
  if (current > 1000)
  {
    alert("Mission finished!");
  }
  else
  {
    setTimeout(doNext, DELAY);
  }
}
window.onload = run;
</script>
</head>
<body>
   <div id="bulbs"></div>
   <button onclick="doNext()">Start</button>
   <div id="counter"></div>
</body>
</html>


Doing such code isn't the "hard" part of the math problem - the "hard" part is to show why the answer is the square root of 1000 (rounded down to nearest integer) Wink
alalex
THANKS!!!
My code was way more complicated... wow.. thanks!!!! Very Happy Very Happy

I also have to try to prove it mathematically, but I think I will be able to work that out...
I will probably ask you some parts of the code I don't understand yet... but thanks

Thanks!!!! Very Happy
alalex
how could I display in the end the number of bulbs that are on, in an alert message? Should I use a for statement to loop over the images, and write down the number of every bulb that is on?? If so, how?

sorry, is my first week with javascript...
thanks! Very Happy
Kaneda
That is easy to figure out without javascript, if you figure out the proof Wink. Because the bulbs that are lit at the end will be all the square numbers lower than 1000:

1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
6^2 = 36
7^2 = 49
etc., up to 31.

31^2 = 961

If you want the javascript to tell you, there's also already a userdefined property on each image taking care of this: b.on.

So, just add a for loop at the end, after (or replacing) the alert "Mission finished!":

Code:
var result = "";
for (var i = 1; i <= 1000; i++) {
  var b = document.getElementById("bulb" + i);
  if (b.on) {
    if (result != "") { // Only add comma if this is not the first bulb that's on.
      result += ", ";
    }
    result += i;
  }
}
alert("Bulbs on: " + result);


(Untested code)
alalex
thanks, i haven't tried it yet, but I believe it will work!! Very Happy
Kaneda
alalex wrote:
I will donate 100 $frihs if someone tells me the answer and it works!!


Time to pay up? Wink Not that I use them for anything, but getting help with what looks like school work should never be free Wink
alalex
oops... Sorry, I forgot that, sure, I will make you the donation! Very Happy
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.