in reply to Incremental indexing exponential

You can think of $line as a base 4 number.

$line = $digits[0] * 4**0 + $digits[1] * 4**1 + $digits[2] * 4**2

That can also be written as

$line = ($digits[2] * 4 + $digits[1]) * 4 + $digits[0]

You can use modulo and division

for my $num_digits (2..3) { for my $line (0 .. 4**$num_digits - 1) { my $remain = $line; my @digits; for (1..$depth) { push @digits, $remain % 4; $remain = int($remain / 4); } print(join(', ', @digits), "\n"); } }

Since 4 is a power of two, bit arithmetic can be used instead.

push @digits, $remain & 3; $remain >>= 2;

Replies are listed 'Best First'.
Re^2: Incremental indexing exponential
by RMGir (Prior) on Jun 21, 2007 at 16:19 UTC
    I think your solution would get the digits reversed from the homework question spec.

    Mike

      Keeping in line with the base4 methaphor, I placed the lower-precedence digits at the lower indexes in @digits. Feel free to change the order in which they are stored (by substituting unshift for push) or to change the order in which they are printed (by using reverse, by iterating over them in reverse order, or ...).