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] ); }