Skip to content

Instantly share code, notes, and snippets.

@nawada
Created June 29, 2015 10:34
Show Gist options
  • Save nawada/ececcfb8abc9480d456f to your computer and use it in GitHub Desktop.
Save nawada/ececcfb8abc9480d456f to your computer and use it in GitHub Desktop.
ユークリッドの互除法
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
val1, err1 := strconv.Atoi(os.Args[1])
val2, err2 := strconv.Atoi(os.Args[2])
if err1 != nil || err2 != nil {
val1, val2 = 1, 1
}
result1, _ := EuclideanMethod(val1, val2)
fmt.Println(result1)
}
func EuclideanMethod(val1 int, val2 int) (int, int) {
if val2 == 0 {
return val1, val2
} else if val2 > val1 {
result := val2 % val1
return EuclideanMethod(val1, result)
} else {
result := val1 % val2
return EuclideanMethod(val2, result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment