I was asked to provide the 73 occurrence of a sequence of numbers, with
the numbers 12345.  Each number can be used only once, and there are a
possible 120 combinations.

I was called by a client to figure this out for them, since one of their
2nd grade children was required to provide the answer to this question.  I
only had a coule of minutes so I pulled this code out of my sleeve to get
the answer.  But, I'm curious to find out if there is a sleeker way to get
the answer and full sequence (preferably more advanced than my 2nd grade
answer).

-John

--------------

#!/usr/bin/perl


my %seen;

my @number;
my $count = 1;

for ($a=1; $a<6; $a++) {
        $seen{$a} = 1;
        $number[0]=$a;

        for ($b=1; $b<6; $b++) {
                next if $seen{$b};
                $seen{$b} = 1;
                $number[1]=$b;

                for ($c=1; $c<6; $c++) {
                        next if $seen{$c};
                        $seen{$c} = 1;
                        $number[2]=$c;

                        for ($d=1; $d<6; $d++) {
                                next if $seen{$d};
                                $seen{$d} = 1;
                                $number[3]=$d;

                                for ($e=1; $e<6; $e++) {
                                        next if $seen{$e};
                                        $seen{$e} = 1;
                                        $number[4]=$e;

                                        print $count++,"\t";
                                        print @number[0,1,2,3,4],"\n";
                                        $seen{$e} = 0;
                                }

                                $seen{$d} = 0;
                        }

                        $seen{$c} = 0;
                }

                $seen{$b} = 0;
        }

        $seen{$a} = 0;
}

 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to