Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Not a golfing solution, but one that doesn't assume an upper bound (although it will loop forever if there's no finite answer):
use strict; use warnings; use Test::More 'no_plan'; $| = 1; my %cache; sub exact { my ($amount, @bills) = @_; return $cache{$amount} if defined $cache{$amount}; return 1 if !$amount; return 0 if $amount < 0; foreach my $bill (@bills) { return $cache{$amount} = 1 if exact($amount - $bill, @bills) } return $cache{$amount} = 0; } sub largest { %cache = (); my @bills = @_; my $smallest = $bills [0]; return -1 if $smallest == 1; my $start = 0; my $prev = 0; for (my $t = 1; 1; $t++) { if (exact($t, @bills)) { if ($prev) { if ($t - $start + 1 == $smallest) {return $start - 1} } else {$start = $t} $prev = 1; } else { $prev = 0; } } } is (largest(6,9,20), 43); is (largest(3,5), 7); is (largest(6,11,20), 27); is (largest(17,18,19,20), 101); is (largest(2,3,5,10), 1); is (largest(1000,1001,1002,1003,1004,1005), 199999); __END__ ok 1 ok 2 ok 3 ok 4 ok 5 ok 6 1..6

In reply to Re: Golf: Buying with exact change by Anonymous Monk
in thread Golf: Buying with exact change by blokhead

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 taking refuge in the Monastery: (6)
    As of 2014-08-23 16:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (174 votes), past polls