An algorithm to compute integer ηth roots using subtractions

16
This article was downloaded by: [University of Central Florida] On: 13 October 2014, At: 07:32 Publisher: Taylor & Francis Informa Ltd Registered in England and Wales Registered Number: 1072954 Registered office: Mortimer House, 37-41 Mortimer Street, London W1T 3JH, UK International Journal of Computer Mathematics Publication details, including instructions for authors and subscription information: http://www.tandfonline.com/loi/gcom20 An algorithm to compute integer ηth roots using subtractions Jose Torres-Jimenez a , Nelson Rangel-Valdez a & Pedro Quiz- Ramos a a Information Technology Laboratory , CINVESTAV-Tamaulipas , Km. 5.5 Carretera Victoria-Soto La Marina, 87130, Victoria Tamps, Mexico Published online: 23 Mar 2011. To cite this article: Jose Torres-Jimenez , Nelson Rangel-Valdez & Pedro Quiz-Ramos (2011) An algorithm to compute integer ηth roots using subtractions, International Journal of Computer Mathematics, 88:8, 1629-1643, DOI: 10.1080/00207160.2010.528755 To link to this article: http://dx.doi.org/10.1080/00207160.2010.528755 PLEASE SCROLL DOWN FOR ARTICLE Taylor & Francis makes every effort to ensure the accuracy of all the information (the “Content”) contained in the publications on our platform. However, Taylor & Francis, our agents, and our licensors make no representations or warranties whatsoever as to the accuracy, completeness, or suitability for any purpose of the Content. Any opinions and views expressed in this publication are the opinions and views of the authors, and are not the views of or endorsed by Taylor & Francis. The accuracy of the Content should not be relied upon and should be independently verified with primary sources of information. Taylor and Francis shall not be liable for any losses, actions, claims, proceedings, demands, costs, expenses, damages, and other liabilities whatsoever or howsoever caused arising directly or indirectly in connection with, in relation to or arising out of the use of the Content. This article may be used for research, teaching, and private study purposes. Any substantial or systematic reproduction, redistribution, reselling, loan, sub-licensing, systematic supply, or distribution in any form to anyone is expressly forbidden. Terms & Conditions of access and use can be found at http://www.tandfonline.com/page/terms- and-conditions

Transcript of An algorithm to compute integer ηth roots using subtractions

Page 1: An algorithm to compute integer ηth roots using subtractions

This article was downloaded by: [University of Central Florida]On: 13 October 2014, At: 07:32Publisher: Taylor & FrancisInforma Ltd Registered in England and Wales Registered Number: 1072954 Registeredoffice: Mortimer House, 37-41 Mortimer Street, London W1T 3JH, UK

International Journal of ComputerMathematicsPublication details, including instructions for authors andsubscription information:http://www.tandfonline.com/loi/gcom20

An algorithm to compute integer ηthroots using subtractionsJose Torres-Jimenez a , Nelson Rangel-Valdez a & Pedro Quiz-Ramos aa Information Technology Laboratory , CINVESTAV-Tamaulipas ,Km. 5.5 Carretera Victoria-Soto La Marina, 87130, Victoria Tamps,MexicoPublished online: 23 Mar 2011.

To cite this article: Jose Torres-Jimenez , Nelson Rangel-Valdez & Pedro Quiz-Ramos (2011) Analgorithm to compute integer ηth roots using subtractions, International Journal of ComputerMathematics, 88:8, 1629-1643, DOI: 10.1080/00207160.2010.528755

To link to this article: http://dx.doi.org/10.1080/00207160.2010.528755

PLEASE SCROLL DOWN FOR ARTICLE

Taylor & Francis makes every effort to ensure the accuracy of all the information (the“Content”) contained in the publications on our platform. However, Taylor & Francis,our agents, and our licensors make no representations or warranties whatsoever as tothe accuracy, completeness, or suitability for any purpose of the Content. Any opinionsand views expressed in this publication are the opinions and views of the authors,and are not the views of or endorsed by Taylor & Francis. The accuracy of the Contentshould not be relied upon and should be independently verified with primary sourcesof information. Taylor and Francis shall not be liable for any losses, actions, claims,proceedings, demands, costs, expenses, damages, and other liabilities whatsoever orhowsoever caused arising directly or indirectly in connection with, in relation to or arisingout of the use of the Content.

This article may be used for research, teaching, and private study purposes. Anysubstantial or systematic reproduction, redistribution, reselling, loan, sub-licensing,systematic supply, or distribution in any form to anyone is expressly forbidden. Terms &Conditions of access and use can be found at http://www.tandfonline.com/page/terms-and-conditions

Page 2: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer MathematicsVol. 88, No. 8, May 2011, 1629–1643

An algorithm to compute integer ηth roots using subtractions

Jose Torres-Jimenez, Nelson Rangel-Valdez* and Pedro Quiz-Ramos

Information Technology Laboratory, CINVESTAV-Tamaulipas, Km. 5.5 Carretera Victoria-Soto LaMarina, 87130 Victoria Tamps, Mexico

(Received 30 June 2010; revised version received 14 September 2010; accepted 28 September 2010)

This work presents two algorithms based on a finite-difference method to calculate the integer ηth rooty of an integer number x. The algorithm ηRA uses only additions and subtractions to calculate y. Thealgorithm is extremely simple and takes time O(log2 x · η · η

√x) and space O(η) to calculate the ηth root.

The algorithm FηRA divides x in group of digits to calculate y; it has a temporal and spatial complexityof O(log3

2 x · (η − 1)/2 · log2 10) and O(η2), respectively. The set of operations of FηRA also includesmultiplications and divisions. The algorithms were compared theoretically and empirically against binarysearch and a numerical method that calculate y. Results show that ηRA has a good performance when thequotient (log2 x)/η is small, and FηRA presents a fair performance when η is small in comparison withlog2 x. An additional contribution is a short addition chain for the sequence {1η, 2η, . . . , yη} producedby ηRA.

Keywords: integer ηth root; algorithm; addition chain

2010 AMS Subject Classifications: 49M15; 49M25; 65D15; 65L12; 68Q25

1. Introduction

In general, the computation of � η√

x� (for η ≥ 2) can be done by many methods, for instance:(a) sequential search; (b) binary search (BS) [7]; (c) numerical methods [6]; (d) manual methods[9]; (e) Taylor series method; (f) continued fraction expansion; and (g) subtraction of integernumbers. Rolfe [9] presented the implementation of the long-hand square root algorithm in binaryarithmetic, using subtractions, additions and bit shifts. One of the most used methods to calculateηth roots is based on the Newton–Raphson (NR) method. This method can be described asfollows: the ηth root of a number x, is the number y such that y = x1/η; this value can beapproximated through the NR method solving the equation yη − x = 0. The method iterates theequation yi+1 = ((η − 1) · yi + x/y

η−1i )/η until an acceptable value for x is found. Fitch [6] and

Caviness [4] presented approaches based on NR to calculate the ηth root of a number method.Another well-known method to calculate ηth roots is based on BS; Higginbotham [7] reported amethod for calculating square roots based upon BS.

The motivation of this paper resides on the presentation of the ηRA, a general algorithm tocompute y = � η

√x� using as core basic operations additions and subtractions. The method uses

*Corresponding author. Email: [email protected]

ISSN 0020-7160 print/ISSN 1029-0265 online© 2011 Taylor & FrancisDOI: 10.1080/00207160.2010.528755http://www.informaworld.com

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 3: An algorithm to compute integer ηth roots using subtractions

1630 J. Torres-Jimenez et al.

sequential subtractions equivalent to subtract perfect ηth powers to calculate the ηth root. Animprovement of this algorithm is also presented; the set of operations in this improvement isincreased by a small set of multiplications and divisions, but the temporal complexity is reducedsignificantly.

Besides the methods for computing the integer ηth roots of a number x, an additional contribu-tion achieved was the short addition chain for the series {1η, 2η, . . . , yη}, when using the additionand subtraction operations defined by ηRA to calculate y = � η

√x�. The number of additions used

are O(η · y), which are less than the O(2 · η · (∑y

i=1 log2(i))) required when using the additionchain of each power iη (having into account that the addition chain for each power is at most2 · log2(i

η)). Moreover, while ηRA has an amortized cost of η additions per power iη, the use ofaddition chains means more than log2(i

η) additions per power, according to the literature [8].The rest of the paper is organized as follows: Section 2 presents algorithms to compute square

and ηth roots by additions and subtractions. The algorithm that computes � η√

x� (called the ηth rootalgorithm or just ηRA) does it through the computation of a subtrahend derived from consecutivefinite differences, and the temporal complexity of this algorithm is proportional to � η

√x�. Section 3

presents the algorithm FηRA, an improvement over ηRA, that divides x in groups of digits tocalculate � η

√x�. Section 4 compares the theoretical complexity analysis of ηRA, FηRA, the BS

and the numerical method. An experimental comparison among the same algorithms is presentedin Section 5. Finally, in Section 6, we present some concluding remarks.

2. Sequential search algorithm to compute the ηth integer root of an integer number

This section presents an algorithm that calculates the ηth root of a number x. This algorithm isbased on the subtraction of perfect powers of η. First we introduce the simple case of square root,then we generalize the algorithm to the case of any ηth root.

2.1 Square-root algorithm

A well-known and easy-to-prove fact of elementary arithmetic is that the sum of the first k oddnumbers is k2.

Algorithm 2.1 Algorithm to calculate the integer square root of a number.

1 SquareRoot(x)2 begin3 δ1 = 1;4 ρ = 0;5 while δ1 ≤ x do6 ρ = ρ + 1;7 x = x − δ1;8 δ1 = δ1 + 2;9 end10 return ρ;11 end

This property can be used to develop a method to calculate �√x�. The method subtracts perfectsquares from x. It starts by subtracting the first odd number, then iterates the subtraction ofsubsequent consecutive odd numbers from x while the value of x is greater than the next odd

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 4: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1631

Table 1. Example of the calculation of√

30.

30 − 1 = 2929 − 3 = 2626 − 5 = 2121 − 7 = 1414 − 9 = 5√

30 = 5

number to be subtracted. The total number of subtractions is �√x�. The complete code is givenin Algorithm 2.1.

Table 1 shows an example of the computation of⌊√

30⌋

using Algorithm 2.1.

