The Coin Problem
Perl, 60 54 51 bytes
50 bytes code + 1 bytes command line
$.*=$_,$r.=1x$_."|"}{$_=$.while(1x$.--)=~/^($r)+$/
The basic approach is to let the regex engine do the hard work with string matching. For example, it will construct a regex similar to ^(.{3})*(.{7})*(.{8})*$
and match against a string of length n
where n
descends from the product of the inputs until it fails to match.
Note that this will become exponentially slow as the number of arguments increases.
Usage: Arguments are read in from STDIN (new line separated), for example:
printf "101\n10" | perl -p entry.pl
Pyth, 23 bytes
ef!fqTs.b*NYQY^UTlQS*FQ
It's very slow, as it checks all values up to the product of all the coins. Here's a version that is almost identical, but 1) reduces the set of coins to those not divisible by each other and 2) only checks values up to (max(coins) - 1) * (min(coins) - 1)
(47 bytes):
=Qu?.A<LiHdG+GHGQYef!fqTs.b*NYQY^UTlQS*thSQteSQ
Explanation
S range 1 to
*FQ product of input
f filter by
UT range 0 to T
^ lQ cartesian power by number of coins
f filter by
s.b*NYQY sum of coin values * amounts
qT equals desired number T
! nothing matching that filter
e take last (highest) element
R, 84 78 72 bytes
For an arbitrary entry of coprimes, \$a_1, a_2,\ldots\$, Frobenius' amount is given by
a=scan();max((1:(b<-min(a)*max(a)))[-colSums(combn(outer(a,0:b),sum(!!a)))])
Try it online!
since it is always smaller than the product of the extreme \$a_i\$'s. It is then a matter of combining all possible combinations (and more) within that range. Thanks to Cole Beck for suggesting outer
with no "*" and colSums
rather than `apply(...,2,sum)'.
A faster but longer (by two bytes) version only considers max(a)
:
a=scan();max((1:(min(a)*(b<-max(a))))[-colSums(combn(outer(a,0:b),sum(!!a)))])
A slightly shorter version (72 bytes) that most often takes too log or too much space to run on Try it online is
a=scan();max((1:(b<-prod(a)))[-colSums(combn(outer(a,0:b),sum(!!a)))])