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.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.