# The TAK function (easy mode)

The TAK function is defined as follows for integers $$\x\$$, $$\y\$$, $$\z\$$:

$$t(x, y, z) = \begin{cases} y, & \text{if x \le y} \\ t(t(x-1,y,z), t(y-1,z,x), t(z-1,x,y)), & \text{otherwise} \end{cases}$$

It can be proved that it always terminates and evaluates to the simple function below:

$$t(x, y, z) = \begin{cases} y, & \text{if x \le y} \\ z, & \text{if x > y and y \le z} \\ x, & \text{otherwise} \end{cases}$$

Your job is to implement the function. (As per the standard rules, it is OK to implement the simplified version, or anything else that evaluates to the same value for all inputs.)

You may assume that the three input values are nonnegative integers.

Standard rules apply. The shortest code in bytes wins.

For a harder challenge, check out "The TAK function".

## Test cases

(x, y, z) -> output

(10, 20, 100) -> 20
(20, 20, 100) -> 20
(20, 10, 100) -> 100
(20, 10, 10) -> 10
(20, 10, 0) -> 20


# JavaScript (Node.js), 25 bytes

f=(x,y,z)=>x>y?f(y,z,x):y


Attempt This Online!

Not shortest but fun

# JavaScript (Node.js), 22 bytes

(x,y,z)=>x>y?y>z?x:z:y


Attempt This Online!

# Python, 31 bytes

lambda x,y,z:x*(x>y>(y:=z))or y


Attempt This Online!

• Every day I learn new ways how not to write an if-then-else in python! Thank you!
– Stef
Commented Feb 4 at 19:41

(x#y)z|x>y=(y#z)x|1<2=y


Try it online!

I see that I4m2 found this approach before I did. Could've saved me the dozen minutes I spent trying to find a non-obvious approach. :P

# UiuaSBCS, 12 bytes

⊡◿3+1⊗1≤↻1..


Try it!

# Awk, 25 bytes

1,$0=$1>$2?$2>$3?$1:$3:$2


Try it online!

# R, 30 bytes

t=\(x,y,z)if(x>y,t(y,z,x),y)


Attempt This Online!

# R, 33 bytes

\(x,y,z)if(x>y,if(y>z,x,z),y)


Attempt This Online!

Straightforward approach.

# 05AB1E, 8 bytes

Rü@2β3Bè


Explanation:

R         # Reverse the (implicit) input-triplet
ü        # For each overlapping pair:
@       #  Do a ≥ check
# (so we now have a pair [z≥y,y≥x])
2β     # Convert this pair from a base-2 list to a base-10 integer
3B   # Convert this base-10 integer to a base-3 integer
è  # Use that to 0-based modular index into the (implicit) input-triplet
# (after which the result is output implicitly)

input R ü@ 2β 3B (0-based modular index) è (output)
[10,20,100] [100,20,10] [1,1] 3 10 1 20
[20,20,100] [100,20,20] [1,1] 3 10 1 20
[20,10,100] [100,10,20] [1,0] 2 2 2 100
[20,10,10] [10,10,20] [1,0] 2 2 2 10
[20,10,0] [0,10,20] [0,0] 0 0 0 20
[10,20,0] [0,20,10] [0,1] 1 1 1 20

# Desmos, 29 28 bytes

-1 bytes thanks to @Neil


f(a,b,c)=\{a<=b:b,b>c:a,c\}


Literally just the piecewise function given in the challenge description.

Try It On Desmos!

Try It On Desmos! - Prettified

• Doesn't b>c:a,c work?
– Neil
Commented Feb 2 at 9:37
• @Neil oh yea, i was just copying the formula verbatim LOL Commented Feb 2 at 20:35

# Perl 5-Mfeature+signatures, 35 bytes

sub($x,$y,$z){$y<$x?$z<$y?$x:$z:$y}


Try it online!

=IFS(A1<=A2,A2,(A1>A2)*(A2<=A3),A1,1,A3)


# PARI/GP, 27 bytes

t(x,y,z)=if(x>y,t(y,z,x),y)


Attempt This Online!

A port of @l4m2's Javascript answer.

# PARI/GP, 27 bytes

t(x,y,z)=if(x<=y,y,y>z,x,z)


Attempt This Online!

# Jelly, 6 bytes

>Ɲi0‘ị


A monadic Link that accepts a list, [x, y, z], and yields the result of the TAK function.

Try it online!

### How?

>Ɲi0‘ị - Link: list of comparables, [x, y, z]
Ɲ     - for neighbouring pairs:
>      -   greater than?         -> [x>y, y>z] == [not(x<=y), not(y<=z)]
i0   - first 1-indexed index of 0, or 0 if not found
-> 1 if x<=y
2 if x>y and y<=z
0 if x>y and y>z
‘  - increment
-> 2 if x<=y
3 if x>y and y<=z
1 if x>y and y>z
ị - 1-index into {[x, y, z]}
-> y if x<=y
z if x>y and y<=z
x if x>y and y>z


# Retina 0.8.2, 28 bytes

.+
$* ^(1*¶)??(1*)¶\2$2
\G1


Try it online! Takes input on separate lines but link is to test suite that splits on non-digits for convenience. Explanation:

.+
$*  Convert to unary. ^(1*¶)??(1*)¶\2$2


If x<=y then delete x, otherwise if y<=z then delete both x and y. (The ?? is needed to prefer the x<=y test.)

\G1


Convert the first remaining input to decimal.

# TI-BASIC, 25 bytes

Ans(2+(Ans(1)>Ans(2))cos(π(Ans(2)≠min(Ans


Takes input in Ans as a list.

# C (gcc), 26 bytes

f(x,y,z){x=x>y?y>z?x:z:y;}


Try it online!

# JavaScript (Node.js), 28 bytes

(x,y,z)=>x<=y?y:x>y&y<=z?z:x


Attempt This Online!

Literal translation of the simple version.

# Funge-98, 29 bytes

"HTRF"4(&&&RROO!2j@._2Pj\$.@


Try it online!

# Python, 35 32 bytes

-3 bytes thanks to @l4m2

lambda x,y,z:[y,[z,x][y>z]][x>y]


Attempt This Online!

# Python, 36 bytes

f=lambda x,y,z:f(y,z,x)if x>y else y


Attempt This Online!

• The first answer needs the leading f= because it is a recursive function. Commented Feb 2 at 6:00
• Python, 32 bytes: lambda x,y,z:[y,[z,x][y>z]][x>y]
– l4m2
Commented Feb 2 at 7:01
• @Bubbler, fixed it. Commented Feb 2 at 9:01

# Charcoal, 18 bytes

ＮθＮηＮζＩ⎇‹ηθ⎇‹ζηθζη


Try it online! Link is to verbose version of code. Explanation: Straightforward approach. For a more interesting approach, my answer to the linked question needs to perform this calculation three times in a loop so it uses array indexing arithmetic.

Ｎθ                  First input as a number
Ｎη                Second input as a number
Ｎζ              Third input as a number
η          Second input
‹           Is less than
θ         First input
⎇            If true then
ζ      Third input
‹       Is less than
η     Second input
⎇        If true then
θ    First input
ζ   Else third input
η  Else second input
Ｉ             Cast to string
Implicitly print