Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: problem while solving basic dynamic programming question

by tybalt89 (Monsignor)
on Mar 15, 2020 at 15:11 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114293]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found