Find amount of heads and tails from percentages [duplicate]

Note: as this question has been marked as a duplicate, I have accepted an answer. However, I will still check this question if users can find a shorter way of doing it, and accept that answer.

Given two numbers (which are multiples of 0.5) representing heads-percentage and tails-percentage from a series of coin-flipping, output the minimum amounts of heads and tails to produce percentages such as those. Input and output can be in any form. However, output must be in a readable format (Good examples are x y, [x y], etc).

Examples:

• Input: 25 75 (25% heads, 75% tails) Output: 1 3 (an output of 25 75 is invalid since that is not the minimum amounts of heads and tails to produce those percentages)
• Input: 30 70 (30% heads, 70% tails) Output: 3 7
• Input: 99.5 0.5 (99.5% heads, 0.5% tails) Output: 199 1

You may assume that there will always be a digit before and after a decimal point in an input (no .5s or 15.s). You may also assume the input adds up to 100%. This is , shortest code in bytes wins!

• Can the numbers be taken as a list? As two number inputs? – xnor Oct 29 '15 at 4:23
• @xnor either is fine – GamrCorps Oct 29 '15 at 4:23
• Would [199.0, 1.0] a valid output for the last example? – Dennis Oct 29 '15 at 4:29
• This might effectively be the same as simplifying a fraction. My answer there is basically the same as here except for input processing. – xnor Oct 29 '15 at 5:02
• Also similar to this one. – grc Oct 29 '15 at 8:57

Dyalog APL, 3 bytes

,÷∨


This is a dyadic function train that accepts the percentages as left and right arguments:

┌─┼─┐
, ÷ ∨


It is equivalent to the following, train-less function:

{(⍺,⍵)÷⍺∨⍵}


Try it online on TryAPL.

How it works

  ∨  Compute the GCD of both arguments.
,    Concatenate both arguments.
÷   Divide the latter by the former.

• There is literally no way this can be beaten. Excellent job! :D – Alex A. Oct 29 '15 at 5:45
• I really should learn this language, it looks fascinating. – user4768 Oct 29 '15 at 7:50

Python 2, 47 bytes

A,B=a,b=input()
while b:a,b=b,a%b
print A/a,B/a


Takes input like 15,20 and prints the output like 3 4.

Performs the Euclidian algorithm to find the gcd, then scales the numbers by it.

Fortunately, Python will happily compute % on fractions, so inputs like 0.5 99.5 behave just the same. More type-strict languages (cough, Haskell) aren't so lucky.

• Also, floats are annoying in Haskell. 20/100*5 is not equal to 20/100 + 20/100 + 20/100 + 20/100 + 20/100, which is what is put in lists like [0,20/100..] – Leif Willerts Nov 3 '15 at 11:00

J, 4 bytes

,%+.


Explanation:

       (x +. y)     NB. GCD of the left and right inputs
%             NB. divided by
(x, y)              NB. a vector consisting of the inputs


Try it online

CJam, 25 14 bytes

{_~{_@\%}h;f/}


This is an unnamed function that pops a list from the stack and leaves one in return. Like @xnor's answer, it uses the Euclidean algorithm to find the GCD, then divides the list elements by it.

Try it online in the CJam interpreter.

How it works

_             Push a copy of the input array.
~            Dump its elements on the stack.
{    }h     Do:
_            Push a copy of the topmost integer.
@           Rotate the bottom-most integer on top of it.
\          Swap them.
%         Calculate the residue of their division.
While the residue is positive, repeat the loop.
;    Discard the last residue (0).
This leaves the GCD on the stack.
f/  Divide the elements of the input array by their GCD.


Pyth, 12 bytes

J/R.5Q/RiFJJ


Like @xnor's answer, this divides each list item by their GCD. Pyth has the GCD as a built-in, but it doesn't work for floats out of the box.

Try it online.

How it works

              (implicit) Save the evaluated input in Q.
/R.5Q        Divide each element of Q by 0.5.
This transforms the array's floats into integers.
J             Save the result in J.
iFJ   Compute the GCD of the elements of J.
/R   J  Divide the elements of J by their GCD.

• Since the numbers are multiples of 0.5, could you multiply the inputs by 2, and then use the integer GCD? – Reto Koradi Oct 29 '15 at 5:50
• That's actually what I'm doing. /R.5 divides by 0.5, casting to integer in the process. – Dennis Oct 29 '15 at 5:51
• Oh, ok, that makes sense. From the description I got the impression that you might not be using the built-in GCD, and went for the Euclid algorithm instead. – Reto Koradi Oct 29 '15 at 6:04

APL, 20 bytes

{⍺⍵÷⍺{0<⍵:⍵∇⍵|⍺⋄⍺}⍵}


This creates an unnamed dyadic function that accepts the inputs on the left and right and returns an array. Unlike Dennis' super slick Dyalog solution, this works with other versions of APL because it doesn't use trains and it doesn't assume that GCD (∨) works for floats.

Explanation:

    ⍺{0<⍵:⍵∇⍵|⍺⋄⍺}⍵}   ⍝ Compute the GCD of the inputs using the Euclidean algorithm
{⍺⍵÷                   ⍝ Divide the inputs by their GCD


Try all test cases online

Mathematica, 21 bytes

#/GCD@@Rationalize@#&


Another program with the same byte count is

#/GCD@@(Floor[2#]/2)&


Mma has never supported FP well...

TI-BASIC, 3122 24 bytes

Forgot to count new lines before...

Prompt A,B
gcd(2A,2B➡C
Disp 2A/C,2B/C


This was done in the middle of math class...

• You can skip the closing parenthesis in the third line. – Ypnypn Oct 29 '15 at 13:20
• And you can use Disp instead of Output to save about 8 bytes. – Ypnypn Oct 29 '15 at 13:21
• Also, chainging the top two lines to 'Prompt A,B' might help. – GamrCorps Oct 29 '15 at 13:22
• You can use the implicit Disp at the end if you output as a list: 2/C{A,B. – lirtosiast Oct 29 '15 at 17:34
• If you also take input as a list from Ans there's an 11 byte solution: 2Ans/min(gcd(2Ans,min(2Ans – lirtosiast Oct 29 '15 at 18:04

Common Lisp, 168 94

(let((x(floor(*(read)2)))(y(floor(*(read)2))))(format t "~A ~A"(/ x(gcd x y))(/ y(gcd y x))))


I missed the original bit about everything being a multiple of 0.5 (thanks xnor!), so this is a good bit shorter than my first go (Gotta' love that almost a third of those bytes are parentheses.)

• You're guaranteed the inputs are multiples of 0.5, so doubling suffices to make them whole. – xnor Oct 29 '15 at 8:00
• Oh wow, I didn't even notice that. I'll shorten this answer right quick, thanks. – Candles Oct 29 '15 at 8:01