[Israel.pm] What does this code do?

Shlomi Fish shlomif at iglu.org.il
Sun May 2 04:43:28 PDT 2004

> > I may try an iterating subroutine. Give me some
> > time, if no one beats me to it.

Well, here's a code for an iterating recursion that also generates the same 
{0,1,2}**$N permutations. (but does not do the rest of the calculation):

#!/usr/bin/perl -w

use strict;

my $N = shift;

my @array = ((0) x $N);

MAIN_LOOP: while (1)
    print join("", @array), "\n";
    my $index = 0;
    while (($index < $N) && ($array[$index] == 2))
        $array[$index] = 0;
    if ($index == $N)
        last MAIN_LOOP;

What I did to get from one permutation to another is increment the number in 
base 3. I.e: turn all initial 2's to 0 and increment the next number.


	Shlomi Fish

Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://shlomif.il.eu.org/

Quidquid latine dictum sit, altum viditur.
        [Whatever is said in Latin sounds profound.]

More information about the Perl mailing list