知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。

说明

算法说明详见 TAOCP 4.6.3 Evaluation of Powers


package main

import "fmt"

func power(x, n uint64) uint64 {
    var total uint64 = 1

    if n == 0 {
        return 1
    } else if n == 1 {
        return x
    } else {
        for n > 0 {
            if n & 1 == 1 {
                total *= x
            }
            x *= x
            n = n >> 1
        }
    }

    return total
}


func main() {
    var x, n, result uint64

    fmt.Print("please input x and n:")

    _, err := fmt.Scanf("%d %d", &x, &n)
    if err != nil {
        fmt.Println("error: ", err)
    }

    result = power(x, n)

    if result == 0 {
        fmt.Println("result is to large!")
    } else {
        fmt.Printf("The result is : %d\n", power(x, n))
    }

}