A Fast Algorithm for Determining the Existence and Value of Integer Roots of N
AA Fast Algorithm for Determining theExistence and Value of Integer Roots of N Vibeke Libby
Woodside, CA 94062
November 4, 2018
Abstract
We show that all perfect odd integer squares not divisible by 3, can beusefully written as √ N = a + 18 p , where the constant a is determined bythe basic properties of N . The equation can be solved deterministically by anefficient four step algorithm that is solely based on integer arithmetic. Thereis no required multiplication or division by multiple digit integers, nor doesthe algorithm need a seed value. It finds the integer p when N is a perfectsquare, and certifies N as a non-square when the algorithm terminates withouta solution. The number of iterations scales approximately as log ( √ N /
2) forsquare roots. The paper also outlines how one of the methods discussed forsquares can be extended to finding an arbitrary root of N . Finally, we presenta rule that distinguishes products of twin primes from squares. In cryptography and broader applications, such as the general factorization of in-tegers, it is often of interest to determine quickly if an integer N is a true squareor is a product of two closely spaced integers, such as twin primes. Numerous non-deterministic methods of varying efficiencies are currently applied to the problemof finding the square root of an integer. These methods include digit-by-digit cal-culations, Taylor series, Newton’s method ref [1] , Pell’s equation ref [2] , and even1 a r X i v : . [ m a t h . N T ] M a r ophisticated hardware implementations ref [3] . For very large integers, these meth-ods, may not yield rapid conclusive results. In contrast, the algorithm presented hereprovides a conclusive answer after a specific number of steps, determined by the sizeof the N . Throughout this presentation, all variables and computations are basedon positive integer arithmetic alone.The number of required iterations to find the square root of N is approximately log ( √ N / − /
2. The same number of iterations is required to certify that N is anon-square. Suppose the object is to find the square root of a general integer M . The presentedalgorithm works by focusing on N , the odd factor of M that is not divisible by 2 or3 to any power. M = 2 k ∗ l ∗ N (1)Clearly, if either k or l is odd, M is not a perfect square. If k and l are both even,then M is square if N is square. Since factors of 2 and 3 can quickly be identifiedin even large numbers, we can choose a representation for N which do not containthese divisors.One such representation is the reduced residue system U with the elements { , , , , , } or {± , ± , ± } modulo 18, where each element [ r ] is a residueclass in U . By definition, ref [4], all [ r ] are relatively prime to 18 from which itfollows that g.c.d. ( r,
18) = 1 and the congruence r ∗ N ≡ r ] in U are relatively prime to each other, whichis not generally true for reduced residue systems; but is true, for example, for systemsmodulo 3, 6, 8, 10, and 18.Since the integer 18 only contains the divisors 2 and 3, U contains no evennumbers and no odd numbers divisible by 3. Therefore, U is an ideal representationof all possible N -values because every N defined in equation 1 can be uniquely assigned to one of the six elements {
1, 5, 7, 11, 13, 17 } . According to Euler’stheorem, for ∀ r relatively prime to 18, r n ≡ mod N repeatedly until a number between one and nine results, often called the DigitRoot (DR). If DR is 3, 6, or 9 then N is divisible by three. This test is equivalent tocomputing N ( mod
9) where the latter calculation involves division by 9 as opposedto summation of digits. 2ere, we extend this test to cover N ( mod
18) replacing division by 18 with theaddition of the digits in N . We will show that a mapping exists between the elementsin U , N ( mod DR ( N ), N ( mod N ( mod mod
9) = N ( mod
9) (2)
P roof :For c <
18 write N(mod18) as N = c +18 X = c +9(2 X ) and for d < N = d + 9 Y . It follows that ( c + 9(2 X ))( mod
9) = c ( mod
9) and ( d + 9 Y )( mod
9) = d ( mod
9) = d . Therefore, the equality in equation 2 is satisfied when: c ( mod
9) = d (3)This condition is uniquely met for all N in U ; and therefore the DR methodcan be applied to the elements in U as long as it is recognized that we have to usec = 11 for d = 2, c = 13 for d = 4, and c = 17 for d = 8. For 1, 5, 7: c = d. Tofind which element an arbitrary N belongs to, we apply the condition in equation3. By doing so, we have substituted division by 18 with addition. For example, if N = 512346251, SOD(N) = 29 with DR(29) = 2, then N belongs to element 11,written as [11] or 11 + 18X. a for known N Writing any integer in U as a + 18 p , with a is uniquely determined by DR(N), anysquare in U can be written as: N = ( a + 18 p ) = a + 36 ap + 18 p (4)From this expression DR ( N ) equals DR ( a ) because the remaining terms containthe factor 18, which does not affect the DR . Since a is limited to the values 1, 5,7, 11, 13, 17, the squares in U are therefore found only in: [1], [7], [13]. Thissimplification comes with a computational cost for squares because two a -values arepossible for each element: • [1] for a = 1, 17 • [7] for a = 5, 13 • [13] for a = 7, 11 3(mod 18)=[ ] a [ a ] n [ a ] n [ a ] n [ a ] n [ a ] n [ a ] n The complete mod power multiplication table for U . Example: a = 5 to the th power belongs to residue class [13] because = 625 and N is thereforecongruent to mod . Conversely, N can’t be a power of n , and specificallya square, if N mod has a value other than 1, 7, or 13. Therefore, two parallel tests are needed for each square root calculation.As argued above for squares, the value of N ( mod
18) is required to determine thepossible values of a for any root. From the first, third, and fifth column in figure 1,all even powers belong to the residue classes [1], [7], [13]. Also note that six tests forthe constant a are required for powers of 6 n , three tests for powers of form 3 + 6 n ,two tests for powers of 2 + 6 n and 4 + 6 n , and only one test each for powers of 1 + 6 n and 5 + 6 n . All such tests are independent and can be performed in parallel. The algorithm is divided into three parts:1. Setup2. Initialization3. Loop
With N and a given rewrite condition 4 as: N − ap p (5)4ere N = N − a is guaranteed integer when ( DR ( N ) , a ) assumes any of the sixcombinations (1 , , (1 , , (7 , , (7 , , (13 , , (13 , N is square with a root of the form ( a + 18 p ) if there exists an integer solution for p satisfying condition 5. We will iteratively test for the existence of such p and for thispurpose define the unique representation of p as: p = i − max (cid:88) i =0 i ∗ b i +1 (6)with 1 ≤ b i +1 ≤ N − ap i ≤ p i (7)is met. When the equality sign is valid, then N is square; and when the reverse istrue a solution may still exist. In this case the iterations must continue until thetermination criterion is met. p i = b + 9 b + 9 b + ... + 9 i b i +1 = p i − + 9 i b i +1 (8) Set i = 0, b = 0, and p − = 0. Initialization N frac b p f i = 0 N − a N − a ∗ b b b ∗ p Figure 2:
Init shows the algorithm initial conditions, where b is determined fromthe condition that f rac be integer. It is useful to define the quantity f = b ∗ b = p ∗ b and call the left hand sideof equation 7 f rac . Using equation 7 and the initial values from figure 2 condition 5 becomes an evalu-ation of: N − ab b ∗ b (9)5e know from the unique representation of p that there exists one and only one b value for which f rac is integer and that b must satisfy DR ( N ) = DR ( a ∗ b ). Thiscondition can be implemented in a nine-entry look-up table. With b now known,condition 7 can be evaluated. The evaluation terminates when f rac < f , continuesfor f rac > f , and declares a solution for f rac = f . In the next step p = b + 9 b . Inserting p and p into condition 5 yields: f rac − f − ab b (2 b + 9 b ) (10)Setting f rac − f = N and realizing 2 b + 9 b = p + b + 9 b yields a conditionsimilar to 9; but now with b as the unknown quantity: N − ab b ( p + p ) (11)As before, b is found from the integer condition of the left hand side, f rac . As i increases, the number of terms on the right hand side, f i , increases as well. However,since each f i can be expressed in terms of the previous and current value of p i , thereexists a general recursion relation for f i as shown in figure 3. ≥ i >
0, of N i as: N i = N i − − a ∗ b i − i ∗ b i − ∗ b i ∗ (cid:80) k = i − k =0 k ∗ b k N i − a ∗ b i +1 i ∗ b i +1 + 2 ∗ b i +1 ∗ k = i (cid:88) k =1 k − ∗ b k (13)Using the definition of p i , equation 8, the right hand side of equation 13 becomes: f i = b i +1 ∗ (9 i ∗ b i +1 + 2 ∗ p i − ) = b i +1 ∗ ( p i + p i − ) (14)The algorithm logic flow diagram is shown in figure 4.6 N i frac i b i p i f i i > f rac i − − f i − N i − a ∗ b i +1 b i +1 p i − + 9 i ∗ b i +1 b i +1 ∗ ( p i − + p i )Figure 3: Illustration of the general recursion relation for all the variables as afunction of i . Reading the tables from left to right gives the order of computation.The quantity b i +1 can assume the values 1 through 9 and is uniquely determined fromthe integer condition: N i − a ∗ b i +1 . Here we show that N is a perfect square if and only if the condition f rac i = f i issatisfied after i steps. Conversely, N is not a square if f rac j < f j without havingsatisfied the condition for squareness above for i < j . Of general concern could bethat the algorithm would map different N values, squares and non-squares, to thesame p . As we shall see, this is not the case.Combining the square condition: N = ( a + 18 p ) with equation 6 yields the testcondition for squareness: N == ( a + 18( b + 9 b + ... + 9 i b i +1 )) (15)As discussed in section 4, equation 15 is equivalent to evaluating: N − a ( b + 9 b + ... + 9 i b i +1 ) == 9( b + 9 b + ... + 9 i b i +1 ) (16)Section 4 also showed how each 1 ≤ b i ≤ i , with f rac i decreasing and f i increasing with i until condition f rac i ≤ f i is satisfied or superseded. At step i all values of b are uniquely determined in theexpansion of p ; and because the representation of p is a unique expansion of b i mod9, we see the validity of the perfect square condition stated above. If N tests to besquare, its root is also unique per equation 15. For cubes, N is determined from N = ( a + 18 p ) as: N = N − a ∗
18 = a p + 18 ap + 6 ∗ p (17)Recall, from figure 1 that three parallel tests for a have to be conducted for thiscase. The first equation to be evaluated becomes:7 DR(N) frac i = N i – a*b i+1 N i = frac i-1 – f i-1 > 0 No solutionNext “a”Select a
SETUP
For frac i integerfind b i+1 i = i + 1YesYes NoNoi = 0 p i = p i-1 +9 i *b i+1 f i = b i+1 *(p i-1 +p i ) frac i == f i LOOP b = 0p -1 = 0N = N – a INIT
N = a + 18p i Figure 4:
Logic flow diagram for finding the square root of an odd integer N notdivisible by 3. − a b ab + 12 b (18)Although two divides both sides, we will maintain the same nomenclature as forsquares. In this case f rac = ( N − a b ) / f = 2 b ( a + 6 b ). Proceeding as forsquares N = f rac − f becomes: N = N − a ∗ b − ∗ f p ; p = b + 9 b yielding: N − a ( b + 9 b )9 == 2 a ( b + 9 b ) + 12( b + 9 b ) (20)As in the quadratic case the termination criterion is met when: f rac i ≤ f i .Observe that the same equations and steps applies to cubes as for squares. Thedifference between the two methods lies in the existence of a recursion relation forsquares. Perhaps recursion relations can be found for higher powers. If not, theabove step-by-step approach can be applied to all powers. Although the formulaefor a given power contain increasingly more terms as i increases, they only have tobe derived once per root. The smallest possible value of N of the form ( a + 18 p ) ∗ ( a + 18 p ) is N = 361 for a = 1and p = 1. Smaller squares are possible for p = 0 but are considered trivial in thiscontext since the focus here lies in computing roots of very large integers. In orderto find an approximate formula for the number of iterations, i , we recall that f rac i decreases by a factor of 9 per step, whereas f i increases by a factor of 9 per step,and that the algorithm terminates when the two quantities are equal or f rac i < f i .Therefore, the relative size of f rac i and f i after i steps are: f rac i = f ∗ i (21)From equation 5 and the conservative approximation of a = 0, we find f rac = N/ (9 ∗ ). Setting f = 9 conservatively in equation 20 yields the desired expressionbetween N and the number of iterations i , N = 9 i +3 . This can be rewritten as: i = log ( √ N −
32 (22)9xpression 21 represents the number of iterations required for each a value andtherefore doubles when two tests have to be performed. Recall from equation 1 that, N does not contain any factors of two and three. If N is not a square, the samenumber of iterations are required. As a point example consider √ N for N = 1429822969 = 37813 with DR ( N ) = 7.The allowed values for a in this case are 13 and 5. We will test for a = 13 first andsee how the algorithm works when a solution is found, in figure 5, and then showwhat happens when there is no solution by testing for a = 5, figure 6.i a = 13 DR ( N i ) b i b i +1 p i f rac i f i N = 1429822969 70 N = 39717300 3 0 3 3 4413029 91 N = 4413020 5 3 8 75 490324 6242 N = 489700 1 8 7 642 54401 50193 N = 49382 8 7 2 2100 5484 54844 N = 0 2Figure 5: Illustration on how the square root of N = 1429822969 can be found infour iterations. The algorithm terminates with a solution when f rac i = f i . Thus p = 2100 = (3 + 9 ∗ ∗ ∗ with solution √ N = 13 + 18 ∗ . The following example shows the algorithm steps for finding the cube root of athirteen digit number N = 6 , , , , DR ( N ) = 1, a can take on thevalues 1, 7, and 13. We will test N for a = 7 since this is the solution in this example.The cubic root in this example is not computed by using a recursion relation but bystep-by-step calculation of f rac i and f i . 10 a=5 DR ( N i ) b i b i +1 p i f rac i f i N = 14298229690 N = 39717304 7 0 5 5 4413031 251 N = 4413006 9 5 9 86 490329 8192 N = 489510 9 9 9 815 54385 81093 N = 46276 7 9 5 4460 5139 263754 N = − Illustration on how a square root test of a ten-digit number N is concludedin four steps when N is not square. The algorithm terminates when f rac i < f i whichoccurs for i = 3 . i a = 7 DR ( N i ) b i +1 f rac i f i N = 6177847762549 10 N = 114404588189 8 2 12711620899 1521 N = 12711620747 2 5 1412402278 1418502 N = 1412260428 3 3 156917809 36119583 N = 153305851 4 1 17033978 170339784 N = 0Figure 7: Example on how the cube root of a thirteen digit number N can be foundin four iterations when the correct value for a is selected. The algorithm terminateswith a solution when f rac i = f i . In this example p = 2 + 9 ∗ ∗ ∗ ;and the cube root of N is therefore: ∗ . Since U contains all odd primes, except 3, all twin primes are members of U . Infact, since the difference between two twin primes is 2, twin primes can only occurbetween the residue classes [5], [7] (Type A); [11], [13] (Type B); and [17], [1] (TypeC). The first 12 twin prime sets and their types are given in figure 8.Because the product between twin primes always belongs to [17] and squaresalways belong to [1] , [7] , [13]; the a , and therefore DR(N), in N = a + 18 p becomes aconvenient differentiator between the two cases.11od 18 [17] [17] [17]Twin Primes [ r i ] [ r j ] Type A Type B Type C5, 7 [5] [7] A11, 13 [11] [13] B17, 19 [17] [1] C29, 31 [11] [13] B41, 43 [5] [7] A59, 61 [5] [7] A71, 73 [17] [1] C101, 103 [11] [13] B107, 109 [17] [1] C137, 139 [11] [13] B149, 151 [5] [7] A179, 181 [17] [1] CFigure 8: Illustration on how twin primes can be divided into three different typesbased on their residues classes ( mod . Note how the product of all three types canbe written as
17 + 18 ∗ X .
10 Acknowledgment
The author would like to thank John Lindgren for providing very helpful suggestions,verifying essential elements, and most of all for encouraging the publication of thiswork.
11 References11 References