The next subsection presents an improvement to Algorithm 2.1. This improvement consists inthe generalization of this algorithm such that any ηth root can be calculated using only additionsand subtractions.

2.2 The ηth-root algorithm

Algorithm 2.1 is equivalent to the subtraction of perfect squares from a number x using oddnumbers in order to calculate the square roots. If we subtract perfect ηth powers from x insteadof perfect squares, we will generalize this algorithm so that any ηth root can be computed. Thisnew generalized algorithm will be termed ηRA.

The derivation of the ηRA is based on finite differences. A finite difference is an expressionlike f (y + b) − f (y + a) that plays an important role in the numerical solution of differentialequations. In this paper, the backward finite-difference operator is used to calculate the ηth rootof a given number x.

The backward finite-difference operator is defined as �f (y) = f (y) − f (y − 1); this operatormeasures the difference of the function f (y) evaluated at two different consecutive points. Theith backward finite difference of a function f (y) is defined by Equation (1); when the function islike f (y) = yη, the ith backward finite difference is described by a polynomial of degree η − i.

�if (y) =i∑

k=0

(i

k

)(−1)kf (y − k). (1)

In a general sense, a finite difference represents the quantity that must be added to a functionf (y) to obtain the value of f (y + 1). Algorithm ηRA uses this property to calculate � η

√x� using

only additions. First, the algorithm uses the values of each finite difference �iyη, for 1 ≤ i ≤ η,evaluated at y = 0. Given that the constant η! is the ηth finite difference of yη, the first backwardfinite difference for (y + 1)η is obtained by adding η! to the (η − 1)th finite difference of yη. Then,the resulting value is added to the (η − 2)th finite difference of yη; this process continues untilthe first finite difference is achieved and updated. Adding the first finite difference of (y + 1)η toyη yields the ηth power of (y + 1). If �1(y + 1)η < x, then the ηRA algorithm subtracts fromx the value �1(y + 1)η; set y to (y + 1) and repeat the process; otherwise, the algorithm stopsand the number of subtractions made over x is the result for � η

√x�. The set of the polynomials

describing the backward finite differences will be referred in the rest of the paper as the vectorδ = {δ1, δ2, . . . , δη}, where δi = �ixη.

Table 2 shows an example of how to calculate the cubic powers using finite differences forthe numbers y = {0, 1, 2, 3, 4}. The numbers are shown in column one and their correspondingpowers in column two. The rest of the columns contain the values for the elements of the vectorδ. The element δi for a number y is computed as the sum of δi of y − 1 and δi+1 of y.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 5: An algorithm to compute integer ηth roots using subtractions

1632 J. Torres-Jimenez et al.

Table 2. Use of backward finite differences to calculate cubic powers of numbers y = {0, 1, 2, 3, 4}.

y y3 δ1 δ2 δ3

0 0 1 −6 61 1 + 0 = 1 0 + 1 = 1 6 + −6 = 0 62 7 + 1 = 8 6 + 1 = 7 6 + 0 = 6 63 19 + 8 = 27 12 + 7 = 19 6 + 6 = 12 64 37 + 27 = 64 18 + 19 = 37 6 + 12 = 18 6

Table 3. Evaluation of the δ at y = 0 for the first eight powers.

δ

η δ1 δ2 δ3 δ4 δ5 δ6 δ7 δ8

1 12 −1 23 1 −6 64 −1 14 −36 245 1 −30 150 −240 1206 −1 62 −540 1560 −1800 7207 1 −126 1806 −8400 16,800 −15,120 50408 −1 254 −5796 40,824 −126,000 191,520 −141,120 40,320

As it is shown in Table 2, consecutive perfect ηth powers can be obtained using only additions.Note that it only requires to know the finite differences of y3 at an initial point (in Table 2, thispoint is y = 0). Table 3 presents the vector δ evaluated at y = 0 for the values of η = {1, 2, . . . , 8}.

A brief inspection in the values presented in Table 3 reveals that the first and last finitedifferences values for a given η are (−1)η−1 and η! Also, we can observe that the absolutevalue of the ith finite difference for a given η can be obtained through the recursive expression(−1)η−i · i((−1)η−i−1 · δ

η−1i + (−1)η−i−2δ

η−i−1i−1 ) for any 1 < i < η (this resembles the definition

of triangle numbers [3,5]). Equation (2) formally defines the calculation of the absolute valuesfor the vector δ corresponding to a specific η; in this expression t (η, i) refers to δ

η

i , the ith finitedifference of f (y) = yη. The sign of δ

η

i can be obtained by multiplying δη

i by (−1)η−i .

t (η, i) =

⎧⎪⎨⎪⎩

1 if i = 1,

η! if i = η,

i(|t (η − 1, i)| + |t (η − 1, i − 1)|) otherwise.

(2)

The validity of Equation (2) can be proved by applying the identity k · (n+1k

) = (n + 1) ·((n+1k

) − (n

k

))to Equation (3). Equation (3) is the expansion of t (η, i) = i((−1)η−i−1 · t (η −

1, i) + (−1)η−i−2 · t (η − 1, i − 1)) in terms of Equation (1), having f (y) = yη evaluated aty = 0 (in this expression, the absolute value is calculated by multiplying the term by itsopposite sign)

i∑k=0

