{"id":205,"date":"2026-11-13T12:34:31","date_gmt":"2026-11-13T04:34:31","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-karatsuba-multiplies-with-three-half-size-products\/"},"modified":"2026-11-13T21:52:43","modified_gmt":"2026-11-13T13:52:43","slug":"how-karatsuba-multiplies-with-three-half-size-products","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-karatsuba-multiplies-with-three-half-size-products\/","title":{"rendered":"How Karatsuba multiplies with three half-size products"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Karatsuba multiplication<\/strong> multiplies two n-digit numbers with three multiplications of n\/2 digits instead of four. Split x = x1\u00b7B + x0 and y = y1\u00b7B + y0. The classical cross terms x1\u00b7y0 and x0\u00b7y1 are recovered from the single product (x1+x0)(y1+y0) by subtracting the two squares x1\u00b7y1 and x0\u00b7y0. The recurrence is <strong>O(n^{log\u2082 3})<\/strong>, about <strong>O(n^1.585)<\/strong>. Below a cutoff, schoolbook multiplication is faster.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Schoolbook multiplication forms four products of the halves. The middle coefficient of the product is the sum of the two cross terms, and that sum can be computed without forming each cross term alone. The algebra costs a few additions, which are linear, and saves one multiplication, which is the expensive branch of the recurrence.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The split radix B is a power of the base, often a power of two so the shift is free. The sums x1+x0 can have one extra digit. The recursive call has to allow that extra digit or the implementation overflows the half-size buffer.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The product equals the schoolbook product. The depth of recursion is log n, and the work obeys T(n) = 3T(n\/2) + O(n), whose solution is \u0398(n^{log\u2082 3}). Extra memory is the temporary sums and the recursive buffers. A cutoff around a few dozen machine words, tuned by measurement, switches to schoolbook multiplication. Running the recursion down to one digit loses to the additions.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The algorithm is not asymptotically the fastest. FFT multiplication is O(n log n) up to further log factors and wins for very large n. Karatsuba is the algorithm in the middle range, and it is the one whose three-product identity is worth knowing.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>If n is below the cutoff, multiply by the schoolbook method and return.<\/li>\n<li>Split each input into high and low halves of n\/2 digits. Let B be the place value of the split.<\/li>\n<li>Recursively compute z2 = x1\u00b7y1, z0 = x0\u00b7y0, and z1 = (x1+x0)\u00b7(y1+y0).<\/li>\n<li>The middle coefficient is z1 \u2212 z2 \u2212 z0. It may be negative. The implementation has to allow a signed temporary.<\/li>\n<li>The product is z2\u00b7B\u00b2 + (z1 \u2212 z2 \u2212 z0)\u00b7B + z0.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The exponent log\u2082 3 assumes the three recursive calls are on half the size and the additions are linear. An FFT-based product is faster past a larger threshold. A cutoff that is never applied makes Karatsuba slower than schoolbook on small inputs even though the asymptotics say otherwise. The identity is for a product of two scalars or two polynomials. Matrix multiplication&#8217;s analogous saving is Strassen.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the cross terms are still formed separately<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Compute x1\u00b7y0 and x0\u00b7y1 by two more recursive calls<\/td>\n<td>That is schoolbook multiplication in recursive clothing, four half-size products, and the exponent returns to 2.<\/td>\n<\/tr>\n<tr>\n<td>Ignore the extra digit of x1+x0<\/td>\n<td>The sum can be B plus a half-size number. A half-size buffer drops the carry and the middle coefficient is wrong.<\/td>\n<\/tr>\n<tr>\n<td>Run the recursion to single digits on a machine word<\/td>\n<td>The linear additions dominate. A cutoff is part of a serious implementation.<\/td>\n<\/tr>\n<tr>\n<td>Quote O(n log n) for Karatsuba<\/td>\n<td>The closed form is n to the power log\u2082 3. The n log n multiplication is the FFT.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Schoolbook multiplication is the cutoff. FFT multiplication is the large-n algorithm. Strassen&#8217;s algorithm is the similar saving for matrices. Karatsuba is the three-product integer or polynomial multiply.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment multiplies with three half-size products. The middle product is z1 \u2212 z0 \u2212 z2. 1234 \u00d7 5678 equals the schoolbook product <code>7006652<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def karatsuba(x, y):\n    if x &lt; 10 or y &lt; 10:\n        return x * y\n    n = max(len(str(x)), len(str(y))) \/\/ 2\n    high = 10 ** n\n    a, b = divmod(x, high)\n    c, d = divmod(y, high)\n    z0 = karatsuba(b, d)\n    z2 = karatsuba(a, c)\n    z1 = karatsuba(a + b, c + d) - z2 - z0\n    return z2 * high * high + z1 * high + z0\n\nassert karatsuba(1234, 5678) == 1234 * 5678\nprint(karatsuba(1234, 5678))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>GMP<\/strong> multiplies medium-size integers with Karatsuba, then switches to Toom-Cook and to FFT multiplication as the operands grow. OpenSSL&#8217;s bignum code has the same ladder. Schoolbook multiplication is the base case below the Karatsuba cutoff.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Karatsuba below the measured cutoff and above the size where FFT multiplication wins. Keep it for the middle range of big-integer or polynomial products, and allow the extra digit in the sums.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Karatsuba returns the three half-size products and the exponent log base 2 of 3. <strong>Karatsuba multiplies two n-digit numbers with three recursive half-size products, in O(n^1.585) operations, by recovering the cross terms from a single extra product.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Karatsuba multiplication multiplies two n-digit numbers with three multiplications of n\/2 digits instead of four. Split x = x1\u00b7B + x0 and y = y1\u00b7B + y0. The classical cross terms x1\u00b7y0 and x0\u00b7y1 are recovered from the single product (x1+x0)(y1+y0) by subtracting the two squares x1\u00b7y1&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,56],"tags":[],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-arithmetic"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/205\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}