My hat's of to you Sir! That is very, very cool code.
At take 3, I managed to get your benchmark (30/1..20) down to under 1 second (675 ms) and with less than 10 MB with a new version and Memoize, but... for 40/1..20 I was up to 11s/98MB whereas your's did it in under half a second and 3 MB, even with the addition of accumulating the results in an AoA rather than printing them direct.
My (pretty worthless) take 3 code
#! perl -slw use strict; use Memoize; use Benchmark::Timer; use List::Util qw[ sum ]; $|=1; BEGIN{ @ARGV = map{ eval } @ARGV } sub sums{ INIT{ memoize( 'sums' ); } my $total = shift; return unless @_ or $total > 1; return $_[ 0 ] == $total ? $_[ 0 ]: () if @_ == 1; my @list; for my $i ( 0 .. $#_ ) { my $next = $_[ $i ]; my $newTotal = $total - $next; for my $partial ( sums( $newTotal, grep $_ <= $newTotal, @_[ grep $_ != $i, 0 .. $# +_ ] ) ) { next unless $partial and sum( split ' ', $partial ) == $newTotal; push @list, "$next $partial"; } } return @list; } my( $total, @list ) = ( shift @ARGV, @ARGV ); my $T = new Benchmark::Timer; $T->start( 'memoized' ); my @sums3 = sums( $total, sort{ $b <=> $a } @ARGV ); $T->stop( 'memoized' ); $T->report; <STDIN>; __END__ P:\test>355455-3 30 1..20 1 trial of memoized (675.107ms total) P:\test>355455-3 40 1..20 1 trial of memoized (11.674s total)
The only problem I have (with the emphasis on I), is that even with the benefit of your commented code, I'm still not entirely sure that I understand how it works. I am certainly sure that I would not have been able to come up with the code (for this problem using Algorithm::Loops) myself.
(FWIW) I've long been impressed by (your examples of using) A::L, I just can't wrap my brain around how to use it for non-trivial tasks like this.
Off to re-read the documentation for the umpteenth time in the hope that something will click.
In reply to Re: Re: Better algorithm than brute-force stack for combinatorial problems? (Take 3 and homage to A::L)
by BrowserUk
in thread Better algorithm than brute-force stack for combinatorial problems?
by Solo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |