11
This article was downloaded by: [Southeast Missouri State University] On: 16 December 2014, At: 00:02 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 Mathematical Education in Science and Technology Publication details, including instructions for authors and subscription information: http://www.tandfonline.com/loi/tmes20 Simple parallel algorithms for primality testing and integer factorization Song Y. Yan a a Department of Mathematics , University of York , Heslington, York Y01 5DD, England Published online: 09 Jul 2006. To cite this article: Song Y. Yan (1993) Simple parallel algorithms for primality testing and integer factorization, International Journal of Mathematical Education in Science and Technology, 24:3, 457-465, DOI: 10.1080/0020739930240317 To link to this article: http://dx.doi.org/10.1080/0020739930240317 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

Simple parallel algorithms for primality testing and integer factorization

  • Upload
    song-y

  • View
    212

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Simple parallel algorithms for primality testing and integer factorization

This article was downloaded by: [Southeast Missouri State University]On: 16 December 2014, At: 00:02Publisher: Taylor & FrancisInforma Ltd Registered in England and Wales Registered Number: 1072954Registered office: Mortimer House, 37-41 Mortimer Street, London W1T 3JH, UK

International Journal ofMathematical Education in Scienceand TechnologyPublication details, including instructions for authors andsubscription information:http://www.tandfonline.com/loi/tmes20

Simple parallel algorithms forprimality testing and integerfactorizationSong Y. Yan aa Department of Mathematics , University of York ,Heslington, York Y01 5DD, EnglandPublished online: 09 Jul 2006.

To cite this article: Song Y. Yan (1993) Simple parallel algorithms for primality testingand integer factorization, International Journal of Mathematical Education in Science andTechnology, 24:3, 457-465, DOI: 10.1080/0020739930240317

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

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 warrantieswhatsoever as to the accuracy, completeness, or suitability for any purpose of theContent. Any opinions and views expressed in this publication are the opinions andviews of the authors, and are not the views of or endorsed by Taylor & Francis. Theaccuracy of the Content should not be relied upon and should be independentlyverified with primary sources of information. Taylor and Francis shall not be liablefor any losses, actions, claims, proceedings, demands, costs, expenses, damages,and other liabilities whatsoever or howsoever caused arising directly or indirectly inconnection 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

Page 2: Simple parallel algorithms for primality testing and integer factorization

forbidden. Terms & Conditions of access and use can be found at http://www.tandfonline.com/page/terms-and-conditions

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 3: Simple parallel algorithms for primality testing and integer factorization

INT. J. MATH. EDUC. SCI. TECHNOL., 1993, VOL. 24, NO. 3, 457–465

Simple parallel algorithms for primality testingand integer factorization

by SONG Y. YAN

Department of Mathematics, University of York,Heslington, York Y01 5DD, England

(Received 25 March 1992)

Primality testing and integer factorization are two classic problems in numbertheory and are believed to be computationally intractable. At present, no one hasfound an efficient algorithm for factoring an integer or for proving its primality,nor has anyone proved that no such algorithm exists. Despite this, remarkableprogress has been made in recent years. Many new algorithms, e.g. elliptic curvemethod (ECM), and number field sieve (NFS) have been proposed to approachthese classic and difficult problems. But unfortunately, most of the modernalgorithms require considerable mathematical sophistication, e.g. algebraicgeometry and algebraic number theory to appreciate them. In this note, wepresent simple (particularly parallel) algorithms for primality testing and integerfactorization based on naive elementary number theory and elementary algorithmdesign techniques, which is easy to understand for most undergraduate computerscience and mathematics students.

1. Introduction

The primality testing problem can be described as

Input: neJ/~ (n is the integer to be tested)

' Yes, if « is prime (1-1)Output:( No, if n is composite

whilst the integer factorization problem can also be similarly described as

Input: MGi/F (n is the integer to be factored)

Pi,Pi />*e primes

Output: and eite2 ek

(1.2)

k

such that n=Y\pV if « > 1

Although primality testing and integer factorization are two separate problems, wecan design one algorithm to solve the two problems. That is

n = n if n is prime (1-3)

n=pei,Pe2, • • • ,Pkk if n i s composite

0020-739X/93 $10-00 © 1993 Taylor & Francis Ltd.

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 4: Simple parallel algorithms for primality testing and integer factorization

458 Song Y. Yan

