-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeastCommonMultiple.go
More file actions
46 lines (36 loc) · 918 Bytes
/
LeastCommonMultiple.go
File metadata and controls
46 lines (36 loc) · 918 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
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"bufio"
"fmt"
"math/big"
"os"
"strings"
)
func greatestCommonDivisor(n1 *big.Int, n2 *big.Int) *big.Int {
if len(n2.Bits()) == 0 {
return n1
}
mod := new(big.Int).Mod(n1, n2)
return greatestCommonDivisor(n2, mod)
}
func calculateLeastCommonMultiple(n1 *big.Int, n2 *big.Int) *big.Int {
divisor := greatestCommonDivisor(n1, n2)
return new(big.Int).Div(new(big.Int).Mul(n1, n2), divisor)
}
func main() {
reader := bufio.NewReader(os.Stdin)
nums, _ := reader.ReadString('\n')
nums = strings.TrimSpace(nums)
stringSlice := strings.Split(nums, " ")
bigIntSlice := make([]*big.Int, len(stringSlice))
for i, str := range stringSlice {
num, ok := new(big.Int).SetString(str, 10)
if !ok {
fmt.Println("cant parse", str, "as big int")
return
}
bigIntSlice[i] = num
}
result := calculateLeastCommonMultiple(bigIntSlice[0], bigIntSlice[1])
fmt.Println(result)
}