Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

69% Positive

Analyzed from 2719 words in the discussion.

Trending Topics

#multiplication#numbers#algorithm#number#base#more#add#digit#digits#bit

Discussion (74 Comments)Read Original on HackerNews

JoelJacobsonabout 4 hours ago
Back in 2024, I was trying to optimize PostgreSQL's NUMERIC data type, which is base-10000, using Karatsuba. The problem of finding the optimal threshold of when to switch to Karatsuba turned out to be really hard, since it depends on the size of both factors combined. After some hundreds of hours, I gave up, and started thinking about if there could be a simpler solution. I came to think about another idea I'd had before but abandoned, about 64-bit modernizing the digit base from 10k to 100M, but that would be a challenge due to existing data on disk. Desperate of finding a solution, I wondered if it could be fast enough to do on-the-fly conversion back and forth between base-10k and base-100M, and then realized that, yes, of course, it will be fast already for quite small N (testing shows already between 3-6 base digits). The trick basically reduced the N in O(N^2) into half, i.e. O((N/2)^2), with some O(2*N) cost for the conversion back and forth.

I had a lot of fun hacking on this idea together with the maintainer of the NUMERIC data type, and after two months the patch finally was ready and got committed:

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit...

wormslayer666about 2 hours ago
A bit tangential, but the folks behind the GNU Multiple Precision Library (GMPLib) have the problem of choosing algorithms more or less fleshed out. They've got some fairly approachable manual pages[1] for the various algorithms they use as operand sizes scale up, where Karatsuba is only the second of six options in terms of operational complexity.

[1] https://gmplib.org/manual/Multiplication-Algorithms

kurlberg19 minutes ago
It's complicated. :-)

There is a nice picture of the "best" choice for different ranges of sizes of numbers to be multiplied at http://gmplib.org/devel/log.i7.1024.png

More context and explanation can be found at: http://gmplib.org/devel/

JoelJacobsonabout 4 hours ago
Here is the full pgsql-hackers mailing list thread where you can follow our work from initial idea to commit: https://www.postgresql.org/message-id/flat/9d8a4a42-c354-41f...
sureglymopabout 2 hours ago
Love this, thank you for providing the context!
jmalickiabout 6 hours ago
I have actually had a ton of success using Strassen matrix multiplication kernels with extra structure in custom CUDA kernels (e.g. a covariance matrix is symmetric positive definite, or can be represented with Cholesky, and that comes up in a ton of useful computation). It's been a couple of years, but IIRC I would find it would start to win over the standard kernels at ~n>2500 or something (and in addition to Strassen was also exploiting the explicit structural constraints of the matrix, so not a completely fair comparison).
TimorousBestieabout 6 hours ago
If you’re interested, I found https://arxiv.org/abs/2505.09814v1 to beat Strassen for medium-sized and larger covariance matrices. YMMV of course. Takes a little adjustment for XX^H but it’s not so bad.
jmalickiabout 5 hours ago
Thanks!
nobrainsabout 1 hour ago
Why do we make computers multiply single digit numbers, instead of taking the result from a lookup table, like humans do? To answer my own question, I am assuming it would be because multiplying would still be faster than reading from a lookup table? Any ideas?
sparky_zabout 1 hour ago
In a sense, they do exactly that! But since there are only two single-digit numbers in binary, it makes for a pretty short table.
less_lessabout 1 hour ago
Hardware multipliers often use a sort of base-4-ish lookup table trick as well, using the Booth-Wallace algorithm. Booth's idea is to rewrite one of the inputs in base (usually) "4", except that the digits go from -2 to +2 instead of 0 to 3. (That's five possible digits! This helps the rewriting stage not have to propagate carries. Carry propagation is very expensive.) You can use Booth in a base higher than 4, especially if you know one of the multiplicands before the other, but you run into tradeoffs pretty quickly.

Then for each digit, you select between the other input multiplied by 0 (all zeros), +1 (identity), +2 (shift left by one bit), or -1 or -2 (flip all the bits of +1 or +2, plus a correction). Since a number has about half as many digits in base 4 as in base 2, you have about half as many digits to sum as if you'd done this in base 2.

Then you sum up all those results, but since carry propagation is expensive, you mostly use "compressors", e.g. you sum up three intermediates at a time, but you do it bit-by-bit, where three 1-bit numbers add up to a 2-bit number (from 0 to 3). This is called a Wallace Tree. The point is that you are generating carries, but you aren't propagating them, just adding them back into the set of things to be summed.

At the end of the tree step, you have just two numbers left, and you add them conventionally. That's the only step that needs full carry propagation.

If you are implementing a multiply-add, or multiplying several numbers and adding up all the results or similar, then you usually only need one full carry propagation stage.

The overall circuit has quadratic area but only a logarithmic depth in gates. IIRC whether to do Booth or not is a tradeoff: at least in some circumstances the rewrite steps make it slower but smaller. Hardware tool vendors have done a lot of work to tune these circuits very tightly, using e.g. specialized gates like AOI, heuristics for how to set up the tree, etc.

SkiFire13about 1 hour ago
> Booth's idea is to rewrite one of the inputs in base (usually) "4", except that the digits go from -2 to +2 instead of 0 to 3.

That's base 5 then. It needs to go from -2 to +1 if you want base 4

pgreenwoodabout 6 hours ago
I don't think the article did a great job with their two digit illustration. They simply state:

(ad + bc) = ((a + b) × (c + d)) – ac – bd.

First note this equation is more clearly be written as:

ad + bc = (a + b)(c + d) – ac – bd.

To see why this is so first expand (a + b)(c + d).

(a + b)(c + d) = ac + ad + bc + bd

now

(a + b)(c + d) − ac − bd = ac + ad + bc + bd − ac − bd

Hence

ad + bc = (a + b)(c + d) – ac – bd.

jordighabout 6 hours ago
Erm, I'm not sure you clarified anything other than removing one pair of spurious round brackets that who knows why they're there in the source material.

There are other weird formatting things in this article, which I blame on AI. I don't think the whole article was written by AI, but the copy-editing and formatting looks like an AI messed up things, such as those pointless round brackets or the inconsistency of multiplication (sometimes there is a × sign, sometimes there isn't), or this:

> have suspected that O(n²) was an inherent speed limit for multiplication. The celebrated Soviet math professor Andrey Kolmogorov posed the O(n^2)

The AI can't decide on notation.

estetlinusabout 3 hours ago
The AI is just like me, then.
qingcharlesabout 7 hours ago
Amazed I hadn't heard of this before. Would be interesting to see if they can prove that they have discovered the fastest at O(n × log n) or whether there is more still to come.
pfdietzabout 6 hours ago
Lower bounds are really hard.
6510about 1 hour ago
If you do it in binary you only need addition.

12 × 34 = 0xC x 0x22 = 1100 x 100010

Only two 1's!

1100 add 5 zeroes + 1100 add one zero = 110011000 = 408

ta-daa!

bombelaabout 7 hours ago
Does the article just end after describing the problem for me only? I am left wanting for more.
kadobanabout 7 hours ago
It's an open question in mathematics/CS, that's all we know. If you want to know more, get to mathing :)
wolfi1about 5 hours ago
if you want a little more in depth explanation of the whole multiplication thing Ican recommend TAOCP volume 2. it has a section called 'How fast can we multiply?' it should provide more insight. there is a paper from djb (Daniel J Bernstein),which I can also recommemd: https://cr.yp.to/lineartime/multapps-20080515.pdf
Anduiaabout 4 hours ago
Scroll down, there is a huge ad but the article continues.
woggyabout 6 hours ago
Ask Sol 5.6 for the answer
ChocMontePyabout 4 hours ago
avmichabout 4 hours ago
We can likely use different number representations for faster results. E.g. numbers in the form of coefficients to prime factors can be multipled at O(n) time, right?
zeroonetwothreeabout 4 hours ago
True but addition becomes a lot less efficient in this representation :)
WCSTombsabout 2 hours ago
To make both addition and multiplication O(n), you can store numbers as their residues modulo a bunch of different primes and appeal to the Chinese Remainder Theorem. However, then size comparison becomes difficult.
less_less44 minutes ago
Residue number systems are really neat! They're sometimes used in crypto implementations, but there you're doing modular multiplication and in most cases the modular reduction then becomes costly, so it's not a free lunch. (Except in RSA and a few other cases. RSA-CRT gets you a "free" ~4x performance boost except it's more brittle to mistakes and side-channel / fault attacks.)

