Calculate the relativistic velocity
Mathematica, 17 bytes
+##/(1+##/9*^16)&
An unnamed function taking two integers and returning an exact fraction.
Explanation
This uses two nice tricks with the argument sequence ##
, which allows me to avoid referencing the individual arguments u
and v
separately. ##
expands to a sequence of all arguments, which is sort of an "unwrapped list". Here is a simple example:
{x, ##, y}&[u, v]
gives
{x, u, v, y}
The same works inside arbitrary functions (since {...}
is just shorthand for List[...]
):
f[x, ##, y]&[u, v]
gives
f[x, u, v, y]
Now we can also hand ##
to operators which will first treat them as a single operand as far as the operator is concerned. Then the operator will be expanded to its full form f[...]
, and only then is the sequence expanded. In this case +##
is Plus[##]
which is Plus[u, v]
, i.e. the numerator we want.
In the denominator on the other hand, ##
appears as the left-hand operator of /
. The reason this multiplies u
and v
is rather subtle. /
is implemented in terms of Times
:
FullForm[a/b]
(* Times[a, Power[b, -1]] *)
So when a
is ##
, it gets expanded afterwards and we end up with
Times[u, v, Power[9*^16, -1]]
Here, *^
is just Mathematica's operator for scientific notation.
MATL, 9 bytes
sG3e8/pQ/
Try it online!
s % Take array [u, v] implicitly. Compute its sum: u+v
G % Push [u, v] again
3e8 % Push 3e8
/ % Divide. Gives [u/c, v/c]
p % Product of array. Gives u*v/c^2
Q % Add 1
/ % Divide. Display implicitly
Jelly, 9 bytes
÷3ȷ8P‘÷@S
Try it online! Alternatively, if you prefer fractions, you can execute the same code with M.
How it works
÷3ȷ8P‘÷@S Main link. Argument: [u, v]
÷3ȷ8 Divide u and v by 3e8.
P Take the product of the quotients, yielding uv ÷ 9e16.
‘ Increment, yielding 1 + uv ÷ 9e16.
S Sum; yield u + v.
÷@ Divide the result to the right by the result to the left.