(i

k

)(−1)kkη = i · (−1)η−i−1 ·

i∑k=0

(i

k

)(−1)kkη−1

+ i · (−1)η−i−2 ·i−1∑k=0

(i − 1

k

)(−1)kkη−1. (3)

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 6: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1633

Table 4. An example of calculate the fourth root of the number444 using ηRA.

y x δ

0 444 {−1, 14, −3624}1 444 − 1 = 443 {1, 2, −12, 24}2 443 − 15 = 428 {15, 14, 12, 24}3 428 − 65 = 363 {65, 50, 36, 24}4 363 − 175 = 188 {175, 110, 60, 24}5 188 − 369 = −181 {369, 194, 84, 24}Notes: The number of iterations is 5, the integer root is 4. The vector δ oniteration y is calculated using the vector δ obtained in iteration i − 1.

To initialize the vector δ using Equation (2) or Equation (3) requires a time of O(η2). However,in the implementation, it is preferred to use Equation (2) because it only requires multiplicationsand additions, while Equation (3) uses ηth powers.

Given the methods to compute (y + 1)η using additions and to calculate the vector δ for y = 0,we can proceed to summarize the ηRA algorithm. The general process to calculate � η

√x� involves

the creation of a new vector δ′ on each iteration of the algorithm. The vector δ′ is created by theaddition from the right to the left of the elements of the actual vector δ, i.e. δi−1 = δi + δi−1, for1 ≤ i ≤ η − 1, keeping the resulting value from the last addition for the next step. The positionδη is the same for every new vector δ′. The subtraction of element δ′

1 from x on iteration y isequivalent to x − yη, i.e. the subtraction of perfect ηth powers. The algorithm will stop whenx − yη > 0. The ηth integer root of x will be the value of y − 1.

The pseudocode of ηRA is shown in Algorithm 2.2. This algorithm takes as input the numberx whose root is to be computed and the initial vector δ. In each iteration, the algorithm calculatesa new ηth power (starting from 0) using the vector δ and subtracting δ1 from x (see lines 5–8).The resulting integer root is increased by one (see line 9). The algorithm stops when the valueof x becomes smaller than δ1 in the last δ vector calculated. The integer ηth root is kept in thevariable y. The number of iterations performed by Algorithm 2.2 until x gets smaller than δ1 is� η√

x� + 1.

Algorithm 2.2 Simple algorithm to calculate the ηth root of a number.

1 ηRA(x, δ, η)2 begin3 y ← 0;4 while x ≥ 0 do5 for i ← η − 1 downto 1 do6 δi ← δi + δi+1;7 end8 x ← x − δ1;9 y ← y + 1;10 end11 return y;12 end

An example of how the algorithm works can be seen in Table 4. In this example, the algorithm isused to calculate the fourth root of the number 444. The initial vector δ is {−1, 14, −36, 24}. Eachrow of Table 4 is an iteration of the algorithm and presents the vector δ created in that iteration (seecolumn 1) and the subtraction of δ1 from x (see column 2). The number of subtractions neededto calculate � 4

√444� is 5.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 7: An algorithm to compute integer ηth roots using subtractions

1634 J. Torres-Jimenez et al.

In general, the approach presented in this section subtracts perfect powers of η from a givennumber x to calculate � η

√x�. It requires � η

√x� + 1 subtractions to reach the solution plus η ×

(� η√

x� + 1) additions to calculate the powers of η. The advantage of using this method is thatfor any person it is easy to compute integer ηth roots manually; moreover, this algorithm can beimplemented in devices with low computing capabilities.

The next section presents an optimized version of the algorithm to calculate the ηth root. Theimprovement consists in skipping some steps in the calculation of the ηth root.

3. Fast ηth root algorithm

The ηRA can be improved so that the number of steps required to calculate the ηth rootof a number x is reduced. The improved version of ηRA, which will be referred fromnow as fast ηthRA or FηRA, divides the number x in groups and calculates � η

√x� using

these groups.Let X = {x1, . . . , xt } be a division of the number x in groups, where x1 = x, xi > 0 and

xi = �(xi−1)/10η�, for all i > 1 (note that by definition the element xi ∈ X is a number of at mosti · η digits). The algorithm FηRA uses this definition of X to calculate � η

√x�. First, FηRA takes

the element xt ∈ X, where t is the maximum integer s.t. xt is not zero, and calculate its ηth integerroot using the algorithm ηRA with the vector δ initialized at y = 0. Let yt be the result returned byηRA, the FηRA will use this value to calculate the ηth integer root of xt−1. The process involvessetting the initial root of xt−1 to yt−1 = 10 · yt and using the algorithm ηRA to find the root of theresidue xt−1 − y

η

t−1; the vector δ will have to be initialized at y = 10 · yt . In general, the FηRAalgorithm calculates the ηth root of an element xi ∈ X by initializing the root at yi = 10 · yi+1

and searching for the correct root with the ηRA algorithm (the vector δ will have to be initializedat y = yi). When x ≤ 10η, the basic algorithm ηRA is used instead of FηRA.

To this moment, we got all the elements to construct the algorithm FηRA but the way toinitialize δ at a given value y (the vector δ was initialized always at y = 0 in ηRA). This problemcan be overcome with the help of Equation (1) for f (y) = yη, which describes the value of anelement δi ∈ δ through a polynomial Pi (y). Then, the vector δ can be initialized by evaluatingeach polynomial Pi (y) of its elements δi at a the desired specific value y.

Given that Equation (1) for f (y) = yη involves the expansion of binomial coefficients, thecoefficients of the polynomials Pi (v) for a given value η can also be calculated using the first η

rows of Pascal’s triangle. Let C be a matrix of size η × η where each element Ci,j contains thecoefficient of the term of degree j of polynomial Pi (y) describing the element δi ∈ δ. For purposeof clarity, the values of i in C will range from 1 to η (representing the ith finite difference) andthe values j will range from 0 to η − 1 (representing the different degrees that can be found in thepolynomials). The coefficients for the polynomial P1(y) correspond to the values of the row η ofthe Pascal triangle. The rest of the coefficients of Ci,j , for 1 < i ≤ η can be determined by the linearcombination defined by Equation (4). The use of Equation (4) avoids the evaluation of binomialsand multiple polynomials because the coefficients of the element δi can be determined usingthe coefficients of element δi−1. These coefficients results from the expansion of the binomialsinvolved in the polynomials of the elements of the vector δ:

Ci,j =

⎧⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎩

(−1)i+j

η − j

)if i = 1,

(−1)i+j

η−i+1∑c=j+1

(η − c

j

)|Ci−1,c| otherwise.

(4)

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 8: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1635

Table 5. Matrix C of coefficients for a value η = 4.

j

i 0 1 2 3

1 −1 4 −6 42 14 −24 12 03 −36 24 0 04 24 0 0 0

The values of the matrix C depend on the root η being calculated. If the number x is changed,the matrix remains the same. This fact can help to save time if the matrix is precalculated in anadequate data structure.

Table 5 shows the matrix C for η = 4. The ith row contains the coefficients of the polynomialPi (v). Column j contains the coefficients of the terms of degree j of each polynomial Pi (v). Ingeneral, the element Ci,j is the coefficient of the term of degree j in the element δi ∈ δ.

Algorithm 3.1 shows the pseudocode for FηRA. Before starting the FηRA algorithm, the methodGenerate C() shown in Algorithm 3.3 is called to compute the matrix C for the given ηth root.The FηRA algorithm takes as input the value η of the root to be calculated and the integer numberx subject of this operation. The variable group_size contains the value 10η (lines 3–6), whichrepresents the maximum size of a group formed by the algorithm. The variable ‘group’ initiallycontains the greatest power of group_size that is not greater than x (lines 7–11). The vector δ

is initialized to the initial solution y = 0 (lines 12 and 13). The first group of digits xnext to beanalysed is calculated in line 14. During each iteration of FηRA, the algorithm ηRA takes as inputthe values of xnext, δ and η and returns the value y� (line 16). The FηRA algorithm calculates theroot of the group of digits xi of iteration i in line 17, using the value y� returned by ηRA. The nextvalue (the residue xt−1 − y

η

t−1) is calculated in line 19. The solution and the vector δ for the nextgroup of digits are initialized to y = 10 · (y + y�) (lines 20 and 21). The FηRA algorithm dividesgroup by group_size on each iteration to get the next group of digits (line 18). The algorithm stopswhen ‘group’ becomes zero (line 15). The value of � η

√x� will be y/10.

Algorithm 3.1 Algorithm to compute the ηth root of a number.

1 FηRA(η, x)2 begin3 group_size = 10;4 for i = 1; i ≤ η; i = i + 1 do5 group_size = 10 ∗ group_size;6 end7 group = group_size;8 while group ≤ x do9 group = group ∗ group_size;10 end11 group = group/group_size;12 Initialize vector δ using Equation (2);13 y = 0;14 xnext = x/group;15 while group ≥ 1 do16 y� = ηRA(xnext, δ, η);17 y = y + y� − 1;18 group = group/group_size;

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 9: An algorithm to compute integer ηth roots using subtractions

1636 J. Torres-Jimenez et al.

19 xnext = group_size ∗ (δ1 + xnext);20 y = y ∗ 10;21 δ =getNew(y);22 end23 return y/10;24 end

An example of how the calculation of the 4√

1,000,000 using the algorithm FηRA is presented inTable 6. The first column of this table shows the iterations of the main loop of the FηRA algorithm.The size of the group and the digits analysed during each iteration are shown in columns 2 and3, respectively. Column 4 shows the initial solution of each iteration. The rest of the columnsshows the iterations performed by the ηRA algorithm. The starting vector δ for ηRA in iteration2 is δ = {102719, 10094, 684, 24}; this vector was calculated using the matrix C, in the functiongetNew(y) with the value y set to 30. The integer fourth root calculated for 1,000,000 was 31.

Algorithm 3.2 Algorithm to update the vector needed for the subtractions.

1 getNew(res)2 begin3 j = η;4 con = 1;5 while j > 1 do6 i = con;7 aux = C[j ][con];8 con = con + 1;9 while i < η do10 i = i + 1;11 aux = aux ∗ res;12 aux = aux + C[j ][i];13 end14 δ[η − j ] = aux;15 j = j − 1;16 end17 end

Table 6. Example of the computation of the 4√

