# Finding Cube Root of Specified Number in Golang

In mathematics, a cube root of a number is a value that, when multiplied by itself twice, gives the number. Golang provides several ways to calculate the cube root of a specified number.

In this article, we will discuss different methods to calculate the cube root of a specified number in Golang.

## Method 1: Using Math.Pow() Function

The easiest way to find the cube root of a specified number is to use the math.Pow() function. We can use the math.Pow() function to calculate the cube root of a number by raising the number to the power of 1/3. The following code demonstrates this method −

### Example

package main

import (
"fmt"
"math"
)

func main() {
var n float64 = 27
fmt.Println("Cube root of", n, "is", math.Pow(n, 1.0/3.0))
}


### Output

Cube root of 27 is 2.9999999999999996


## Method 2: Using math.Cbrt() Function

The math package in Golang also provides a built-in function called math.Cbrt() that calculates the cube root of a specified number. The following code demonstrates how to use math.Cbrt() function −

### Example

package main

import (
"fmt"
"math"
)

func main() {
var n float64 = 64
fmt.Println("Cube root of", n, "is", math.Cbrt(n))
}


### Output

Cube root of 64 is 4


## Method 3: Using Newton-Raphson Method

The Newton-Raphson method is an iterative method that can be used to find the cube root of a number. The Newton-Raphson method uses the following formula to calculate the cube root of a number −

x = (2*x + n/(x*x))/3


Where x is an approximation of the cube root of the number n.

The following code demonstrates how to use the Newton-Raphson method to calculate the cube root of a specified number −

### Example

package main

import (
"fmt"
)

func cubeRoot(n float64) float64 {
var x float64 = n / 3.0
for i := 0; i < 10; i++ {
x = (2*x + n/(x*x))/3
}
return x
}

func main() {
var n float64 = 125
fmt.Println("Cube root of", n, "is", cubeRoot(n))
}


### Output

Cube root of 125 is 5


## Conclusion

In this article, we have discussed different methods to calculate the cube root of a specified number in Golang. We can use the math.Pow() function, math.Cbrt() function, or the Newton-Raphson method to find the cube root of a number. The choice of method depends on the specific needs of the program.

Updated on: 12-Apr-2023

54 Views