The gf and I were listening to car talk this morning and they had an interesting puzzle - imagine a hallway as long as you can see, with light bulbs going down at regular intervals, the kind with a chain you pull that toggles it on or off. Now suppose that someone walks down to the end and turns on every light. Next, another person walks down and pulls the chain on every other light (light #2, 4, 6, 8, ....). The third person pulls every third light, the forth every forth light and so on. After lots of pulls, what pattern emerges?
The answer (spoiler ahead!) is that only numbers which have an odd number of divisors will remain on, and the only number with an odd number of divisors are squares! For example, 25 is divisible by 1, 5 and 25 (3 divisors), so it remains on, while 24 is divisible by 1, 2, 3, 4, 6, 8, 12 and 24 (8 divisors) so it is off.
See for yourself:
Here is the source code:
<style type="text/css">
#results td{
border: 1px solid white;
margin: 2px;
padding: 5px;
text-align: center;
}
.off{
background-color: black !important;
color: yellow !important;
}
.light{
background-color: yellow;
color: black;
}
</style>
<script type="text/javascript">
var limit = 200;
var tableRows = 20;
$(document).ready(function() {
// Build table and set all rows to zero
for(var row = 1; row < tableRows; row++)
{
$('#results').append("<tr id=\"row-" + row + "\"></tr>");
}
// Add row zero to bottom
$('#results').append("<tr id=\"row-0\"></tr>");
for(var x = 1; x <= limit; x++)
{
// Create columns
$('#row-' + x % tableRows).append("<td id=\"light-" + x + "\" class=\"light off\">" + x + "</td>");
}
});
function startAnimation()
{
$('#start-animation').attr("disabled", true);
pullEveryNthLight(1);
}
function pullEveryNthLight(n)
{
for(var i = n; i <= limit; i+=n)
{
var item = $('#light-' + i);
if( item.hasClass('off') )
{
item.removeClass('off');
}
else
{
item.addClass('off');
}
}
if(n < limit)
{
setTimeout(function(){
pullEveryNthLight(n+1);
}, 125);
}
}
</script>
<input id="start-animation" type="button" value="Click to start!" onclick="startAnimation()" />
<table id="results"></table>
Recent comments
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago
6 weeks 2 days ago