-
Notifications
You must be signed in to change notification settings - Fork 0
/
highestCommonFactor.go
61 lines (46 loc) · 938 Bytes
/
highestCommonFactor.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import "fmt"
var result []int
func main() {
var number1, number2, largervalue, temp int
var slice1, slice2 []int
fmt.Println("enter 2 numbers")
fmt.Scanf("%d", &number1)
fmt.Scanf("%d", &number2)
for i := 1; i <= number1; i++ {
if number1%i == 0 {
slice1 = append(slice1, i)
}
}
//fmt.Println(slice1)
for i := 1; i <= number2; i++ {
if number2%i == 0 {
slice2 = append(slice2, i)
}
}
//fmt.Println(slice2)
s := intersection(slice1, slice2)
//fmt.Println(s)
for _, element := range s {
if element > temp {
temp = element
largervalue = temp
}
}
fmt.Printf("Highest common factor of %d and %d is %d\n", number1, number2, largervalue)
}
func intersection(slice1, slice2 []int) []int {
for _, i := range slice1 {
flag := false
for _, j := range slice2 {
if i == j {
flag = true
break
}
}
if flag {
result = append(result, i)
}
}
return result
}