-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multiply Strings.go
66 lines (58 loc) · 1.21 KB
/
Multiply Strings.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
62
63
64
65
66
package main
import (
"fmt"
"math"
)
func sum(num1 string, num2 string) string {
res := ""
remainder := 0
for i := 0; i < len(num1) || i < len(num2); i++ {
cur := remainder
if i < len(num1) {
cur += int(num1[len(num1)-1-i] - '0')
}
if i < len(num2) {
cur += int(num2[len(num2)-1-i] - '0')
}
remainder = cur / 10
cur = cur % 10
res = fmt.Sprint(cur) + res
}
if remainder != 0 {
res = fmt.Sprint(remainder) + res
}
return res
}
func multiply(num1 string, num2 string) string {
current := "0"
tot := "0"
remainder := byte(0)
for j := len(num2) - 1; j >= 0; j-- {
for i := len(num1) - 1; i >= 0; i-- {
mul := (num2[j]-'0')*(num1[i]-'0') + remainder
remainder = 0
remainder = mul / 10
mul = mul % 10
current = sum(current, fmt.Sprint(int(mul)*int(math.Pow(10, float64(len(num1)-1-i)))))
}
current = sum(current, fmt.Sprint(int(remainder)*int(math.Pow(10, float64(len(num1))))))
remainder = 0
for k := 0; k < len(num2)-1-j; k++ {
current = current + "0"
}
tot = sum(tot, current)
current = "0"
}
firstNonZero := 0
for i, c := range tot {
if c != '0' {
firstNonZero = i
break
}
}
res := tot[firstNonZero:]
if len(res) == 0 {
res = "0"
}
return res
}