1, 000, 000 using the algorithm FηRA.

ηRA

Iteration group x/group y y� xnext δ

1 10,000 100 0 0 100 {−1, 14, −36, 24}1 99 {1, 2, −12, 24}2 84 {15, 14, 12, 24}3 19 {65, 50, 36, 24}4 −156 {175, 110, 60, 24}

2 1 1,000,000 30 0 190,000 {102719, 10094, 684, 24}1 76,479 {113521, 10802, 708, 24}2 −48,576 {125055, 11534, 732, 24}

y = 31

Notes: The iterations of the main loop of the algorithm FηRA were 2. The results returned was 31.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 10: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1637

Algorithm 3.3 Algorithm to calculate matrix C.

1 GenerateC(η)2 begin3 Compute the pascal’s triangle in C;4 for i = η; i > 1; i = i − 1 do5 f irst = 1;6 for j = η − i + 1; j < η; j = j + 1 do7 if f irst then8 for k = j + 1; k < η + 1; k = k + 1 do9 C[i − 1][k] = C[i][j ] ∗ C[η − j ][k];10 end11 f irst = 0;12 end13 else14 for k = j + 1; k < η + 1; k = k + 1 do15 C[i − 1][k] = C[i − 1][k] + C[i][j ] ∗ C[η − j ][k];16 end17 end18 end19 end20 for i = 2; i < η + 1; i = i + 1 do21 for j = η − i + 2; j < η + 1; j = j + 2 do22 C[i][j ] = −1 ∗ C[i][j ];23 end24 end25 end

The next section presents the theoretical complexity analysis for the algorithms ηRA and FηRA.The analysis is compared with the NR method and the BS algorithm for finding ηth roots.

4. Theoretical complexity analysis

In this section, we discuss the theoretical complexity analysis for the algorithms ηRA and FηRAto compute � η

√x�, where x is an integer. The analysis is based on the bit cost model [1]. The set of

operations is formed only by additions, subtractions, multiplications and divisions. The numberof bit operations for each arithmetic operation is shown in Table 7. The algorithms considered for

Table 7. Table of costs for arithmetic operations.

Operation Cost

Addition O(n)

Subtraction O(n)

Multiplication O(n2)

Division O(n2)

ηth Power O(η · n2)

Note: The costs are expressed in the function of the numberof bits n of the numbers involved in the operations.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 11: An algorithm to compute integer ηth roots using subtractions

1638 J. Torres-Jimenez et al.

these operations are the schoolbook addition, subtraction, long multiplication and long divisionmethods. The calculus of an specific power η of a number x is performed using η multiplications.The algorithm FηRA is compared with two well-known fast strategies that calculates ηth roots ofa number x, the NR and the BS methods.

The algorithm ηRA substracts consecutive perfect powers yη (starting at 0) from the numberx until yη > x. The number of perfect ηth powers analysed by the algorithm is � η

√x� + 1. To

find each ηth power, the algorithm updates the vector δ using additions over its elements. Giventhat the size of the vector δ is η, the number of additions required to update it is η (this actioncorresponds to lines 5–7 inAlgorithm 2.2). Once the vector δ has been updated, the algorithm onlysubtracts from x the value δ1. Summarizing, the ηRA algorithm performs � η

√x� + 1 iterations to

compute the integer ηth root of a number x; during each iteration, the algorithm does η additions.Then, the temporal complexity of the ηRA algorithm is O(n · η · � η

√x�), where n is the number

of bits of the number x.The algorithm FηRA calculates the ηth root of x using groups of digits X = {x1, . . . , xt }.

Given that the algorithm FηRA takes the number x in decimal base, the total number of groups t

analysed by the algorithm is �log10η (x) . The most time consuming action performed by FηRAon each group xi is the computation of the ηth root of xi (via a call to ηRA) and the update ofvector δ. The algorithm ηRA requires to calculate only nine perfect ηth powers to calculate theηth root of xi . Then, during each iteration of FηRA, the complexity of ηRA is only O(n · η).The vector δ is updated by the function getNew() in Algorithm 3.1; there, each element δi ∈ δ

requires to evaluate a polynomial Pi (y) of degree i − 1. Using Horner’s method, a polynomialPi (y) can be evaluated in i multiplications and i additions. Taking this fact into account, thecomplexity of updating the vector δ will be O(n2 · (η · (η − 1)/2)), where n2 is the cost of themultiplication. Then, the temporal complexity of FηRA is O(n2 · (η · (η − 1)/2) · log10η (x)) orthe simplified expression O(n3 · (η − 1)/2 · log2 (10)). For a fixed value of η, the complexity ofFηRA is a polynomial in the number of bits n of the input number x, while the complexity of theηRA algorithm is exponential.

According with [2], the BS method does a BS between 1 and 2�log2 (x)/η�+1 to find y = � η√

x�.Then, the number of iterations performed by the algorithm are �n/η� + 1, where n = log2 (x).Given that the algorithm evaluates yη on each iteration to guide the search, the overall complexityof the algorithm is O(η · n2(n/η)) (or just O(n3)), where η · n2 is the cost of the ηth power andn/η the number of iterations of BS.

The numerical method of NR solves Equation (5) to find y = � η√

