You receive an input with a number and a string with a space in the middle. The string tells you what you need to do with the number, it can be:

sqrt - square root of number

sqr - square of number

cube - cube of number

abs - absolute of number

round - rounded version of number

You need to output the final result of the calculation with whatever you have. If your language doesn't have an input, you can hard-code that in.


You can't use a math library, even if it is built-in.

You don't get a number or a result bigger than the highest possible one in your language.

Because of the vulnerability of the above rule, you can't use languages, which only support a 1 bit number. (1 and 0)

If your language doesn't support non-integer numbers, you need to output a rounded result.

Test cases

16 sqrt = 4

25 sqr = 625

3 cube = 27

-10 abs = 10

18.38 round = 18

Because this is a code-golf, the shortest answer in bytes wins.

Good luck!


closed as unclear what you're asking by Dennis Feb 12 '16 at 18:39

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ 25 sqr = 625? \$\endgroup\$ – andlrc Feb 12 '16 at 12:02
  • 1
    \$\begingroup\$ can sqrt result in decimal values eg 5 sqrt? \$\endgroup\$ – Eumel Feb 12 '16 at 12:02
  • 6
    \$\begingroup\$ Define a built-in math library \$\endgroup\$ – LegionMammal978 Feb 12 '16 at 12:03
  • 1
    \$\begingroup\$ @Bálint Javascript's Math is not a class. \$\endgroup\$ – SuperJedi224 Feb 12 '16 at 13:07
  • 2
    \$\begingroup\$ It's not clear what can be used to calculate the requested operation. any sort of functionality which allows you to calculate the square root of a number is prohibited makes the challenge, by definition, impossible. You'd also have to clarify what counts as rounding. \$\endgroup\$ – Dennis Feb 12 '16 at 18:39

Mathematica, 155 153 bytes

{a=ToExpression@#,a a,a a a,c=a~Mod~1,a+Boole[c>=.5]-c,,,,If[Chop@a==0,0,a/Sign@a],If[a>0,(#+a/#)/2&~FixedPoint~1.,0]}[[Hash@#2~Mod~12]]&@@StringSplit@#&

Could probably be golfed further. Doesn't use "any sort of functionality, wich allows you to calculate the squareroot/square/cube/absolute/roubded version of a number."


Bash - 141 137 132

t(){ for((;x*x!=$1;x++));do :;done;}
r(){ ((x=$1*$1));}
e(){ ((x=$1**3));}
s(){ x=${1#-};}
d(){ x=${1%.*};}
${2:${#2}-1} $1
echo $x

Run as

bash math.sh 16 sqrt
bash math.sh 25 sqr
bash math.sh 3 cube
bash math.sh -10 abs
bash math.sh 18.38 round

It strips the operation done to the last character, which is unique for each, then calls the appropriate function with the number

  • \$\begingroup\$ How does the square root work if the number is not a perfect square? Wouldn't it be an infinite loop? (Maybe replace != with < ? Even then the rounding seems wrong) \$\endgroup\$ – senegrom Feb 12 '16 at 14:11

ES6 - 124 bytes

(a,b)=>eval('_=>'+'{for(c=1;c*c<a;c++);return c}_a*a_a*a*a_a<0?-a:a_a.toFixed()'.split`_`['tresd'.indexOf(b[b.length-1])])()

Square root only works for rounded numbers. Sqrt of 15 will result in 4, I hope this is allowed. Re-writing a fully functional square root function feels like overkill.


(a,b)=> // anonymous function
        '_=>' + // construct an anonymous function to eval
        '{for(c=1;c*c<a;c++);return c} // sqrt
        _ // seperator
        a*a // sqr
        a*a*a // cube
        a<0?-a:a // abs
        a.toFixed()' // round
        .split`_` // create an array of functions
        [ // select the function to use
            'tresd'.indexOf(b[b.length-1]) // get index of input
    )() // end eval and call the returned function

Golfing tips are welcome

Test cases:

(a,b)=>eval('_=>'+'{for(c=1;c*c<a;c++);return c}_a*a_a*a*a_a<0?-a:a_a.toFixed()'.split`_`['tresd'.indexOf(b[b.length-1])])()

F=(a,b)=>document.body.innerHTML+='<pre>'+a+', '+b+':\n'+f(a,b)+'\n</pre>';


  • \$\begingroup\$ It is. As some of the people mentioned here, you can use the last letter of the input instead of the whole word, because it is unique for every type. \$\endgroup\$ – Bálint Feb 12 '16 at 14:19

Python 2, 153 bytes

exec'print '+dict(zip('tresd',S('[a for a in range(x)if a*a==x][0]|x*x|x**3|abs(x)|round(x)','|')))[f[-1]].replace('x',x)

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