Skip to content

Instantly share code, notes, and snippets.

@kanapuli
Created January 20, 2017 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanapuli/197259b97d8c7d7d8adf84b2836dae76 to your computer and use it in GitHub Desktop.
Save kanapuli/197259b97d8c7d7d8adf84b2836dae76 to your computer and use it in GitHub Desktop.
Find the Greatest Common Divisor between Two Numbers
package main
import "fmt"
//FindGCD - finds the greatest common divisor between two numbers
func FindGCD(a, b int) (gcd int) {
var list []int
for i := 1; i <= smallestNumber(a, b); i++ {
if a%i == 0 && b%i == 0 {
list = append(list, i)
}
}
//return the last element in the slice
return list[len(list)-1]
}
func smallestNumber(a, b int) (c int) {
if a < b {
return a
}
return b
}
func main(){
firstNum := 63
secondNum := 35
greatestFactor := FindGCD(firstNum , secondNum)
fmt.Println(greatestFactor)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment