0
\$\begingroup\$

This question already has an answer here:

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.
\$\endgroup\$

marked as duplicate by HyperNeutrino code-golf Nov 8 at 23:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – Wisław Nov 8 at 19:48
  • \$\begingroup\$ @wislaw does the updated description clarify things well enough? \$\endgroup\$ – Ryan Burrow Nov 8 at 19:55
  • 3
    \$\begingroup\$ This is a simpler version of this challenge. \$\endgroup\$ – Arnauld Nov 8 at 19:57
  • 1
    \$\begingroup\$ What's the expected output if \$\gcd(x,y)\neq 1\$? Or is it guaranteed that it won't happen? \$\endgroup\$ – Arnauld Nov 8 at 20:04
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Nov 8 at 20:09
4
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Swift, 42 bytes

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

Try it online!

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

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.
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.