Last active
September 1, 2016 23:26
-
-
Save bryceandress/8b434966816ba663b0ea7a8a1f377a29 to your computer and use it in GitHub Desktop.
powmod homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func simpPowMod(base, exp, mod int) int { | |
var val int = 1 | |
for i:=0; i < exp; i++ { | |
val = val * base | |
} | |
return val % mod | |
} | |
func betterPowMod(base, exp, mod int) int { | |
var val int = 1 | |
for i:=0; i < exp; i++ { | |
val = (val * base) % mod | |
} | |
return val | |
} | |
func qkPowMod(base, exp, mod int) int { | |
var accum int = 1 | |
for ; exp > 0; exp = exp/2 { | |
if exp % 2 > 0 { | |
accum = (accum * base) % mod | |
} | |
base = (base * base) % mod | |
} | |
return accum | |
} | |
func main() { | |
var base, exp, mod int | |
fmt.Println("Base: ") | |
fmt.Scanf("%d", &base) | |
fmt.Println("Exponent: ") | |
fmt.Scanf("%d", &exp) | |
fmt.Println("Modulus: ") | |
fmt.Scanf("%d", &mod) | |
/* | |
base = 2147483647 | |
exp = 2147483647 | |
mod = 212116501 | |
*/ | |
var total int = qkPowMod(base, exp, mod) | |
fmt.Println(total) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment