示例#1
0
文件: calc_gcd.go 项目: DuoYao/GoLib
func main() {

	args := os.Args[1:]

	list := []int{}
	for _, val := range args {
		asInt, _ := strconv.Atoi(val)
		list = append(list, asInt)
	}
	if len(list) < 2 || util_common.IsIntegerInSlice(0, list) {
		fmt.Println("Need to privide at least two non-zero integers to calculate GCD")
		return
	}

	var max int = util_common.GetMax(list)
	var min int = util_common.GetMin(list)

	_gcd := util_math.GCD{}

	if len(list) == 2 {
		_gcd.Calc_gcd_euclidean_algo(max, min)
	} else {
		_gcd.Calc_gcd_common(list)
	}

}
示例#2
0
文件: gcd.go 项目: DuoYao/GoLib
func (_gcd GCD) Calc_gcd_common(input []int) {

	var isGCD bool = true

	for i := util_common.GetMax(input); i > 0; i-- {
		isGCD = true
		for _, val := range input {
			if val%i != 0 {
				isGCD = false
				break
			}
		}
		if isGCD == true {
			fmt.Printf("GCD of %v is %d \n", input, i)
			break
		}
	}
}
示例#3
0
文件: gcd.go 项目: DuoYao/GoLib
func (_gcd GCD) Calc_gcd_euclidean_algo(a int, b int) int {
	_max := util_common.GetMax([]int{a, b})
	_min := util_common.GetMin([]int{a, b})

	divider := _min
	remainder := _max % _min
	fmt.Printf("%d = %d * %d + %d \n", _max, _max/divider, divider, remainder)

	for remainder != 0 {
		new_divider := remainder
		new_remainder := divider % remainder
		fmt.Printf("%d = %d * %d + %d \n", divider, divider/new_divider, new_divider, new_remainder)
		remainder = new_remainder
		divider = new_divider
	}

	gcd := divider
	fmt.Printf("GCD of %d and %d is: %d \n", a, b, divider)
	return gcd
}