x�. This method has a quadraticconvergence. A quadratic convergence means that the number of bits found for the ηth root of anumber x is doubled on each iteration of the algorithm i.e. the algorithm finds y in log2 n iterations,where n is the number of bits of x. According to [2], in order to guarantee that NR has a quadraticconvergence, at least the first log2 (η) bits of y must be given. Then, the quadratic convergencefor NR can be assured by determining the first log2 (η) bits of y through BS. Given that the NRmethod requires log2 n iterations to compute y (the root of x), and that each iteration requires to

Table 8. Theoretical comparison between the temporal complexityof the NR algorithm, BS and FηRA to calculate ηth roots.

Algorithm Complexity

ηRA O(n · η · 2n/η)

FηRA O

(n3 · (η − 1)

2 · log2 10

)

BS O(n3)

NR O(η · n2 · log2 (n))

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 12: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1639

calculate the power yη−1, then the temporal complexity of NR is O(η · n2 · log2 (n)).

yk+1 = (η − 1) · yη

k + x

n · xη−1k

. (5)

Table 8 summarizes the temporal complexity of the FηRA, NR, BS and ηRA algorithms tocalculate ηth roots of the number x according to the bit cost model. The complexity is describedin terms of the number of bits of x, i.e. the value n.

Figure 1. Graphical comparison of theoretical temporal complexities for the algorithms NR, FηRA and BS for valuesof η = {3, 6, 20}. The temporal complexity is taken from Table 8.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 13: An algorithm to compute integer ηth roots using subtractions

1640 J. Torres-Jimenez et al.

Figure 1(a) and (b) graphically compares the temporal complexity of FηRA, NR and BS shownin Table 8, for small values of η = {3, 6}. The graphs relate the size of the numbers in bits n

(x-axis) with the number of bit operations required for that size (y-axis). In these graphs, it ispossible to see that for such small values of η, FηRA requires less bit operations than BS forincreasing values in the number of bits n. With respect to NR, the FηRA algorithm is better whenthe number of bits n are smaller than 60.

Figure 1(c) presents a comparison between the algorithms NR, FηRA, BS and ηRA for the valueof η = 20. The curves shown in this graph indicates that in spite of the exponential complexityof the algorithm ηRA, it requires less bit operations than NR, BS and FηRA. This is mainlydue to two reasons: first, for small values of n/η, ηRA improves the temporal complexity of theother algorithms when solving � η

√x�, and secondly, the algorithm ηRA only uses additions and

subtractions to calculate the η root.With respect to the spatial complexity,ηRA uses the vector δ of sizeη, which means a complexity

of O(n · η). The FηRA algorithm requires to store the matrix C that contains the polynomialsof the elements δi ∈ δ. The size of this matrix is η × η which means an spatial complexity ofO(n · η2), where n is the number of digits of the values stored in C. Compared with NR and BSwhich have an spatial complexity of O(n), ηRA FηRA require more space. However, given thatfor a specific value of η the vector δ or the matrix C is the same for any number x, the use of thealgorithms proposed in this paper can be motivated in cases where the value of η is small (whichcoincides in the theoretical range where FηRA is better than NR and BS) or when a given rootη must be obtained for great number of integers, because the cost of generating and saving thisstructures can be justified.

The next section presents an empirical comparison of the performance of FηRA whencalculating the ηth power of integer numbers against the NR and BS algorithms.

5. Experimental results

This section presents the experimental comparison of the performance of these algorithms tocalculate y = � η

√x�. The NR method was implemented in the ring of the integers, as proposed in

[6]. The BS algorithm was implemented following the definition presented in [2], which representsa generalization of the BS algorithm proposed in [7].

