Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

pow

godoc

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?

like image 275
Dany Avatar asked Apr 28 '15 06:04

Dany


1 Answers

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

like image 77
Ainar-G Avatar answered Sep 20 '22 01:09

Ainar-G