The Hall of 20,000 Ceiling LightsCan you solve the puzzle? Without a script?There are 20,000 lights on. A person comes through and pulls the cord on every second light. A third person comes along and pulls the cord on every third light, etc. When someone comes who pulls every 20,000th chain, which lights are on?
My script looks like so:
And the answer is: only bulbs whose numbers are perfect squares are lit. 1,4,9,16...use strict; my $MAX = 20001; my @bulbs; $bulbs[$_] = 0 for ( 0..$MAX ); # use 1 and 0 for display purposes # Try goes thru the integers, Switching the bulbs. TRY: for my $try ( 1..$MAX ) { # switch by multiples; MULTIPLE: for my $i ( 1..$MAX ) { my $m = $i*$try; next TRY if ( $m > $MAX ); # a not works nicely here # if you don't plan to print your array $bulbs[$m] = $bulbs[$m] == 1 ? 0 : 1; } } print "\n\n"; for my $n ( 1..$MAX ) { printf "Bulb %5d is ON\n", $n if ( $bulbs[$n] ); }
-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday
In reply to CarTalk Puzzler by freddo411
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |