# Math without math class [closed]

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.

Rules:

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!

• 25 sqr = 625? Feb 12, 2016 at 12:02
• can sqrt result in decimal values eg 5 sqrt? Feb 12, 2016 at 12:02
• Define a built-in math library Feb 12, 2016 at 12:03
• @Bálint Javascript's Math is not a class. Feb 12, 2016 at 13:07
• 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. Feb 12, 2016 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

• 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) Feb 12, 2016 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.

Explanation

(a,b)=> // anonymous function
eval(
'_=>' + // 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:

f=
(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>';

F(16,'sqrt')
F(25,'sqr')
F(3,'cube')
F(-10,'abs')
F(18.38,'round')

• 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. Feb 12, 2016 at 14:19

# Python 2, 153 bytes

S=str.split;x,f=S(raw_input())
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)