forked from rohan17088/algos
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheuclidean_gcd.go
More file actions
35 lines (31 loc) · 812 Bytes
/
euclidean_gcd.go
File metadata and controls
35 lines (31 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import "fmt"
// EuclideanGCD finds GCD of given numbers by iterative method
// first : first number
// second : second number
// Time complexity :O(log min(first,second))
func EuclideanGCD(first int, second int) int {
for second != 0 {
tmp := second
second = first % second
first = tmp
}
return first
}
// EuclideanGCDRecursive finds GCD of given numbers by recursion
// first : first number
// second : second number
// Time complexity : O(log min(first,second))
func EuclideanGCDRecursive(first int, second int) int {
if second != 0 {
return EuclideanGCDRecursive(second, (first % second))
}
return first
}
func main() {
first := 90
second := 65
fmt.Println("GCD of numbers is :")
fmt.Println(EuclideanGCD(first, second))
fmt.Println(EuclideanGCDRecursive(first, second))
}