There's also NTT / Fourier multiplication as an option, for big integers or polynomials or modular arithmetic.

necovekabout 4 hours ago
You mean like those guaranteed-always-compresses-by-at-least-one-bit algorithm patents gzip page made fun of?

In your case, doing prime factoring is where the cost would be, wouldn't it?

ccleveabout 6 hours ago
Ok, maybe I don't understand the problem, but it seems obvious that it should never be greater than O(min(n1, n2) * 10), where n1 and n2 are the lengths in digits of each argument, and assuming we are multiplying decimal numbers.

Take the first digit of the longer number. Multiply it by the shorter number and store the result. Take the second digit of the longer number. If it matches the first digit, do a lookup of the last result and use that, else multiply and store. Repeat.

There will be a maximum of 10 * (length of the shorter number) multiplies, because there are only 10 unique digits. After that every operation is a lookup.

You could even do a tiny optimization by skipping the multiplication for the zero digit.

Worst case, the two numbers are the same length, in which case it's O(n/2 * 10), which is a heck of a lot better than O(n log n).

What am I missing here?

EDIT to respond to the comments: in the article, they are only counting the number of multiplies in the O() value. They are not including the adds.

svatabout 4 hours ago
By your reasoning, multiplying two numbers in binary involves no multiplication at all, because multiplying by 1 and multiplying by 0 are both trivial operations.

But obviously multiplying two n-bit binary numbers is not done in O(1) time, so "only counting the number of multiplies" is not a meaningful model, and not the model adopted by the researchers quoted in the article.

inkysigmaabout 3 hours ago
The final time complexity for Karatsubas does account for the addition via the master theorem and gives you something less than n^2. It’s just in some sense the recursion is dominant so we add to the exponent. As a result, I think the article is just talking about counting multiplications since it’s in some sense the “expensive” operation in the both the recursive karatsuba and the regular grade school method.
SkiFire13about 2 hours ago
> EDIT to respond to the comments: in the article, they are only counting the number of multiplies in the O() value. They are not including the adds.

This simplification relies on the fact that after making a multiplication the cost of merging it with the result of another is always less than the cost of performing the multiplication, so it doesn't change the overall complexity.

This is not true in your proposed algorithm: a lookup is O(1), but merging is O(N), so you cannot do the same simplification and have to count the complexity of performing adds as well.

pfdietzabout 6 hours ago
Adding up those n1 numbers, each at least n2 digits long, takes O(n1 * n2) time.
mgaunardabout 5 hours ago
You're missing the fact that "multiplying a digit with a number" is not a single operation.
ccleveabout 5 hours ago
No, that's taken into account.
evandijk70about 5 hours ago
Not an expert, but I think the algorithm works for any base, not just 10. The python implementation uses base 2^30 for their multiplication. Base 10 is just a convenient illustration

The lookup table would not work for that case

pdpiabout 5 hours ago
You’re missing the O(1) space complexity.
floxyabout 5 hours ago

             1234567890 
           x     111111
           ------------
             1234567890
            12345678900
           123456789000
          1234567890000
         12345678900000
      + 123456789000000
      -----------------
    137,174,072,825,790 
...looks like O(n^2).
ccleveabout 5 hours ago
Once the longer number starts repeating digits, then it's not n^2 anymore. Multiplies get replaced with lookups. And we're only counting the multiplies. That's all they counted in the article. Not the adds, not the shifts.
CaptainNegativeabout 5 hours ago
They don't count the additions or shifts because they're both linear time operations, and thus provably at least as fast as multiplication (both in an asymptotic and exact sense). In any case where multiplication is super-linear, this means that addition and shifting are are not the temporal bottleneck at any stage in the algorithm where you have a constant number of those operations surrounding at least one multiplicative recursive call (on numbers of similar magnitudes).

If additions were truly free, an even easier optimal algorithm would just be repeated addition involving zero multiplications.

cvzabout 3 hours ago
The complexity of Karatsuba's algorithm, because it's a recursive one that gets "wider" at every level, is dominated by the width of that recursion. The top level always has a small number of operations, so we don't explicitly count them there, but the bottom has the truly huge number of operations that contribute to the algorithm's complexity, because each level of recursion dramatically increases the number of operations. Some number of those operations (most of them, in fact) will be single-digit additions.

