in reply to Re^4: Odometer pattern iterator (in C).
in thread Odometer pattern iterator (in C). (Updated.)
Ok, here is the index-based algorithm:
use strict; use warnings; sub first_idx { my( $N, $M ) = @_; return ( $N-$M .. $N-1 ); } sub next_idx { my( $N, $M, @idx ) = @_; return () if $idx[ -1 ] == $M-1; my $i = $M-1; $i-- while $i>0 and $idx[$i]-$idx[$i-1]==1; return ( @idx[0..$i-1], $idx[ $i ]-1, map { $idx[ $i ] + $N - $idx[ $M - $_+ $i ] } $i+1 .. $M-1); } my $N = 5; my $M = 3; my @indices = first_idx( $N, $M ); 1 while print( "@indices\n"), @indices = next_idx( $N, $M, @indices );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Odometer pattern iterator (in C).
by BrowserUk (Patriarch) on May 29, 2015 at 13:10 UTC | |
by hdb (Monsignor) on May 29, 2015 at 13:13 UTC | |
by BrowserUk (Patriarch) on May 29, 2015 at 14:34 UTC | |
by hdb (Monsignor) on May 29, 2015 at 15:12 UTC |