Search code examples
gopowgodoc

What is the equivalent for bigint.pow(a) in Go?


In my use case, I would like to know how the following Java code would be implemented in Go

BigInteger base = new BigInteger("16");
int exponent = 1;
BigInteger a = base.pow(exponent); //16^1 = 16

I am able to import the math/big package and create big integers, but not able to do Pow() function in Go. Also I don't find the function in the Go doc.

Do I have to implement my own version of Pow() for bigint? Could anyone help me on this?


Solution

  • Use Exp with m set to nil.

    var i, e = big.NewInt(16), big.NewInt(2)
    i.Exp(i, e, nil)
    fmt.Println(i) // Prints 256
    

    Playground: http://play.golang.org/p/0QFbNHEsn5