在Golang中计算大指数幂。

huangapple go评论91阅读模式
英文:

Calculating large exponentiation in Golang

问题

我一直在尝试在Golang中计算2^100。我了解到了数字类型的限制,并尝试使用math/big包。以下是我尝试过的代码,但我无法弄清楚为什么它不起作用。

我使用了二的幂次计算的方法来计算指数。

package main

import (
	"fmt"
	"math/big"
)

func main() {
	two := big.NewInt(2)
	hundred := big.NewInt(50)
	fmt.Printf("2 ** 100 is %d\n", ExpByPowOfTwo(two, hundred))
}

func ExpByPowOfTwo(base, power *big.Int) *big.Int {
	result := big.NewInt(1)
	zero := big.NewInt(0)
	for power != zero {
		if modBy2(power) != zero {
			multiply(result, base)
		}
		power = divideBy2(power)
		base = multiply(base, base)
	}
	return result
}

func modBy2(x *big.Int) *big.Int {
	return big.NewInt(0).Mod(x, big.NewInt(2))
}

func divideBy2(x *big.Int) *big.Int {
	return big.NewInt(0).Div(x, big.NewInt(2))
}

func multiply(x, y *big.Int) *big.Int {
	return big.NewInt(0).Mul(x, y)
}
英文:

I've been trying to calculating 2^100 in Golang. I understand the limit of numeric type and tried using math/big package. Here's what I've tried but I can't figure out why it doesn't work.

I've used computation by powers of two method to calculate the exponentiation.

package main

import (
	"fmt"
	"math/big"
)

func main() {
	two := big.NewInt(2)
	hundred := big.NewInt(50)
	fmt.Printf("2 ** 100	is %d\n", ExpByPowOfTwo(two, hundred))
}

func ExpByPowOfTwo(base, power *big.Int) *big.Int {
	result := big.NewInt(1)
	zero := big.NewInt(0)
	for power != zero {
		if modBy2(power) != zero {
			multiply(result, base)
		}
		power = divideBy2(power)
		base = multiply(base, base)
	}
	return result
}

func modBy2(x *big.Int) *big.Int {
	return big.NewInt(0).Mod(x, big.NewInt(2))
}

func divideBy2(x *big.Int) *big.Int {
	return big.NewInt(0).Div(x, big.NewInt(2))
}

func multiply(x, y *big.Int) *big.Int {
	return big.NewInt(0).Mul(x, y)
}

答案1

得分: 19

BigInt包允许您以对数时间计算x^y(由于某种原因它被称为exp)。您只需要将nil作为最后一个参数传递即可。

package main

import (
	"fmt"
	"math/big"
)

func main() {
	fmt.Println(new(big.Int).Exp(big.NewInt(5), big.NewInt(20), nil))
}

如果您想自己计算它,请看一下我的实现:

func powBig(a, n int) *big.Int{
    tmp := big.NewInt(int64(a))
    res := big.NewInt(1)
    for n > 0 {
        temp := new(big.Int)
        if n % 2 == 1 {
            temp.Mul(res, tmp)
            res = temp
        }
        temp = new(big.Int)
        temp.Mul(tmp, tmp)
        tmp = temp
        n /= 2
    }
    return res
}

或者在Go Playground上尝试一下。

英文:

BigInt package allows you to calculate x^y in log time (for some reason it is called exp). All you need is to pass nil as a last parameter.

package main

import (
	"fmt"
	"math/big"
)

func main() {
	fmt.Println(new(big.Int).Exp(big.NewInt(5), big.NewInt(20), nil))
}

If you are interested how to calculate it by yourself, take a look at my implementation:

func powBig(a, n int) *big.Int{
    tmp := big.NewInt(int64(a))
    res := big.NewInt(1)
    for n > 0 {
        temp := new(big.Int)
        if n % 2 == 1 {
            temp.Mul(res, tmp)
            res = temp
        }
        temp = new(big.Int)
        temp.Mul(tmp, tmp)
        tmp = temp
        n /= 2
    }
    return res
}

or play with it on go playground.

答案2

得分: 13

例如,

package main

import (
    "fmt"
    "math/big"
)

func main() {
    z := new(big.Int).Exp(big.NewInt(2), big.NewInt(100), nil)
    fmt.Println(z)
}

输出:

1267650600228229401496703205376

由于它是2的幂,你也可以进行位移操作:

package main

import (
    "fmt"
    "math/big"
)

func main() {
    z := new(big.Int).Lsh(big.NewInt(1), 100)
    fmt.Println(z)
}

输出:

1267650600228229401496703205376
英文:

For example,

package main

import (
	"fmt"
	"math/big"
)

func main() {
	z := new(big.Int).Exp(big.NewInt(2), big.NewInt(100), nil)
	fmt.Println(z)
}

Output:

1267650600228229401496703205376

Since it's a power of two, you could also do a bit shift:

package main

import (
	"fmt"
	"math/big"
)

func main() {
	z := new(big.Int).Lsh(big.NewInt(1), 100)
	fmt.Println(z)
}

Output:

1267650600228229401496703205376

答案3

得分: 2

如果 power % 2 == 0,你立即返回。相反,你只想得到 base ** (power / 2)result。然后将 result * result 相乘,如果 power 是偶数,则将 base 乘以该结果。

英文:

You are returning immediately if power % 2 == 0. Instead, you just want to get the result of base ** (power /2). Then multiply result * result, and if power is even then multiply base to that.

答案4

得分: 2

计算2的100次方

package main

import (
	"fmt"
	"math/big"
)

func main() {
	n := big.NewInt(0)
	fmt.Println(n.SetBit(n, 100, 1))
}

Playground

英文:

To compute 2^100

package main

import (
	"fmt"
	"math/big"
)

func main() {
	n := big.NewInt(0)
	fmt.Println(n.SetBit(n, 100, 1))
}

Playground

答案5

得分: 1

package main

import (
	"fmt"
	"math/big"
)

func main() {

	bigx, power10 := new(big.Int), new(big.Int)
	var x int64
	bigx.SetInt64(x)                          //将int64类型的x设置给bigx
	power10.Exp(big.NewInt(10), bigx, nil)     //power10 *big.Int指向解决方案

	str10 := power10.Text(10)
	fmt.Printf(str10)                          //打印出数字并自行检查

}
package main

import (
	"fmt"
	"math/big"
)

func main() {

	bigx, power10 := new(big.Int), new(big.Int)
	var x int64
	bigx.SetInt64(x)                          //将int64类型的x设置给bigx
	power10.Exp(big.NewInt(10), bigx, nil)     //power10 *big.Int指向解决方案

	str10 := power10.Text(10)
	fmt.Printf(str10)                          //打印出数字并自行检查

}
英文:
package main

import(
    "fmt"
    "math/big"
)

func main() {

    bigx, power10 := new(big.Int), new(big.Int)
    var x int64
    bigx.SetInt64(x) //set x int64 to bigx
    power10.Exp(big.NewInt(10), bigx, nil) //power10 *big.Int points to solution

    str10 := power10.Text(10)
    fmt.Printf(str10) // print out the number and check for your self

}

huangapple
  • 本文由 发表于 2015年5月12日 13:00:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/30182129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定