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:
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
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
