Skip to content

Instantly share code, notes, and snippets.

@kommradHomer
Last active November 30, 2019 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kommradHomer/e101333fb311f119021cdaef7bcead04 to your computer and use it in GitHub Desktop.
Save kommradHomer/e101333fb311f119021cdaef7bcead04 to your computer and use it in GitHub Desktop.
brute-force-large-integer-multiplication
package main
import (
"fmt"
"strconv"
)
func main() {
num1 := "9879879879879879879879879879879879879879879879879879879879879879879879879879879875463473583567398798798798798798734526456367456745674999234582345898923845234523452345234523452989986001020012345678912398798798798798798798798798798798798798798798798798798798798798798798798798798798754634735835673987987987987987987345264563674567456749992345823458989238452345234523452345234529899860010200123456789123987987987987987987987987987987987987987987987987987987987987987987987987987987987546347358356739879879879879879873452645636745674567499923458234589892384523452345234523452345298998600102001234567891239879879879879879879879879879879879879879879879879879879879879879879879879879879875463473583567398798798798798798734526456367456745674999234582345898923845234523452345234523452989986001020012345678912398798798798798798798798798798798798798798798798798798798798798798798798798798798754634735835673987987987987987987345264563674567456749992345823458989238452345234523452345234529899860010200123456789123"
num2 := "9879879879879879879879879879879887987987987987987987987987345264563674567479879879879878798798798798798798754634735835673987998795674999234582345898923845234523452345234523452989986001020012345678912398798798798798798798798798798798798798798798798798798798798798798798798798798798754634735835673987987987987987987345264563674567456749992345823458989238452345234523452345234529899860010200123456789123987987987987987987987987987987987987987987987987987987987987987987987987987987987546347358356739879879879879879873452645636745674567499923458234589892384523452345234523452345298998600102001234567891239879879879879879879879879879879879879879879879879879879879879879879879879879879875463473583567398798798798798798734526456367456745674999234582345898923845234523452345234523452989986001020012345678912398798798798798798798798798798798798798798798798798798798798798798798798798798798754634735835673987987987987987987345264563674567456749992345823458989238452345234523452345234529899860010200123456789123"
//num1 := "1234567890123456789012345678901234567890"
//num2 := "1234567890123456789012345678901234567890"
result := multiplyStr(num1, num2)
fmt.Println("result:", result)
}
func multiplyStr(a string, b string) string {
columns := make([][]uint8, len(a)+len(b))
for i := 0; i < len(columns); i++ {
columns[i] = make([]uint8, len(a)+len(b))
}
for i := len(b) - 1; i > -1; i-- {
var tens uint8 = 0
for j := len(a) - 1; j > -1; j-- {
n1Int, _ := strconv.Atoi(string(a[j]))
n2Int, _ := strconv.Atoi(string(b[i]))
n1 := uint8(n1Int)
n2 := uint8(n2Int)
//fmt.Println("numbers ", n2, " ", n1)
t1 := n1*n2 + tens
tens = uint8(t1 / 10)
newDigit := uint8(t1 % 10)
fmt.Println("tens:", tens, "newDigit:", newDigit)
columns[len(a)-j-1+(len(b)-i-1)][(len(b) - i - 1)] = newDigit
}
columns[len(a)+(len(b)-i-1)][len(b)-i-1] = tens
}
resultStr := ""
var leftoverFromLastDigit uint = 0
for i := 0; i < len(columns); i++ {
var total uint = 0
for j := 0; j < len(columns[i]); j++ {
total += uint(columns[i][j])
}
//fmt.Println("total:", total, "\t", "leftoverFromLastDigit:", leftoverFromLastDigit)
total += leftoverFromLastDigit
leftoverFromLastDigit = uint(total / 10)
newDigit := strconv.Itoa(int(total) % 10)
fmt.Println("newDigit:", newDigit)
resultStr = newDigit + resultStr
}
return resultStr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment