13
\$\begingroup\$

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

19 Answers 19

7
\$\begingroup\$

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!

\$\endgroup\$
6
\$\begingroup\$

Python, 31 bytes

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

Attempt This Online!

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

Haskell, 23 bytes

(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

\$\endgroup\$
2
\$\begingroup\$

Uiua SBCS, 12 bytes

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

Try it!

\$\endgroup\$
2
\$\begingroup\$

Awk, 25 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

R, 30 bytes

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

Attempt This Online!

Port of @l4m2's JavaScript answer

R, 33 bytes

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

Attempt This Online!

Straightforward approach.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 8 bytes

Rü@2β3Bè

Try it online or verify all test cases.

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 ü@ 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
\$\endgroup\$
2
\$\begingroup\$

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

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

Perl 5 -Mfeature+signatures, 35 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 40 bytes

=IFS(A1<=A2,A2,(A1>A2)*(A2<=A3),A1,1,A3)
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

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.

\$\endgroup\$
1
\$\begingroup\$

TI-BASIC, 25 bytes

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

Takes input in Ans as a list.

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 26 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Funge-98, 29 bytes

"HTRF"4(&&&RROO`!2j@._2P`j$.@

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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

Port of l4m2's answer

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

Attempt This Online!

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

Charcoal, 18 bytes

NθNηNζI⎇‹ηθ⎇‹ζηθζη

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.

Nθ                  First input as a number
  Nη                Second input as a number
    Nζ              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
      I             Cast to string
                    Implicitly print
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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