26
\$\begingroup\$

This is a CMC (chat mini challenge) that I posted in our chatroom, The Ninteenth Byte, a bit ago.

The Challenge

Given a positive integer x, depending on the last 2 bits of x, do the following:

x & 3 == 0: 0
x & 3 == 1: x + x
x & 3 == 2: x * x
x & 3 == 3: x ^ x (exponentiation)

Input/Output

Single Integer -> Single Integer  

A trailing newline is permitted in the output. No other whitespace is permitted.

Testcases

input       output
    1            2
    2            4
    3           27
    4            0
    5           10
    6           36
    7       823543
    8            0
    9           18
   10          100
   11 285311670611
   12            0

This is a challenge, so the shortest code wins!

\$\endgroup\$
  • 8
    \$\begingroup\$ Shouldn't the 0 case be x + 2, seeing as how the others are x * 2, x ^ 2, and x ^^ 2 (tetration)? :P \$\endgroup\$ – ETHproductions May 1 '17 at 13:47
  • \$\begingroup\$ What is the largest output we should support (regarding x ^ x)? 32-bit is already not enough for test case 11, and 64-bit is not enough for test case 19. \$\endgroup\$ – Kevin Cruijssen May 1 '17 at 14:23
  • \$\begingroup\$ @KevinCruijssen I will say that you only have to handle cases where the input and output are in the scope of your language, as long as your program would theoretically work given infinite number size. \$\endgroup\$ – HyperNeutrino May 1 '17 at 14:28
  • \$\begingroup\$ @HyperNeutrino Ok, in that case I've fixed my (Java 7) code (for +72 bytes.. xD) \$\endgroup\$ – Kevin Cruijssen May 1 '17 at 14:42

38 Answers 38

0
\$\begingroup\$

vba, 101

Function x(f)
Select Case f Mod 4
Case 0:x=0
Case 1:x=f+f
Case 2:x=f*f
Case 3:x=f ^f
End Select
End Function
\$\endgroup\$
0
\$\begingroup\$

Dyvil, 24 Bytes

x=>[0,x+x,x*x,x**x][x&3]

Usage:

let f: int->int = x=>[0,x+x,x*x,x**x][x&3]
print f(10) // => 100

Explanation:

The code creates a lambda expression that takes and returns a 32-bit integer. The body stores the result of all calculations in an array and retrieves the result from the index given by the last 2 bits of x.

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

TI-BASIC, 27, 23 bytes

:Prompt X          //Get input as X, 3 bytes
:{2X,X²,X^X,0      //Implicitly store the list {2X,X²,X^X,0} to Ans, 16 bytes
:Ans(remainder(X,4 //Compute X&3 (as X%4), get that element of the list, and implicitly print 8 bytes

TI-BASIC doesn't have bitwise operators, so I used the modulus operator (remainder() instead.

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

Swift, 105 68 67 bytes

Here is the body of the function:

i in let a=i%4;return [0,i+i,i*i,Int(pow(Double(i),Double(i)))][a]

Here is the full implementation:

import Foundation;var e:(Int)->Int={i in let a=i%4;return [0,i+i,i*i,Int(pow(Double(i),Double(i)))][a]}

Try it here

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

oK, 20 bytes

{*/(."-+*#"4!x).x,x}

Try it online!

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

Pure Bash, 35

a=(- + * **)
echo $[$1${a[$1%4]}$1]

Try it online.

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

Axiom, 59 33 bytes

h(x)==[0,x+x,x*x,x^x].(1+x rem 4)

traslate in Axiom the answer of Mego... this is the old 59 bytes

h(x)==(x quo 2 rem 2=1=>(x rem 2=0=>x^2;x^x);(x rem 2)*2*x)

results

(24) -> for i in 0..30 repeat output [i,h(i)]
   [0,0]
   [1,2]
   [2,4]
   [3,27]
   [4,0]
   [5,10]
   [6,36]
   [7,823543]
   [8,0]
   [9,18]
   [10,100]
   [11,285311670611]
   [12,0]
   [13,26]
   [14,196]
   [15,437893890380859375]
   [16,0]
   [17,34]
   [18,324]
   [19,1978419655660313589123979]
   [20,0]
   [21,42]
   [22,484]
   [23,20880467999847912034355032910567]
   [24,0]
   [25,50]
   [26,676]
   [27,443426488243037769948249630619149892803]
   [28,0]
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 59 bytes

#include<math.h>
h(x){return x&2?pow(x,x&1?x:2):(x&1)*2*x;}

Test and results. It overflows at 11.

main()
{int i;
 for(i=0;i<20;++i)
       printf("%u %u\n", i, h(i));
}

0 0
1 2
2 4
3 27
4 0
5 10
6 36
7 823543
8 0
9 18
10 100
11 1843829075
12 0
13 26
14 196
15 1500973039
16 0
17 34
18 324
19 0

Ideone

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Why someone downvote this? \$\endgroup\$ – RosLuP May 4 '17 at 14:15
  • \$\begingroup\$ does not compile.int main(); int h(int x) please! \$\endgroup\$ – user69099 May 9 '17 at 21:54
  • \$\begingroup\$ @xakepp35 here ideone.com/4eI270 it compile; if it is for arguments of function, in a old standard they are seen from compiler all as int... \$\endgroup\$ – RosLuP May 9 '17 at 22:03
  • \$\begingroup\$ In "tio" gcc compier not compile even this: #include <math.h> #include <stdlib.h> double h(int x){return x&2?pow((double)x,(double)(x&1?x:2)):(double)((x&1)*2*x);}; it says the linker not find the pow function (double pow(double, double) in math.h header) \$\endgroup\$ – RosLuP May 9 '17 at 22:35
  • \$\begingroup\$ The trick it is to add "-lm" [link math functions] to the line compiler is called \$\endgroup\$ – RosLuP May 10 '17 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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