Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: problem while solving basic dynamic programming question

by tybalt89 (Parson)
on Mar 15, 2020 at 15:11 UTC ( #11114293=note: print w/replies, xml ) Need Help??


in reply to problem while solving basic dynamic programming question

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11114280 use warnings; use List::Util qw( uniq ); my @cache = []; for my $n ( 3 .. 4 ) { print "$_\n" for sort @{ find($n) }; print "\n"; } sub find { my $n = shift; $cache[$n] //= [ uniq $n, map { my $nn = $_; map { my $t = $_; map "$t+$_", @{ find($nn) } } @{ find($n - $nn +) } } 1 .. $n - 1 ]; }

Outputs:

1+1+1 1+2 2+1 3 1+1+1+1 1+1+2 1+2+1 1+3 2+1+1 2+2 3+1 4

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11114293]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-05-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (146 votes). Check out past polls.

    Notices?