Brute-Force (pen&paper) Large Integer Multiplication in Golang

kommradHomer
2 min readNov 30, 2019

--

In the 3rd part of my yet-to-be-named series of Golang implementations of well-known algorithms , I’m here with an algorithm of horrible run time complexity and Space complexity: an implementation of pen&paper multiplication of 2 Integers.

Just as pen&paper multiplication would do , multiplying every digit of number_1 , from right to left ,by number_2 and storing the digits for intermediate results as integers in slices . Thus, using a slice containing slices , a 2-dimensional array or a Matrix in other words. Every “slice i” is holding the digits for “10 to the power of i .

While implementing this solution , I got a little big more familiar with the slices in Go , the strconv library ,type conversion and types of integers. I’ve had to traverse the first slice called column , and init each slice, filled with zeros. Took me some time to figure this necessity out and get rid of the ugly looking hard-coded limits on my slices. 4 topics , not bad.

I’m pretty sure that this code calculates the result , as long as the system can allocate roughly (len(number_1) * len(number_2)) bytes of memory, since I’ve used uint8 (1 byte) for storing digits by powers. I’ve tested up to 2 1000-digit integers and cross-checked the result with the Big Number Calculator of calculator.net .

I apologize in advance , for the horrible looking index calculation on columns slice access. As usual , the code is below.

--

--

kommradHomer
kommradHomer

Written by kommradHomer

proud seeder of 146.5GB The.Lord.of.the.Rings.Trilogy.1080p.Extended.Complete.Bluray.DTS-HD-6.1.x264-Grym

No responses yet