#! /usr/bin/perl -w use strict; my @knight=(); my $arraysize=3000000; my $total=$arraysize; my $justkilled=0; my $sword=1; my $tick=0; while ($total>1) { # --- first the knight kills the next knight in order my $next = nextknight($sword); $knight[$next]=1; $justkilled=$next; $total--; # --- now we hand the sword over to the next knight $sword = nextknight($next); }; print "There is $total knight. $justkilled was just killed. $sword has the sword\n"; sub nextknight { my $t = shift; $t++; $t++ while $knight[$t]; if ($t>$arraysize) { # we've wrapped around the table; $t = 1; $t++ while $knight[$t]; }; return $t; };