# Currency exchange [duplicate]

You are starting up a cryptocurrency exchange website which supports conversion from USD to two currencies, foo-coin and bar-coin. Write a program that takes the exchange rate for each coin to USD as arguments and outputs the maximum value of USD that cannot be completely divided into the two currencies (assuming these crypto-coins can only be purchased as full coins):

e.g. If the two exchange rates are $23 and$18, then 41 dollars could be split into 1 foo-coin and 1 bar-coin, but there is no way to completely split 42 dollars between the two coins. The output of the program for these two values is 373, as that is the largest dollar amount that CANNOT be divided into whole values of foo-coin and bar-coin.

Winner is the program with the fewest characters.

edit: You may assume the GCD of the two rates is 1

New contributor
Ryan Burrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

## marked as duplicate by HyperNeutrino code-golf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Nov 8 at 23:27

• Can you add more test cases? I am not sure I even understand the question. What does "cannot be completely divided into the two currencies" mean in this case? How is 373 "completely divided" into foo-coin and bar-coin? – Wisław Nov 8 at 19:48
• @wislaw does the updated description clarify things well enough? – Ryan Burrow Nov 8 at 19:55
• This is a simpler version of this challenge. – Arnauld Nov 8 at 19:57
• What's the expected output if $\gcd(x,y)\neq 1$? Or is it guaranteed that it won't happen? – Arnauld Nov 8 at 20:04
• The title of exchange rates is confusing. It made me think the challenge would be about converting an amount in one currency to another given the exchange rate. – xnor Nov 8 at 20:09

# 05AB1E, 3 bytes

<P<


Try it online!

This is of course only assuming that the greatest common denominator of the inputs (exchange rates) is 1, because otherwise this number is not well-defined.

Explanation:

The number we want to compute is called the Frobenius Number and can be calculated with the formula $$\\operatorname{Frob}(a,b) = (a-1)(b-1) - 1\$$.

<    | Subtract 1 from each input
P   | Take their product
<  | Subtract 1


# Swift, 42 bytes

func d(f:Int,b:Int)->Int{return f*b-(f+b)}


Try it online!

• Wouldn't f*b-f-b be 2 bytes shorter? – Arnauld Nov 8 at 22:37

# Pyth, 7 bytes

t*thQte


Try it online!

As @Wisław pointed out, this is simply the Frobenius Number. Also uses (a-1)(b-1)-1, as the alternate ab-a-b is a bit longer.

## How it works

 *thQ   - Multiply the first number minus one
te - By the second number (the Q is implicit) minus one
t       - Minus one

New contributor
frank is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.