I need to find the number of digits of very large multiplications (about 300 digits each). I was wondering if there is a trick to predict the number of digits that the product will be without actually performing the calculation.
-
It's generally about 2*n, where n is the number of digits. – cristobalito Jul 03 '11 at 23:27
-
1You can bound the number of digits as follows: `floor(log x)*floor(log y) <= digits(x*y) <= ceil(log x)*ceil(log y)` log base 10. – davin Jul 03 '11 at 23:30
-
@critobalito it more n+m where n and m are the number of digits of each expression. e.g. `9*9=81` `999*9=8991` – Lynch Jul 03 '11 at 23:34
-
2@davin - isn't the number of digits just `floor(log(x) + log(y)) + 1`, for x, y positive? – Will A Jul 03 '11 at 23:35
-
it's not log you need - it's log10. see my answer – Bohemian Jul 03 '11 at 23:56
2 Answers
The number of digits can be calculated exactly by the rounded (down) sum of the base 10 log of the two multiplicands plus 1, as follows:
public static void main(String[] args) {
DecimalFormat f = new DecimalFormat("#");
double num1 = 12345678901234567890d;
double num2 = 314159265358979d;
// Here's the line that does the work:
int numberOfDigits = (int) (Math.log10(num1) + Math.log10(num2)) + 1;
System.out.println(f.format(num1) + " * " + f.format(num2) + " = " +
f.format((num1 * num2)) + ", which has " + numberOfDigits + " digits");
}
Output:
12345678901234567000 * 314159265358979 = 3878509413969699000000000000000000, which has 34 digits
This will work for arbitrarily large numbers.

- 412,405
- 93
- 575
- 722
-
-
thank you so much everyone for your responses, but this one takes the cake. thanks. – Descartes Jul 03 '11 at 23:53
-
2Of course, it is only `log10` if we want the number of *decimal digits*. More generally, it is `log_k` if we want digits in a base-k positional system. – Paŭlo Ebermann Jul 04 '11 at 00:26
-
Mathematical explanation: http://math.stackexchange.com/questions/857424/estimate-or-calculate-the-number-of-digits-of-a-multiplication – GitaarLAB Mar 03 '16 at 03:03
Cristobalito's answer pretty much gets it. Let me make the "about" more precise:
Suppose the first number has n digits, and the second has m. The lowest they could be is 10^(n-1) and 10^(m-1) respectively. That product would the lowest it could be, and would be 10^(m+n-2), which is m+n-1 digits.
The highest they could be is 10^n - 1 and 10^m - 1 respectively. That product would be the highest it could be, and would be 10^(n+m) - 10^n - 10^m + 1, which has at most m+n digits.
Thus if you are multiplying an n-digit number by an m-digit number, the product will have either m+n-1 or m+n digits.
Similar logic holds for other bases, such as base 2.

- 1,194
- 7
- 13
-
The base 10 logarithm, which other posters describe, is a simple technique. However, you could just find the base 2 logarithm and multiply by (log 2)/(log 10), which is about 0.693. The base 2 logarithm can be found without resorting to floating point by just finding the position of the most significant 1 in the binary representation. If you then multiply by 69 and integer divide by 100, you should find the approximate digit count without ever using anything but integer operations. You probably shouldn't ever do this, as it'd probably never actually be worth the trouble. Cute, though, no? – Domingo Ignacio Jul 03 '11 at 23:44
-
-
1Because I believe it's unlikely to be genuinely useful in practice. – Domingo Ignacio Jul 04 '11 at 00:26
-
I think this answer does not make sense given Bohemians answer because you can compute it exactly, e,g, "Thus if you are multiplying an n-digit number by an m-digit number, the product will have either m+n-1 or m+n digits." is a loose bound but you can do better. – Tommy Mar 01 '16 at 21:23