5

[Golang] Integer Exponentiation

 2 years ago
source link: https://siongui.github.io/2017/05/20/go-integer-exponentiation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[Golang] Integer Exponentiation

May 20, 2017

Integer exponentiation in Go programming language.

Run Code on Go Playground

package main

import (
      "fmt"
)

// return a^n
func Power(a, n uint) uint {
      var i, result uint
      result = 1
      for i = 0; i < n; i++ {
              result *= a
      }
      return result
}

func main() {
      fmt.Println(Power(2, 0))
      fmt.Println(Power(2, 1))
      fmt.Println(Power(2, 2))
      fmt.Println(Power(2, 3))
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK