XP is just a number PerlMonks

### Re: Golf: Buying with exact change

by blazar (Canon)
 on Feb 23, 2005 at 09:30 UTC ( #433620=note: print w/replies, xml ) Need Help??

I've not followed this thread in detail so I don't know if it's already been mentioned, but this is indeed a well known mathematical (combinatorial) problem, and a "hard" one, actually called "the money changing problem".

The problem is discussed in some detail e.g. in the beautiful book generatingfunctionology by Prof. Wilf, available for download from http://www.math.upenn.edu/%7Ewilf/DownldGF.html

To quote from Wilf:

There are no general `formulas' for the conductor if M >= 3, and no good algorithms for calculating it if M >= 4.
(M is the number of "changes" and the "conductor" is the smallest quantity N such that all n>=N can be represented as sums of the changes. In this case the latter must be -of course- coprime.)

Replies are listed 'Best First'.
Re^2: Golf: Buying with exact change
by tilly (Archbishop) on Feb 25, 2005 at 05:25 UTC
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.
Re^2: Golf: Buying with exact change
by Anonymous Monk on Feb 24, 2005 at 08:11 UTC
• For those of us "math challanged" you denominations must not have any common factors. i.e. all even, all multiples of some N. Otherwise there is an infinite number of prices which you can't buy:-(

• In a more obscure vein. A closed solution for arbitrary M has been shown to be NP-hard.

starbolin

Create A New User
Node Status?
node history
Node Type: note [id://433620]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-03-19 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (246 votes). Check out past polls.

Notices?