$| = 1; sub explore { my $n = shift; my $depth = shift; my $min0 = shift; my $max0 = shift; my $min1; my $max1; my $min; my $max; if (($depth % 4)<2) { $min1 = $max0 + 1; $max1 = $min1 + 2**$depth - 1; $min = $min0; $max = $max1; } else { $max1 = $min0 - 1; $min1 = $max1 - 2**$depth + 1; $min = $min1; $max = $max0; } # print "* n=$n d=$depth 0=$min0..$max0 1=$min1..$max1 $min..$max\n"; my ($val, $rem); if ($n < $min || $n > $max) { ($val, $rem) = explore($n, $depth+1, $min, $max); } else { ($val, $rem) = (0, $n); } if ($rem >= $min1 && $rem <= $max1) { $rem -= $min1 - $min0; if ($depth % 2) { $val += 12 * 4**($depth-1); } else { $val += 4**$depth; } } return ($val, $rem); } sub a { my $n = shift; my ($val, $rem) = explore($n, 0, 0, 0); die if $rem; return $val; } foreach my $n (0..10_000) { print $n, " ", a($n), "\n"; }