如何在golang中将int转换为bigint?

我试图描述来实现快速的双斐波那契算法在这里:


// Fast doubling Fibonacci algorithm

package main


import "fmt"


//  (Public) Returns F(n).

func fibonacci(n int) int {

    if n < 0 {

        panic("Negative arguments not implemented")

    }

    fst, _ := fib(n)

    return fst

}


// (Private) Returns the tuple (F(n), F(n+1)).

func fib(n int) (int, int) {

    if n == 0 {

        return 0, 1

    }

    a, b := fib(n / 2)

    c := a * (b*2 - a)

    d := a*a + b*b

    if n%2 == 0 {

        return c, d

    } else {

        return d, c + d

    }

}


func main() {

    fmt.Println(fibonacci(13))

    fmt.Println(fibonacci(14))

}

这适用于小数字;但是,当输入的数字变大时,程序会返回错误的结果。所以我尝试使用bigIntfrommath/big包:


// Fast doubling Fibonacci algorithm

package main


import (

    "fmt"

    "math/big"

)


//  (Public) Returns F(n).

func fibonacci(n int) big.Int {

    if n < 0 {

        panic("Negative arguments not implemented")

    }

    fst, _ := fib(n)

    return fst

}


// (Private) Returns the tuple (F(n), F(n+1)).

func fib(n int) (big.Int, big.Int) {

    if n == 0 {

        return big.Int(0), big.Int(1)

    }

    a, b := fib(n / 2)

    c := a * (b*2 - a)

    d := a*a + b*b

    if n%2 == 0 {

        return c, d

    } else {

        return d, c + d

    }

}


func main() {

    fmt.Println(fibonacci(123))

    fmt.Println(fibonacci(124))

}

但是, go build 抱怨说


cannot convert 0 (type int) to type big.Int

如何缓解这个问题?


交互式爱情
浏览 225回答 1
1回答

阿波罗的战车

使用big.NewInt()代替big.Int()。big.Int()只是类型转换。您需要查看包的文档big您应该主要使用带有形式的方法func (z *T) Binary(x, y *T) *T&nbsp; &nbsp; // z = x op y要在调用Mul方法之后将需要提供结果变量的 2 个参数相乘。因此,例如,要获得 2*2 的结果,您需要:big.NewInt(0).Mul(big.NewInt(2), big.NewInt(2))您可以尝试在 Go playground您还可以创建扩展功能,例如:func Mul(x, y *big.Int) *big.Int {&nbsp; &nbsp; return big.NewInt(0).Mul(x, y)}为了使代码更具可读性:// Fast doubling Fibonacci algorithmpackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "math/big")//&nbsp; (Public) Returns F(n).func fibonacci(n int) *big.Int {&nbsp; &nbsp; if n < 0 {&nbsp; &nbsp; &nbsp; &nbsp; panic("Negative arguments not implemented")&nbsp; &nbsp; }&nbsp; &nbsp; fst, _ := fib(n)&nbsp; &nbsp; return fst}// (Private) Returns the tuple (F(n), F(n+1)).func fib(n int) (*big.Int, *big.Int) {&nbsp; &nbsp; if n == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return big.NewInt(0), big.NewInt(1)&nbsp; &nbsp; }&nbsp; &nbsp; a, b := fib(n / 2)&nbsp; &nbsp; c := Mul(a, Sub(Mul(b, big.NewInt(2)), a))&nbsp; &nbsp; d := Add(Mul(a, a), Mul(b, b))&nbsp; &nbsp; if n%2 == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return c, d&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return d, Add(c, d)&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; fmt.Println(fibonacci(123))&nbsp; &nbsp; fmt.Println(fibonacci(124))}func Mul(x, y *big.Int) *big.Int {&nbsp; &nbsp; return big.NewInt(0).Mul(x, y)}func Sub(x, y *big.Int) *big.Int {&nbsp; &nbsp; return big.NewInt(0).Sub(x, y)}func Add(x, y *big.Int) *big.Int {&nbsp; &nbsp; return big.NewInt(0).Add(x, y)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go