Skip to content

Instantly share code, notes, and snippets.

@thekarel
Created April 11, 2015 22:10
Show Gist options
  • Save thekarel/5e4027a74655f72c481b to your computer and use it in GitHub Desktop.
Save thekarel/5e4027a74655f72c481b to your computer and use it in GitHub Desktop.
GCD in go
package main
import (
"fmt"
"math"
)
func gcd(a, b int) int {
for b != 0 {
rem := math.Mod(float64(a), float64(b))
a = b
b = int(rem)
}
return a
}
func main() {
fmt.Println(gcd(30, 8))
}
@md2perpe
Copy link

md2perpe commented Jul 4, 2016

Why math.Mod and conversion between int and float64 when there's % ?

func gcd(a, b int) int {
    for b > 0 {
        a, b = b, a%b
    }

    return a
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment