Arithmetic::PaperAndPencil — karatsuba
POD
=encoding utf8
NAME
karatsuba.raku --computing a multiplication using Karatsuba's method
DESCRIPTION
This programme prints an HTML file to the standard output, with all steps of a multiplication using Karatsuba's method.
USAGE
raku karatsuba.raku --radix=10 --hardware-length=1 --n1=123 --n2=456
Parameters
n1, n2
The numbers for the multiplication. Mandatory. They are entered as strings because they may represent numbers in a radix higher than 10.
radix
The radix. Allowed values are 2 to 36. Default value is 10.
hardware-length
The number of digits which the simulated Arithmetic and Logic Unit can store in its arithmetic registers. Default is 1.
knuth
Boolean parameter allowing the choice between the Karatsuba's standard method and Knuth's variant. Default is False (= standard).
BUGS, ISSUES AND ACCEPTABLE BREAKS FROM REALITY
The program uses directory ../lib. This enables me to test the
program without installing the intermediate version of the module.
Yet, this is bad practise for a program installed in /usr/bin or
/usr/local/bin.
When using a radix other than 10, some lines may be confusing and, in some cases, inconsistant. For example, using hexadecimal, you may read:
4 2 a × b = E × A = 96 scaled by 10 and 10 1 1
a_1 × b_1 = E × A = 96 scaled by 10^4 and 10^2
Obviously, a1 and b1 (with subscripts) are symbols. Nearly as
obvious, E and A are hexadecimal values, as well as 96. But
10^4 and 10^2 are problematic. 10 means "the current radix",
or "the number written '16' in decimal and '10' in hexadecimal". At
the same time, 4 and 2 are decimal numbers, without any
conversion to the current radix.
When several of a_0, a_1, b_0 and b_1 are equal to 0 or 1,
the program uses the naive method. It gives a single reason, even if
there are several of them. For example, it does not print:
a_0 = 1 and b_0 = 0, therefore using the naive multiplication method.
When using Knuth's variant, it may happen that α or β is 0 or 1.
The program does not deal correctly with these special cases.
When α or β is 1, the multiplication α × β should be a
trivial multiplication, yet, the program executes a recursive call to
the multiplication routine, in which the product will be computed as a
direct multiplication, not a trivial one.
When α or β is 0, the result is ignored in the addition, yet the
program prints:
α × β = 0 × 3 = 0 scaled by 10^1,
with a comma at the end of the line, preparing for a phrase "added because..." or "subtracted because...", which will not come because the value is neither added nor subtracted. And anyhow, we do not care about scaling a value equal to zero (both before and after scaling).
COPYRIGHT and LICENCE
Copyright (C) 2026, Jean Forget, all rights reserved
This programme is published under the same conditions as Raku: the Artistic License version 2.0.
The text of the license is available in the file in this repository, or you can read them at: