Member-only story

Long multiplication problem, with Java examples

Pierre-Marie Poitevin
6 min readNov 9, 2021

--

Introduction

The long multiplication is a computational problem in which we want to multiply 2 very large integers, that potentially have hundreds or thousands of digits. This is a problem because such numbers cannot usually be represented as int in traditional computer languages, and we need to store them as strings and we cannot use ‘*’ to get the result of the multiplication.

There are sometimes libraries for such large integers like BigInteger in Java, but we are assuming that for this exercise we cannot use it.

This is useful to solve some Leetcode problems, such as https://leetcode.com/problems/multiply-strings/. Moreover large number operations are an interesting algorithmic field.

Here is the formal problem as describe in the leetcode problem:

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.public String multiply(String num1, String num2) {}

The grade school multiplication solution

Given that the way we learn to solve multiplication in elementary school can solve large sumber multiplications, I wonder why teachers don’t use large numbers to prevent students from cheating with calculators. Anyways, this is the way most people are taught to do multiplications:

   123
x 456
------
738
+ 615
+492…

--

--

Pierre-Marie Poitevin
Pierre-Marie Poitevin

No responses yet