Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This reduces the 8316 loops your code requires to 197 which is almost 2 orders of magnitude faster tahn your original and 3.5x faster than grinders example that needs 686 loops. Only 4:1 wastage on the test is pretty good as semi brute force goes and certainly beats the original 166:1. All it does different is to limit the looping to the limited subset of possible cases....

my ( $loops, $cnt ); printf "%4d %4d %4d %4d %4d\n", 100,50,20,10,5; for (my $e5=0; $e5<=100; $e5+=10 ) { for (my $e10=0; $e10<=100-$e5; $e10+=10 ) { for (my $e20=0; $e20<=100-$e10-$e5; $e20+=20 ) { for (my $e50=0; $e50<=100-$e20-$e10-$e5; $e50+=50 ) { for (my $e100=0; $e100<=100-$e50-$e20-$e10-$e5; $e100+=100 ) { $loops++; if ( $e5+$e10+$e20+$e50+$e100 == 100 ) { printf "%4d %4d %4d %4d %4d\n",$e100/100,$e50/50,$e20/20,$ +e10/10,$e5/5; $cnt++; } } } } } } print "\n$cnt possibilities in $loops loops\n"; __DATA__ 100 50 20 10 5 1 0 0 0 0 0 2 0 0 0 0 0 5 0 0 0 1 2 1 0 0 0 4 2 0 0 1 1 3 0 0 0 3 4 0 0 1 0 5 0 0 0 2 6 0 0 0 1 8 0 0 0 0 10 0 0 1 2 0 2 0 0 4 1 2 0 1 1 2 2 0 0 3 3 2 0 1 0 4 2 0 0 2 5 2 0 0 1 7 2 0 0 0 9 2 0 0 4 0 4 0 1 1 1 4 0 0 3 2 4 0 1 0 3 4 0 0 2 4 4 0 0 1 6 4 0 0 0 8 4 0 1 1 0 6 0 0 3 1 6 0 1 0 2 6 0 0 2 3 6 0 0 1 5 6 0 0 0 7 6 0 0 3 0 8 0 1 0 1 8 0 0 2 2 8 0 0 1 4 8 0 0 0 6 8 0 1 0 0 10 0 0 2 1 10 0 0 1 3 10 0 0 0 5 10 0 0 2 0 12 0 0 1 2 12 0 0 0 4 12 0 0 1 1 14 0 0 0 3 14 0 0 1 0 16 0 0 0 2 16 0 0 0 1 18 0 0 0 0 20 50 possibilities in 197 loops

cheers

tachyon


In reply to Re: How to generate restricted partitions of an integer by tachyon
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?
    [Cosmic37]: Permissions are interesting earthlings. Did nature determine who gives permission and who asks permission. Who was the first to get permission? Are you not related to them as one big earthling family?
    [karlgoethebier]: Cojones! We need cojones!
    [karlgoethebier]: Ouch! Permissions! We need permissions!
    [BarApp]: I can not use modules. I gain temporary access and still can not use modules.
    [Cosmic37]: ta erix - this szabo geezer is pretty cool methinks and he writes about undef but I cannot see instructions for redefining the record separator after having undefined it
    [Corion]: $/ = "wahtever";
    [Corion]: (it's a magic variable)
    [karlgoethebier]: BarApp: whoami

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (7)
    As of 2017-06-29 16:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many monitors do you use while coding?















      Results (672 votes). Check out past polls.