Primality testing and integer factorization problems are very important inmathematics. Gauss wrote the following in his most profound publication Disqui-sitions Arithmeticae [1]:

The problem of distinguishing prime numbers from composites, and ofresolving composite numbers into their prime factors, is one of the mostimportant and useful in all of arithmetic... The dignity of science seems todemand that every aid to the solution of such an elegant and celebratedproblem be zealously cultivated.

But unfortunately, it is not a simple matter to find the prime factorization of n or todetermine whether or not n is prime. In fact, at present no one has found an efficientalgorithm for primality testing or integer factorization, nor has anyone proved thatthere is not an efficient algorithm [2]. Despite this, remarkable progress has beenmade in recent years. Many new algorithms, e.g. elliptic curves method (ECM), andnumber field sieve (NFS) have been invented to approach these problems.According to our teaching experience for third and even fourth year computerscience students at La Trobe University, most of the new algorithms require asophisticated mathematical background (e.g. ECM uses some deep ideas fromalgebraic geometry, and NFS is based on algebraic number theory) to appreciatethem. Thus, in this note, we shall develop simple (particularly parallel) primalitytesting and integer factorization algorithms based on naive number theory [3] andelementary algorithm design techniques, which are easy to understand for theundergraduate computer science and mathematics students.

2. Algorithm designThe simplest method of solving the primality testing problem is that for each

integer i= 2, 3,4, 5 . . . , w/2 to test whether i divides n (see figure 1). This method issound and theoretically complete, but the problem is that it is too slow, since it needsw/2 trial divisions.

Input n for primality testing

Initialization

« = 2

A> =

No •

I I

Yes

1

No

n

• )

n is composite

n i s prime

Exit

Figure 1. Simplest primality testing.

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 5: Simple parallel algorithms for primality testing and integer factorization

Parallel primality testing and integer factorization 459

Theorem 2.1. Let n > 1. If n has no prime factors less than or equal to ̂ /n, then nis prime.

According to this theorem, to determine whether a given integer ntJf (n> 1) isprime or not, we can simply check whether n is divisible by the successive primedivisors

(2.4)2,3,5,7,11,17,23 L ^

since every integer greater than 1 has a prime factor. If all the answers are No, thenwe declare n to be prime, otherwise it is composite.

Let n be the given positive integer and all the possible prime divisors

2 = dl<d2<d3<.. .^dk

then we need to perform the following divisions

n n n

(2.5)

(2.6)

to decide whether n is prime or to produce the prime factors of n:

• </>, (2.7)

In practical implementation, the known primes can be stored in a sequential file(or database when n becomes very large) D

D={di\i=\,2,...,k) (2.8)

such that di = 2, d2 = 3, d3 = 5, ,..., dk4^\.s/n\-According to this understanding, a simple sequential algorithm based on trial

divisions can be described as follows (the flowchart for this algorithm is illustrated infigure 2):

Input n for primality testing

1max <— sqrt(n)

-I

I1 dk < max J—

Ye«

f N Ye*„ I—f dt\n j — |No V /

n is prime

k <— k + 1 n is composite^—•

|

Exit

Figure 2. Simpler primality testing.

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 6: Simple parallel algorithms for primality testing and integer factorization

460 Song Y. Yan

Algorithm 2.1. (Simpler primality testing algorithm)

beginifw = l

then terminate the algorithm (abnormal)else set C<-0

for each dt{i= 1,2,...,*), perform «/df

if dk\n then C=C+\i fC = 0

then output n is primeelse output n is composite

end

The following is a C program for this simple algorithm:

# include (stdio.h)main(){int c, n, i, a;c = 0; a = 0;n = 0;i = 0;printf ("Enter the integer n for primality testing ");scanf ("%d", &n);

for (i = 2; i<sqrt(n); + +i){

if((n%i)==0)c = c +1; else continue;

}if (c>0)

printf ("No. It's composite ");else

printf ("Yes. It's prime ");}This method is, of course sound and complete, but it still may need up to rc(Ly n J)1

trial divisions. For example, consider n = 388 7561. Since

V = 1971 (2.9)

and

thus, there are at most about 259 trial divisions in determining the primality of388 756 1. That is, the following divisions are needed in determining the primality of3887561:

3887561 388 7561 3887561 388 7561

