-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
53 lines (48 loc) · 890 Bytes
/
common.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
package mathics
// GCDT is Greatest Common Factor function for two number
func GCDT(a int, b int) int {
for b != 0 {
t := b
b = a % b
a = t
}
return a
}
// GCDM is Greatest Common factor function for more than two number
func GCDM(i []int) int {
l := len(i)
if l != 0 {
a := i[0]
for x := 1; x < l; x++ {
b := i[x]
a = GCDT(a, b)
}
return a
} else {
return 0
}
}
// LCMT is Least Common Multiple function for two number
func LCMT(a int, b int) int {
return (a * b) / GCDT(a, b)
}
// LCMM is Least Common Multiple function for more than two number
func LCMM(i []int) int {
l := len(i)
a := 0
b := 0
for x := 0; x < (l - 1); x++ {
a = i[x] % i[x+1]
if a == 0 {
i[x+1] = (i[x] * i[x+1]) / i[x+1]
} else {
b = i[x+1] % a
if b == 0 {
i[x+1] = (i[x] * i[x+1]) / a
} else {
i[x+1] = (i[x] * i[x+1]) / b
}
}
}
return i[l-1]
}