All the algorithms compared in the experiment were compiled in C language under the optimiza-tion flag −O3, using the version 4.3.1 of the GMP library (http://gmplib.org/) for manipulation ofbig numbers. The experiments were run in a computer with a Intel Pentium Dual-Core 2.16 GHzprocessor, 2.9 GB of RAM memory; the operating system was Ubuntu 9.04.

Figure 2 shows three graphs, each containing the performance of the FηRA, NR and BSalgorithms for increasing values in the number of bits n of the numbers analysed. Each graphplots the average number in total number of bit operations (y-axis) against the different valuesof η analysed, for each algorithm. These graphs shows that the performance of FηRA againstNR is decreasing with the increase in the number of bits n. This can be observed in the lastgraph (the one with n = 60) where the curve for FηRA is almost completely above of thecurve of NR.

A relationship between the performances of FηRA and BS can also be observed from the graphsin Figure 2. While the size of the numbers n increases and the value of η remains fixed, the gapbetween the performance of FηRA and BS grows. This means that the FηRA algorithm is betterthan BS when n is large with respect to the value of η.

Figure 3 graphically compares the algorithms ηRA, FηRA, NR and BS. This graph shows thenumber of bits operations (y-axis) spent by each algorithm according with different values of n

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 14: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1641

Figure 2. Graphical comparison of the empirical performance of the algorithms NR, FηRA and BS. Each graph corre-sponds to a different value of n = {40, 50, 60}, the size of the numbers in bits. There is a curve per algorithm analysed(FηRA, NR, BS) in each graph. Each point in the curves corresponds to the average bit cost of calculating the ηth root ofa set of 1000 random numbers. The values of the roots were η = {3, 4, . . . , 8}.

(the number of bits is shown in the x-axis). We can see in this graph that while the quotient n/η

is small, the algorithm ηRA requires less bit operations than FηRA, NR and BS to calculate aninteger ηth root.

Summarizing, the empirical performance of the algorithms ηRA and FηRA presented in thissection is in accordance with the theoretic performance shown in the last section.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 15: An algorithm to compute integer ηth roots using subtractions

1642 J. Torres-Jimenez et al.

Figure 3. Graphical comparison of the empirical performance of the algorithms NR, FηRA, BS and ηRAfor a big value of η = 20. Each point in the curves corresponds to the average bit cost spent by thealgorithm to calculate the 20th root of a set of five random integers numbers. The number of bits tested were{50, 75, 100, 125, 150, 175, 200}.

6. Conclusions

This paper presents the ηRA and FηRA algorithms to calculate y = � η√

x�. The algorithms arebased on a finite-difference method. The ηRA has as core operations additions and subtractions.The FηRA algorithm extends this set of operations to multiplications and subtractions.

In the bit cost model, and taking the number of bits n of the number x as the size of the input,the algorithm FηRA takes time O(n3 · (η − 1)/2 · log2 10) and space O(η2) to compute y. Thealgorithm ηRA has an exponential temporal complexity of O(n · η η

√x) and a spatial complexity

of O(η) for the same action.The ηRA and FηRA algorithms, along with BS and the numerical method of NR to calculate

ηth roots, are theoretically and experimentally compared. The FηRA algorithm performs betterthan NR for cases where the numbers have less than n = 60 bits and the root calculated is η = 6or smaller. In comparison with the BS method, the FηRA algorithm improves its performance forroot values of η = 6 or smaller when the number of bits n increases.

The theoretical and empirical information presented for the ηRA algorithm shows that whenthe quotient n/η is small, ηRA requires less bit operations than FηRA, NR and BS to compute y.These results come from the fact that the exponential complexity of ηRA depends on the value2n/η. Therefore, if the quotient n/η is kept in a low value, then ηRA will have a performancecompetitive with the polynomial behaviour of FηRA, NR or BS.

In general, the algorithms ηRA and FηRA only uses additions, subtractions, multiplicationsand divisions to calculate � η

√x�. This is possible with the precalculation of matrix C for the

FηRA algorithm or the vector δ in ηRA (structures that depends only on the value of η). Theset of simple operations represents an advantage for the algorithms proposed in this work, incomparison with other algorithms that are mainly based upon complex operations as powers, andlogarithms. These algorithms can be implemented in devices with low resources that involves asmall set of arithmetic operators such as FPGAs.

Finally, the same addition chain defined by the algorithm ηRA to calculate y = � η√

x� can beused to calculate the whole series {1n, 2n, . . . , yn}. This result implies that to compute all thepowers iη, where 1 ≤ i ≤ y, requires only η · y additions; these are considerably less than theworst case of 2 · η · (

∑y

i=1 log2(i)) additions resulting from computing each power iη throughone of its addition chains. In general, the ηRA algorithm can generate each power in the sequence{1η, 2η, . . . , yη} having an amortized cost of η additions per power iη.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4

Page 16: An algorithm to compute integer ηth roots using subtractions

International Journal of Computer Mathematics 1643

Acknowledgement

This research was partially funded by the following projects: CONACyT 58554-Cálculo de CoveringArrays, 51623-FondoMixto CONACyT y Gobierno del Estado de Tamaulipas.

References

[1] G. Ausiello, P. Crescenzi, G. Gambosi, V. Kann, A. Marchetti-Spaccamela, and M. Protasi, Complexity and Approx-imation: Combinatorial Optimization Problems and their Approximability Properties, Springer, Berlin, Heidelberg,1999.

[2] E. Bach and J. Sorenson, Sieve algorithms for perfect power testing, Algorithmica 9 (1993), pp. 313–328.[3] A.T. Benjamin and J.J. Quinn, Proofs that Really Count: The Art of Combinatorial Proof, The Mathematical

Association of America, Washington, DC, 2003.[4] B. Caviness, More on computing roots of integers, ACM SIGSAM Bull. 9 (1975), pp. 18–20.[5] O. Chandon, J. LeMaire, and J. Pouget, Denombrement des quasi-ordres sur un ensemble fini, Math. Sci. 62 (1978),

pp. 61–80.[6] J. Fitch, A simple method of taking nth roots of integers, ACM SIGSAM Bull. 8 (1974), pp. 26–26.[7] T. Higginbotham, The integer square root of n via a binary search, SIGCSE Bull. 25 (1993), pp. 41–47.[8] A. Matsuura and A. Nagoya, Formulation of the addition-shift-sequence problem and its complexity, in Algorithms

and Computation, Lecture Notes in Computer Science, Vol. 1350, H. Leong, H. Imai, and S. Jain, eds., Springer,Berlin/Heidelberg, 1997, pp. 42–51.

[9] T. Rolfe, On a fast integer square root algorithm, SIGNUM Newslett. 22 (1987), pp. 6–11.

Dow

nloa

ded

by [

Uni

vers

ity o

f C

entr

al F

lori

da]

at 0

7:32

13

Oct

ober

201

4