if( X == Y ) { return depth; } elsif ( ( X,Y ) is in a 4x4 block on the major diagonal ) { return $depth - 1; } else { ## Move the 4x4 block to the origin and treat as for depth -1 X %= 4; Y %= 4; depth--; recurse; }