-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmultiply.go
More file actions
31 lines (27 loc) · 747 Bytes
/
multiply.go
File metadata and controls
31 lines (27 loc) · 747 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
package multiplystrings
func multiply(num1 string, num2 string) string {
sum := make([]byte, len(num1)+len(num2))
for i := len(num1) - 1; i >= 0; i-- {
var carry, bit byte
// multiply
for j := len(num2) - 1; j >= 0; j-- {
bit = byte(num1[i]-'0')*byte(num2[j]-'0') + carry // bit=num1[i]*num2[j]+carry
if sum[i+j+1] != 0 {
bit += byte(sum[i+j+1] - '0') // supplements last i+j+1 bit number
}
sum[i+j+1] = bit%10 + '0' // compute new i+j+1 bit number
carry = bit / 10 // compute next carry bit
}
// carry bit
if carry != 0 {
sum[i] = carry + '0'
}
}
// get the legal highest bit
for i := 0; i < len(sum); i++ {
if sum[i] != 0 && sum[i]-'0' > 0 {
return string(sum[i:])
}
}
return "0"
}