Quick actions

cmd+k|ctrl+k

Navigation

Languages

Eksponentiation

Snippet info

Language

Go

Visibility

public

Author

ubaidillah.hf

Created

2021-07-11T08:24:11.494466Z

Updated

2021-07-11T08:25:48.238329Z

package main

import (
    "fmt"
)

// x = 2, n = 5
// 2 x 2 x 2 x 2 x 2 = 36
//   4   x   4   x 2 = 36
func pow(x,n int) int{
    var hasil int=1
    
    for n > 0 {
        fmt.Println("## Iterate")
        fmt.Println("PANGKAT =>",n)
        if n % 2 == 1 {
            fmt.Println("hasil =", hasil , " * " , x, "=",hasil*x)
            hasil*=x
        }
        
        fmt.Println("n =", n , " / " , 2 ,"=",n/2)
        fmt.Println("x =", x , " * " , x ,"=",x*x)
        n /= 2
        x *= x
    }
    fmt.Println("HASIL AKHIR ITERATE =>",hasil)
    return hasil
}

func main() {
    fmt.Println(pow(2,5))
}
INFO