in reply to amount permutations
#!/usr/bin/perl use strict; use warnings; use Algorithm::Loops 'NestedLoops'; my ($val, $sum, $count, $key); my (%seen, %uniq); my (@matches, @key); my ($amount, $depth) = (28, 7); my $RS = [10, 5, 13, 3, 15, 1, 4]; NestedLoops( [ ( $RS ) x $depth ], { OnlyWhen => \&ok }, sub{} ); print_results(); sub ok { $count++; %uniq = (); $sum = 0; for $val ( @_ ) { $uniq{$val}++; return 0 if $uniq{$val} > 1; $sum += $val; return 0 if $sum > $amount; } $key = join " " , sort {$b <=> $a} @_; return 0 if exists $seen{$key}; $seen{$key} = undef; push @matches , $key if $sum == $amount; } sub print_results { my %convert; @convert{ @$RS } = 'A' .. 'H'; print "Searched $count combinations...\n"; for my $match ( @matches ) { for my $value ( split " " , $match ) { print "$convert{$value} "; } print "\n"; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: amount permutations
by gr0k (Novice) on Mar 04, 2004 at 19:55 UTC | |
by Limbic~Region (Chancellor) on Mar 04, 2004 at 20:01 UTC | |
Re: Re: amount permutations
by gr0k (Novice) on Mar 04, 2004 at 22:27 UTC | |
by Limbic~Region (Chancellor) on Mar 04, 2004 at 22:43 UTC | |
by tye (Sage) on Mar 05, 2004 at 03:37 UTC | |
by Limbic~Region (Chancellor) on Mar 05, 2004 at 13:23 UTC | |
by gr0k (Novice) on Mar 05, 2004 at 15:26 UTC | |
by Limbic~Region (Chancellor) on Mar 05, 2004 at 15:54 UTC | |
by the_0ne (Pilgrim) on Mar 05, 2004 at 16:36 UTC | |
|