language-agnosticcode-golfrosetta-stone

Code Golf: Frobenius Number


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).


Solution

  • GolfScript 47/42 chars

    Faster solution (47).

    ~:+{0+{.1<{$}{1=}if|}/.!1):1\{:X}*+0=-X<}do];X(
    

    Slow solution (42). Checks all values up to the product of every number in the set...

    ~:+{*}*{0+{.1<{$}{1=}if|}/1):1;}*]-1%.0?>,
    

    Sample I/O:

    $ echo "[6 9 20]"|golfscript frobenius.gs
    43
    $ echo "[60 90 2011]"|golfscript frobenius.gs
    58349