The shifting ''n''th root algorithm is an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
for extracting the
''n''th root of a positive
real number
In mathematics, a real number is a number that can be used to measurement, measure a ''continuous'' one-dimensional quantity such as a distance, time, duration or temperature. Here, ''continuous'' means that values can have arbitrarily small var ...
which proceeds iteratively by shifting in ''n''
digits of the radicand, starting with the most significant, and produces one digit of the root on each iteration, in a manner similar to
long division
In arithmetic, long division is a standard division algorithm suitable for dividing multi-digit Hindu-Arabic numerals ( Positional notation) that is simple enough to perform by hand. It breaks down a division problem into a series of easier steps ...
.
Algorithm
Notation
Let ''B'' be the
base of the number system you are using, and ''n'' be the degree of the root to be extracted. Let
be the radicand processed thus far,
be the root extracted thus far, and
be the remainder. Let
be the next
digits of the radicand, and
be the next digit of the root. Let
be the new value of
for the next iteration,
be the new value of
for the next iteration, and
be the new value of
for the next iteration. These are all
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
s.
Invariants
At each iteration, the
invariant will hold. The invariant
will hold. Thus
is the largest integer less than or equal to the ''n''th root of
, and
is the remainder.
Initialization
The initial values of
, and
should be 0. The value of
for the first iteration should be the most significant aligned block of
digits of the radicand. An aligned block of
digits means a block of digits aligned so that the decimal point falls between blocks. For example, in 123.4 the most significant aligned block of two digits is 01, the next most significant is 23, and the third most significant is 40.
Main loop
On each iteration we shift in
digits of the radicand, so we have
and we produce one digit of the root, so we have
. The first invariant implies that
. We want to choose
so that the invariants described above hold. It turns out that there is always exactly one such choice, as will be proved below.
To summarize, on each iteration:
# Let
be the next aligned block of digits from the radicand
# Let
# Let
be the largest
such that
# Let
# Let
Now, note that
, so the condition
:
is equivalent to
:
and
:
is equivalent to
:
Thus, we do not actually need
, and since
and
,
or