Memoizing number-by-digit multiplication doesn't make multiplication O(1) because one must still do an N-digit addition (which is O(N)) for each digit.

chowellsabout 3 hours ago
The slowest multiplications are always when the two numbers are about the same size... That is to say, the case you're talking about doesn't happen.
aaronbwebberabout 6 hours ago
your algorithm for multiplication involves doing multiplication?
eruabout 5 hours ago
You might want to learn about recursion. It'll blow your mind.
hankbondabout 6 hours ago
Shout out to a cookie ToS modal on top of an email newsletter modal on top of the article. What a great way to make me immediately click back and leave the site.
Terr_about 5 hours ago
Shout-out to javascript disabled by default :)
derwikiabout 4 hours ago
And Safari Reader Mode!
Advertisement
Davidzhengabout 2 hours ago
The complexity is obviously nlogn - it's just hard to prove (this comment is only somewhat serious)
Davidzhengabout 2 hours ago
Maybe they should reduce sorting to multiplication lol
ErroneousBoshabout 1 hour ago
I don't know what age range "grade school" is, but I remember being taught that method when I was about 7 or 8, although it didn't really "land" properly until I read the short story "The Feeling of Power" by Isaac Asimov.

What I'm surprised to see left out here (unless I missed it in the page's horrible formatting) is a mention of the way that computers multiply two integers. They use a technique I saw described in a book when I was about 11 as the "Russian Farmer Method" (or something like that, it was in English and I might have misremembered it).

In that you shift the multiplier right and multiplicand left, halving one and doubling the other. If the multiplier is odd, add the multiplicand to the total.

It's really doing the same thing as "long multiplication" like you're taught in primary school but in binary so when you add a 0 to the right for the higher order digits you're doubling, not multiplying by ten. If you write code to do it you'd shift the multiplier first then consider whether or not to add by testing the Carry flag, or "Link bit" if like the author of the book I read you're demonstrating it on a PDP8 ;-)

But let's have a worked example, picking two numbers at random 205 * 707, use the smaller as the multiplier:

  205, 707   odd, add   707 to total
  102, 1414  even, disregard
  51,  2828  odd, add  2828 to the total
  25,  5656  odd, add  5656 to the total
  12,  11312 even, disregard
  6,   22624 even, disregard
  3,   45248 odd, add 45248 to the total
  1,   90496 odd, add 90496 to the total
  --------------------------------------
                     144935
If we're disregarding shifts and adds as completing in negligible time, well, this whole thing is just done with shifts and adds, and you can predict how many of them by identifying the leftmost bit set in the multiplier.
less_less26 minutes ago
Yeah, that shift-and-add algorithm is sometimes used on microcontrollers, either in software if there's no hardware multiplier, or in hardware if you want the bare minimum in acceleration at a tiny cost in area.

Adds are not really considered negligible; the article is just sloppy. (Some shifts might be negligible in some models because a fixed shift requires no logic gates.) The cost of the adds in Karatsuba is significant both theoretically and in practice, and determines the cutoff where Karatsuba is useful. But the exponent in O(n^(log_2 3)) is dominated by the recursive multiplications; the adds only affect the leading constant hidden in the O().

SkiFire1328 minutes ago
> If we're disregarding shifts and adds as completing in negligible time

When considering multiplication algorithms the parameter N is the number of digits of the two numbers. In that model adds do not complete in negligible time, and instead take O(N) time.

stfnonabout 3 hours ago
back in 2008 I had actually a ton of success using Strassen turboplicattion kernels with some extra custom CUDA lora (its more for Cholay) than anything else but it worked. Obviously I was forbidden to use it due to interest of shana.
MarcelOlszabout 5 hours ago
Terrence Howard already figured this out.
jdw64about 5 hours ago
I already know about fast multiplication algorithms, but it seems there's still no proof that a faster algorithm absolutely cannot exist. In other words, we don't know where the limit is yet.

If that gets proven, would programming multiplication algorithms become faster? I'm curious

TimTheTinkerabout 5 hours ago
The O(n log n) algorithm is galactic (only becomes more efficient when multiplying massive numbers)

So for numbers we normally work with, no. Maybe with cryptographic operations though.

