Write the shortest program that calculates the Frobenius number for a given set of positive numbers. The Frobenius number is the largest number that cannot be written as a sum of positive multiples of the numbers in the set.
Example: For the set of the Chicken McNuggetTM sizes [6,9,20] the Frobenius number is 43, as there is no solution for the equation a*6 + b*9 + c*20 = 43 (with a,b,c >= 0), and 43 is the largest value with this property.
It can be assumed that a Frobenius number exists for the given set. If this is not the case (e.g. for [2,4]) no particular behaviour is expected.
References:
[Edit] I decided to accept the GolfScript version. While the MATHEMATICA version might be considered "technically correct", it would clearly take the fun out of the competition. That said, I'm also impressed by the other solutions, especially Ruby (which was very short for a general purpose language).
Mathematica PROGRAM - 28 chars
Well, this is a REAL (unnecessary) program. As the other Mathematica entry shows clearly, you can compute the answer without writing a program ... but here it is
Invoke with
Mathematica 0 chars (or 19 chars counting the invoke command)
Invoke wtih
Example
Is it a record? :)
Perl 105
107110119122127152158charactersLatest edit: Compound assignment is good for you!
Explanation:
Set
$t
to the product of all of the input numbers. This is our upper limit.For each number from 1 to
$t
: If it's one of the input numbers, mark it using the%h
hash; otherwise, if there is a marked entry from further back (difference being anything in the input), mark this entry. All marked entries are non-candidates for Frobenius numbers.Extract all UNMARKED entries. These are Frobenius candidates...
...and the last of these, the highest, is our Frobenius number.
GolfScript 47/42 chars
Faster solution (47).
Slow solution (42). Checks all values up to the product of every number in the set...
Sample I/O:
Ruby
100 8680 chars(newline not needed) Invoke with
frob.rb 6 9 20
Works just like the Perl solution (except better:).
$*
is an array of command line strings;a
is the same array as ints, which is then used to collect all the numbers which can be made;eval(a*"*")
is the product, the max number to check.In Ruby 1.9, you can save one additional character in by replacing
"*"
with?*
.Edit: Shortened to 86 using
Symbol#to_proc
in$*.map
, inliningm
and shortening its calculation by folding the array.Edit 2: Replaced
.times
with.map
, traded.to_a
for;i
.FrobeniusScript 5 characters
Sadly there does not yet exist any compiler/interpreter for this language.
No params, the interpreter will handle that: