Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I find this claim slightly bizarre. I downloaded the PDF and read Wilf's description of the problem (he's looking for a number 1 bigger than the largest function described above). The following fairly obvious code solves the problem for arbitrary M in worst case time proportional to O(M*n*n) where n is the size of the smallest denomination. (If there are only 2 denominations then this will be O(M*n).)
#! /usr/bin/perl -w use strict; print largest(@ARGV)."\n"; sub largest { # Everything works mod $least my $least = min(@_); # We start off not knowing how to do anything. my @first = map {undef} 1..$least; # For technical reasons this needs to be 0 now, # and $least later. $first[0] = 0; for my $num (@_) { my @in_process = grep {defined($first[$_])} 0..($least - 1); while (@in_process) { my $to_process = shift @in_process; my $value = $first[$to_process] + $num; my $mod = $value % $least; if (not defined($first[$mod]) or $value < $first[$mod]) { $first[$mod] = $value; print " $value = $mod (mod $least)\n"; push @in_process, $mod; } } } $first[0] = $least; if (grep {not defined($_)} @first) { # We didn't reach one... return undef; } else { my $worst = max(@first); return $worst - $least; } } sub min { my $min = shift; for (@_) { $min = $_ if $_ < $min; } return $min; } sub max { my $max = shift; for (@_) { $max = $_ if $_ > $max; } return $max; }
I can only guess that he's measuring complexity in terms of the number of bits required to represent the numbers in the denomination. So he'd consider my solution as taking exponential time.

In reply to Re^2: Golf: Buying with exact change by tilly
in thread Golf: Buying with exact change by blokhead

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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2018-05-25 17:35 GMT
    Find Nodes?
      Voting Booth?