Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #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":



  • 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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2020-10-21 08:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (212 votes). Check out past polls.

      Notices?