# Calculating roots of a number

Good day everyone.

How do we calculate the square root, cube root and so on?

I know there is the sqrt() function which allows me to find the square root. But what about the further roots? How can we calculate them? If i want the fourth or fifth root how do i do it?

It would be great if someone tells me what actually we are doing in finding roots.

Thank you.

What did Google tell you?

dannable: What did Google tell you?

The root of a number x is another number, which when multiplied by itself a given number of times, equals x.

So from what i understand a root of a number x can mathematically expressed as 'x to the power of one over the number of times to be multiplied'.

For example: Fourth root of x is equal to x to the power of one-fourth.

How to multiply x one by four times?

Correct me if i'm wrong.

You are correct. A quick test on a scientific calculator proves this.

3 ^ 4 = 81

81 ^ 0.25 (1/4) = 3

Roots can be done with fractional powers. Ie: pow(x, 0.333333333) will give you about the cube root of x.

1. ??
2. squareroot ==> sqrt(number);
3. cuberoot ==> cbrt(number);
4. quadroot ==> sqrt(sqrt(number));
5. pentaroot ==> pow(number, 0.2);
6. sixtroot ==> cbrt(sqrt(number));

in general

``````double nthroot(double number, double n)
{
if (n == 0) return NAN;
if (number > 0) return pow(number, 1.0 / n);
if (number == 0) return 0;
if (number < 0 && int(n) == n && (int(n) & 1)) return -pow(-number, 1.0 / n);
return NAN;
}
``````

This link gives information regarding finding the nth root of a number. It also shows Newton's method for calculation.

What remains now is converting maths into a function.

Thanks for your replies.

4) quadroot ==> sqrt(sqrt(number));

An interesting thought! It turns out that pow() is the slowest of the AVR math functions; it's about 10x slower than the sqrt() function; cbrt() is about twice as fast as pow()... http://www.nongnu.org/avr-libc/user-manual/benchmarks.html

I was broadly thinking of using log() for the calculation. But I don't have the IDE or an Arduino at work so couldn't develop the idea.

lohitpm: This link gives information regarding finding the nth root of a number. It also shows Newton's method for calculation.

What remains now is converting maths into a function.

So give it a try ;)