Created
January 20, 2017 16:23
-
-
Save kanapuli/197259b97d8c7d7d8adf84b2836dae76 to your computer and use it in GitHub Desktop.
Find the Greatest Common Divisor between Two Numbers
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" | |
//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