Let’s assume that PI is multiplied by 105010^{50} and e is multiplied by 105010^{50} 103010^{30}1030 has a value of A2), calculates a1xa2, and the values of A1 /a2

  • To analyze, multiply two numbers x and y, that is (the ones place of y times x) + (the tens place of y times x) times 10 +… Plus the highest bit of y times x times the sum of the highest bits

  • For example, take 1035×287 as an example, that is, 7×1035 + 8x1035x10 + 2x1035x100, that is, the deformation of the distributive property of multiplication,1035x(7+80+200)=1035×287

  • // If two numbers are multiplied, the length of the product will not exceed the sum of the lengths of the two numbers, for example, 9999×9999=99980001

  • A core point, determine how many pits there are in a good result, and figure out how to design the algorithm to write the right content into the pits


func main(a){
	str1 := "314159265358979323846264338327950288419716939937510"
	str2 :=                     "2718281828459045235360287471352"
	rs := multiply(str1,str2)
	fmt.Println(rs)
}


func multiply(str1, str2 string) string {
	l1, l2 := len(str1), len(str2)
	l := l1 + l2
	num := make([]int, l)

	for i := 0; i < l1; {

		for j := 0; j < l2; {
			num[l-i-j- 1] + =int(str1[l1- 1-i]-'0') * int(str2[l2- 1-j]-'0')
			j = j + 1
		}

		i = i + 1
	}
	// The top part of the loop is the core, and the bottom part is the same as the large number


	// Carry from right to left
	for n := l; n > 1; {
		temp := num[n- 1]
		num[n- 1] = num[n- 1] % 10 // If 35/10, you get 5
		num[n2 -] += temp / 10    // If 35/10 is equal to 3, add 3 to the previous digit
		n = n - 1
	}

	// Summarize the string results
	res := ""

	for _, i := range num {
		s := strconv.Itoa(i)
		res += s
	}

	// Remove any possible 0 at the beginning
	if len(res) > 1 {
		res = strings.TrimLeft(res, "0")}return res
}


Copy the code

Running result:

 853973422267356706546355086954449319694770135199398255015274067614534738795213520

Copy the code

Poke here for more interesting introductory algorithm problems

Golang to achieve large number multiplication