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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This one takes the desired total and/or the list of denominations on the command line and shows all possible combinations:

#!/usr/bin/perl -w use strict; use Algorithm::Loops qw( NestedLoops MapCarMin ); my @sum= shift || 100; my @size= ( 100, 50, 20, 10, 5 ); @size= @ARGV if @ARGV; my $tries= 0; my $iter= NestedLoops( [ ( sub { my $n= $sum[@_]/$size[@_]; ! $n ? [] : @_ < $#size ? [ 0 .. $n ] : [ $n .. int $n ]; } ) x @size ], { OnlyWhen => sub { $tries++; not $sum[@_]= $sum[$#_] - $_[-1]*$size[$#_]; }, }, ); my @cnt; my $seq= 0; while( @cnt= $iter->() ) { printf "%d) %s\n", ++$seq, join ' + ', MapCarMin { !$_[0] ? () : join '*', @_ } \@cnt, \@size; } print "($tries tries)\n";

And here's a sample run:

> perl change.pl 103 100 33 10 3 | more 1) 1*10 + 31*3 2) 4*10 + 21*3 3) 7*10 + 11*3 4) 10*10 + 1*3 5) 1*33 + 1*10 + 20*3 6) 1*33 + 4*10 + 10*3 7) 1*33 + 7*10 8) 2*33 + 1*10 + 9*3 9) 1*100 + 1*3 (56 tries) >

- tye        


In reply to Re: How to generate restricted partitions of an integer by tye
in thread How to generate restricted partitions of an integer by borisz

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found