1 n([\/n\) represents the number of primes up to [y/n\. For the calculation of such n(x)function, see [3].

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 7: Simple parallel algorithms for primality testing and integer factorization

Parallel primality testing and integer factorization 461

If n = 388 756 9, however, then it will need 4 divisions to find the first prime factor 7,since the first three primes 2, 3, 5 do not divide 388 756 9.

Can we design a faster algorithm than this but without involving modernmathematical tools? The answer is yes, since we can implement the divisions in thealgorithm concurrently.

Now we introduce a parallel algorithm for the above sequential algorithm.Ideally, to test, whether or not a given integer neJf is prime, or to factor it intoprimes, we can create 7r([-y/wJ) processors, each processor pt performing one divisionof an integer n by another integer di, that is

Pi performs the divisiond, = 2

p2 performs the division — -d2 = 3

p3 performs the division — -d-, = 5

Pk(k=MlJ^D) performs the division —

The following is the algorithm:

Algorithm 2.2. (an ideal parallel algorithm)

begininput nset C+-0if M = 1

then terminate the algorithm (abnormal)else create pt =2, p2, • • • ,/>* = rc(l.\/wJ) child processors

co-begin (parallel division)perform n/di(i= 1,2,... ,k)if dt\n then C = C + 1

co-endif C = 0

then output n is primeelse output n is composite

end

The flowchart for this algorithm is shown in figure 3.This method is also sound and complete, but it has reduced Tz([y/n\) divisions

into only one division in determining whether a number n ir prime or not. Forexample, if the number is 3887561, by parallel processing only one division isneeded in a unit time. Since n([y/n J) in practice may be very large, to create n([y/n J)processors may be very expensive. We now consider a realistic method, which coulduse an optimized number of processors based on some statistical or experimentalresults. For example, if we choose 106 as the processor numbers, then we use the

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 8: Simple parallel algorithms for primality testing and integer factorization

462 Song Y. Yan

Input n for Primality Testing

Create T child processors

Each perform a division d

d,\nNox w " Yes

C +-0

Nodr\n )-J Yes

Yes / * \ NoC=0?)—-

n is composite n is prime

Exit

Figure 3. Simple parallel primality testing.

modular arithmetic method to divide n([y/n\) by 106(mod 106) to get 106 equivalentclasses as follows:

+ 106

^•.(lO^ + l, ( 1 0 6 ) 1 + 2 , . . . , ( lO^

i?2:(106)2 + l, (106)2 + 2 (106)2

Ry.

Each processor Ai(i= 1,2,..., 106) performs one division within the correspondingequivalent classes (l06y + i(j=\,2,...). The algorithm for this method can bedescribed as follows:

Algorithm 2.3. (a realistic parallel algorithm)

begininput nset C«-0if w = l

then terminate the algorithm (abnormal)

then create Pi = 2,p2,... ,/>,- = rc(l.\Aj) child processorsco-begin (parallel division)

perform nldi(i= 1,2,.. .,k)ifrf,|nthen C=C+1

co-end

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 9: Simple parallel algorithms for primality testing and integer factorization

Parallel primality testing and integer factorization 463

e l s e for Rt(i= 1,2,...,j)create 106 child processorsco-begin (parallel division)

perform nldt(i= 1,2 k)iidi\n then C=C+1

co-endif C = 0

then output n is primeelse output n is composite

end

For practical implementation, the parallel algorithms discussed above can beeasily implemented on any MISD and MIMD parallel machines; they can even beeasily implemented on serial machines by using some advanced facilities, e.g., fork()or pipe() provided by the programming language C.

All these algorithms are actually for primality testing, since they can onlyproduce the smallest (i.e. the first) prime factor of n if n is composite.

Theorem 2.2. (fundamental arithmetic theorem)Every positive integer n > 1 can be written in exactly one way as a product of the

form:

n=PVPi1...pfr=flPi' (2-12)> = i

where the pi are primes, p1 <p2 < . . . </>„, and et are positive integers.According to this theorem and Theorem 2.1, if n is composite, then we should be

able to get the unique prime decomposition expressed in equation (2.12). Forconvenience, we slightly change equation (2.12) as follows:

n=p1p2...pn, pi^Pi^-.-^Pn (2.13)

The computation procedure of the factorization is as follows:

Step 1: Use the following formulas (same as Algorithm 2.1) to find the first primefactor if n is composite:

9 = 7 (2-14)

r = nmoddi (2.15)

Step 2: If r = 0, we find the first prime factor and the next step is to set n*-q, andcontinue to perform the similar division as in step 1; if n # 0, that means dt

does not divide n, we increase i by one to select the next prime to test n again(same as step 1).

In this way, if we repeat the operations in step 1 for at most it([y/n\) times, thenwe should eventually get the standard decomposition form of n if n is composite.

For example, consider n = 388 756 9; by the above procedure, we can first get388 756 9 = 7 x 555 367 (since 2, 3, 5 do not divide 388 756 9 then we next try someother divisions and can get 555 367 = 59 x 9413; since 9413 do not divide 61,67,71 up

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 10: Simple parallel algorithms for primality testing and integer factorization

464 Song Y. Yan

to 101, hence, 9413 is prime (because 9413/101 =93.19802 and 93.19802 s$ 101). Thecomplete factorization of 388 756 9 is thus as follows:

388 7569 = 7x555 367

=7x59x9413

The following is a simple algorithm which can produce all the prime factors ofcomposite n. Of course, if n is prime, it performs the same function as Algorithm 2.1.

Algorithm 2.4. (simpler integer factorization algorithm)

begind[i](i= 1,2,..., k) is the file containing primes up to [y/n\PiiYJ— 1) 2 , . . . , m) is a list of the prime factors of n if n is compositei,j*-0; n is the integer to be factoredfor i = l step 1 to n([^/n\)

beginif w = l

then terminate the algorithmelse g+-dit r*—n mod dt

ifr^O

n<-qelse j<-j+1

endend

The flowchart of the algorithm is illustrated in figure 4.

Yeil

p, = dt

prink pt

iPt = n

print pt

Figure 4. Simpler integer factorization.

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014

Page 11: Simple parallel algorithms for primality testing and integer factorization

465 Parallel primality testing and integer factorization

The integer factorization algorithm above is also well suited for parallelimplementation (see Algorithm 2.5).

Algorithm 2.5. (ideal parallel integer factorization algorithm)

begin

d[i](t = 1, 2 , . . . , k) is a file containing primes up to L\/WJP\j](j= 1> 2 , . . •, m) is a list of the prime factors of n if n is compositei,j*-0; n is the integer to be factoredfor i = l step 1 to n([y/n\)

co-begin (create at most ^([-^/MJ) for parallel divisionsi f w = l

then terminate the algorithmelse g*-di, r*-n mod d{

ifr^OthenjV-y+1

Pj*~din*-q

else j*-j+lPj*-n

co-endend

Similarly, we can implement this algorithm in a more realistic way asAlgorithm 2.3.

3. ConclusionIn this note, we have discussed several sequential algorithms, particularly

Parallel Algorithms, for the problems of primality testing and integer factorization.Although these algorithms do not offer anything new from the complexity point ofview, compared with other primality testing and integer factorization algorithms,they are easy to understand and easy to use for most undergraduate computer scienceand mathematics students; they are also easy to implement on machines. Our point inthis note is not to develop complicated algorithms using sophisticated mathematicaltools. Those readers interested in modern primality testing and integer factorizationalgorithms could consult [4] and [5]. For general information on the problems ofprimality testing and integer factorization, the following two books [1] and [6] arehighly recommended. The design and analysis of Parallel Algorithms is discussed in[7].

References[1] KNUTH, D. E., 1981, The Art of Computer Programming: Seminumerical Algorithms, 2nd

edition (Addison-Wesley).[2] ADLEMAN, L. M., and MCCURLEY, K. S., 1987, in Discrete Algorithms and Complexity

(Academic Press), pp. 237-262.[3] EYNDEN, C. V., 1987, Elementary Number Theory (New York: Random House).[4] LENSTRA, A. K., and LENSTRA, JR., H. W., 1990, in Handbook of Theoretical Computer

Science, edited by J. van Leeuwen (Elsevier Science Publishers), pp. 673-715.[5] LENSTRA, A. K., 1990, in Cryptology and Computational Number Theory, edited by C.

Pomerance (American Mathematical Society), pp. 13-26.[6] BRESSOUD, D. M., 1990, Primality Testing and Factorization (Springer-Verlag).[7] AKL, S. G., 1989, The Design and Analysis of Parallel Algorithms (Prentice-Hall).

Dow

nloa

ded

by [

Sout

heas

t Mis

sour

i Sta

te U

nive

rsity

] at

00:

02 1

6 D

ecem

ber

2014