Reng v.3.3, 33 bytes
ii+i-:²1#x1ø
:x²eq!vx1+#x
~n%2+<
Takes input like a c b
. Try it here!
The calculation is simple. i
gets input, so ii+
is a + c
, and i-
is then (a + c) - b
. Very simple stuff.
The hard part is taking the max with 0
. "What!? How could it be that hard?" Well, I didn't implement inequality in Reng. So we'll have to use math!
Observation 1: max(a,b) = (a+b+|a-b|)/2
Observation 2: max(a,0) = (a+|a|)/2
Observation 3: |a| = Sqrt[a^2]
Observation 4: Reng doesn't have square roots, either. Nor does it have power. But knowing that we'll only be taking the square roots of perfect squares, we can use trial and error, starting at 1. That is, our algorithm can look like this:
x = 1 to a, iterate:
if x * x == a break, return X
Since we always have k
being a perfect square, this algorithm always terminates.
So, into the rest of the code explanation!
ii+i-:²1#x1ø
:x²eq!vx1+#x
~n%2+<
part 1: initialization
I've already explained how ii+i-
works, so let's look at the rest of the line.
:²1#x1ø
:
duplicates this number. The bottom number will be a
in (a + |a|)/2
. Now, we need to take the absolute value of the top value. We can do this using observations 3 and 4. ²
squares the top value, and 1#x
initializes our counter x
with 1
. Then we go to the next line with 1ø
part 2: square root loop
:x²eq!vx1+#x
:
duplicates our maximum value a
from the top of the stack (for the equality check). x²
squares our counter x
and e
pushes a Boolean representing the equality of x²
and a
. If they are equal, q!
breaks out of the loop by going down (v
), leaving |a|
on the stack. Otherwise, we increment x
(x1+
) and set x
to that value (#x
).
finalization
~n%2+<
<
redirects the program to look left. shhh it saves bytes. It's equivalent to this series of steps:
+2%n~
+
adds the top two of the stack, thus implementing the first part of (a + |a|)/2
. 2%
divides that sum by two (%
is division here because /
is a mirror). Lastly, we output this as a number (n
) and terminates the program (~
).
Is the orientation of the sides always as in the picture? – Dennis – 2016-05-04T14:37:37.993
@Dennis Yes, it is. – Leaky Nun – 2016-05-04T14:38:09.260
7According to Wikipedia's mention of the Pitot theorem,
a+c-b
should be the only valid solution, provided it's positive. – Martin Ender – 2016-05-04T14:43:12.673Is the order for inputting
a
,b
andc
strict, or can I also provide input likea, c, b
? – Adnan – 2016-05-04T15:06:39.7632Input are not strict by default. – Leaky Nun – 2016-05-04T15:08:38.857
8
In case no one has mentioned it before, I recommend posting challenges to the Sandbox where they can get feedback before going live.
– Alex A. – 2016-05-04T16:56:11.2006
@AlexA. Strangely, it seems he is aware of the sandbox as he keeps recommending that other people use it.
– Suever – 2016-05-05T01:46:50.493