groundzeros2015about 4 hours ago
Matrix multiplication is constantly getting improved but these methods aren’t improvements on practical implementation.
charcircuitabout 4 hours ago
How is it measured? A lookup table takes 1 step to find the answer of a multiplication.
less_less19 minutes ago
The model usually measures in terms of fixed-size operations, e.g. 2-input binary gates. There's some variation in how to count memory lookups, but even in models where accessing a large memory counts as only one step, any tables present in the code still have to be fixed-size (except in models like P/poly, but even then they can't be exponential size).
bmachoabout 3 hours ago
This would be the solution for any problem/algorithm, wouldn't it?

Factorize big numbers, sort an array, beat stockfish at chess, create a SOTA microkernel OS from English description. All O(1) with lookup table!

It's not how complexity works.

charcircuitabout 1 hour ago
>This would be the solution for any problem/algorithm, wouldn't it?

Yes, but it suffers from a large amount of space complexity, and probably would have high constant factors in practice.

bmachoabout 1 hour ago
Exactly.

So what I wrote debunks your assumptions and proves your argument wrong.

This is how conversations normally go.

hcsabout 2 hours ago
An algorithm is a finite sequence of instructions, and so can't include an infinite table. More generally, https://en.wikipedia.org/wiki/Effective_method
charcircuitabout 1 hour ago
I disagree. 1 step is a finite sequence of instructions.
SkiFire1322 minutes ago
The lookup table is part of the algorithm, and is not finite.

In general any problem can be solved in 1 step with a lookup table, so here you go P=NP solved.

blovescoffeeabout 4 hours ago
because you're looking up a result not solving for it
charcircuitabout 4 hours ago
You solve it by looking it up.
NetMageSCWabout 3 hours ago
Paywall.
monster_truckabout 2 hours ago
On some level I feel like we aren't even really trying. This stuff is so damn dry, though.

(warning, I refuse to like math and address it on my own terms, proceed further at your own peril)

Started looking into exact integer matrix multiplication, wanted to use it for some differential bullshit to find whatever they call the magic numbers that simplify a lot of complicated work into virtually no work for suspension/drivetrain/grip simulations at scale

To my surprise rocm didn't even usefully accelerate it! I said there is no fuckin way a 7900XTX is only good for 0.5 TOPS when working with 64 bit integers. I knew RNS/CRT/GEMM was a thing which led me to this https://github.com/RIKEN-RCCS/GEMMul8. Nothing pisses me off more than CUDA having something ROCm doesn't. So I told the models to try and fill the moat in with concrete. Think I got up to almost 3 TOPS before I stopped, and there are some pretty absurd wins for int32/other shapes.

Here's the slop https://github.com/doublemover/RNS8, I haven't cleaned it up or anything.

Life has gotten in the way so I had to set it down, and fighting the air conditioning when its "95 feels like 107" and the sky is filled with smoke is... not cool. I will finish it after summer. The HotAisle guy is a legend and hooked it up with some credits so I will be able to do the same for CDNA3, it at least compiles and runs but it has not been optimized/tested much yet.

Started with ChatGPT 5.5 but it sucked. I'm not paying $200/mo to play reset bingo while they figure out their bugs, especially without 20x. They lit my last $50 on fire in like 20 minutes with no remediation past "keep paying and you'll get more resets". Don't sleep on Deepseek, V4 Pro was responsible for the biggest leaps and it cost all of $15. It's genuinely great. The only way I'd go back to a closed model is if it was completely free. It will be fun to see how much better models are in a few months.

Kyurenabout 1 hour ago
If the 2019 algorithm is only useful for "galactic numbers" that's really neat because it will help a lot in the future as it seems like computation is only increasing in scale.
chmod775about 1 hour ago
If it ever had the slightest chance of being useful on Earth, it would not be called a "galactic algorithm". In this case that algorithm might be more than galactic: there isn't enough matter in the observable universe to build a conventional computer able to execute it.

In fact a computer executing it would be so large that speed of light would be the main constraint limiting execution speed, not theoretical algorithmic complexity that ignores data locality.

cwilluabout 1 hour ago
2 ^ (713 739 807 325 663 489 766 475 852 620 783 120 641) digits. Nope, an implementation of that algorithm will never be directly useful.