Book Pseudocode
Total Page:16
File Type:pdf, Size:1020Kb
Appendix BOOK PSEUDOCODE All algorithms in this book are written in a particular pseudocode form describable, perhaps, as a "fusion of English and C languages." The motivations for our particular pseudocode design have been summarized in the Preface, where we have indicated our hope that this "mix" will enable all readers to understand, and programmers to code, the algorithms. Also in the Preface we indicated a network source for Mathematica implementations of the book algorithms. That having been said, the purpose of this Appendix is to provide not a rigorous compendium of instruction definitions, for that would require something like an entire treatise on syntactical rules as would be expected to appear in an off-the-shelf C reference. Instead, we give below some explicit examples of how certain pseudocode statements are to be interpreted. English, and comments For the more complicated mathematical manipulations within pseudocode, we elect for English description. Our basic technical motivation for allowing "English" pseudocode at certain junctures is evident in the following example. A statement in the C language if((n== floor(n» && (j == floor(sqrt(j»*floor(sqrt(j»» ... , which really means "if n is an integer and j is a square," we might have cast in this book as if(n, vJ E Z) ... That is, we have endeavored to put "chalkboard mathematics" within conditionals. We have also adopted a particular indentation paradigm. If we had allowed (which we have not) such English as: For all pseudoprimes in S, apply equation (X); Apply equation (Y); then, to the aspiring programmer, it might be ambiguous whether equation (Y) were to be applied for all pseudoprimes, or just once, after the loop on equation (X). So the way we wish such English to appear, assuming the case that equation (Y) is indeed applied only once after looping, is like so: For all pseudoprimes in S, apply equation (X); Apply equation (Y); 498 Appendix BOOK PSEUDOCODE Because of this relegation of English statements to their own lines, the interpretation that equation (Y) is to be invoked once, after the pseudoprime loop, is immediate. Accordingly, when an English statement is sufficiently long that it wraps around, we have adopted reverse indentation, like so: Find a random t E [O,p - 1] such that t2 - a is a quadratic nonresidue (mod p), via Algorithm 2.3.5; x = (t + ~)(p+l)/2; In this last example, one continually chooses random integers t in the stated range until one is found with the required condition, and then one goes to the next step, which calls for a single calculation and the assignment of letter x to the result of the calculation. Also in English will be comments throughout the book pseudocode. These take the following form (and are right-justified, unlike pseudocode itself): x = (t + ~)(p+l)/2; I I Use F p2 arithmetic. The point is, a comment prefaced with "I I" is not to be executed as pseudocode. For example, the above comment is given as a helpful hint, indicating perhaps that to execute the instruction one would first want to have a subroutine to do F p2 arithmetic. Other comments clarify the pseudocode's nomenclature, or provide further information on how actually to carry out the executable statement. Assignment of variables, and conditionals We have elected not to use the somewhat popular assignment syntax x := y, rather, we set x equal to y via the simple expedient x = y. (Note that in this notation for assignment used in our pseudocode, the symbol "=" does not signify a symmetric relation: The assignment x = y is not the same instruction as the assignment y = x.) Because assignment appears on the face of it like equality, the conditional equality x == y means we are not assigning, merely testing whether x and yare equal. (In this case of testing conditional equality, the symbol "==" is indeed symmetric.) Here are some examples of our typical assignments: x = 2; I I Variable x gets the value 2. x = y = 2; I I Both x and y get the value 2. F = {}; I I F becomes the empty set. (a,b,c) = (3,4,5); I I Variable a becomes 3, b becomes 4, c becomes 5. Note the important rule that simultaneous (vector) assignment assumes first the full evaluation of the vector on the right side of the equation, followed by the forcing of values on the left-hand side. For example, the assignment (x,y) = (y2,2x); means that the right-hand vector is evaluated for all components, then the left-hand vector is forced in all components. That is, the example is equivalent to the chain (technically, we assume neither of x, y invokes hidden functions) Appendix BOOK PSEUDOCODE 499 t = x; / /Variable t is temporary here. x = y2; y = 2t; and it is quite evident by comparison how visually efficient is the single-line vector assignment. Note, too, that the composite assignments x = y2; y = 2x; and y = 2x; x = y2; are both different than the vector assignment, and different from each other. Because our text adheres to the rule that ordered sequences are symbolized by parentheses (as in (x n )) while sets use braces (as in {X,a,a}), we assign sequences, vectors, and so on with a style consistent with the text; e.g., iJ = (0,1,0) is an ordered assignment, whereas a set of three polynomials might be assigned as S = {x2 + 1,x,x3 - x} and the order is unimportant. Moreover, S = {x2 + 1, x, x, x3 - x} is exactly the same assignment, since set notation does not record multiplicity. Note that the distinction between sequence and set assignment is important, in view of the liberal use of braces in modern languages. In the Mathematica language, braces denote "lists" and these in turn can be manipulated as either vectors (sequences) or sets, with vector-algebraic (such as matrix-vector) and set-theoretical (such as union, intersection) operators available. Likewise, the C language allows assignment of data records via braces, as in "float x[3] = {1.1, 2.2, 3.3};" which would fill a vector x in ordered fashion. In this latter case, our pseudocode would say instead x = (1.1,2.2,3.3). The internal conditionals in if0 statements often use classical mathemat- ical notation, but not always. Let us exemplify conditional syntax like so: if(x == y) taskO; / / Testing equality of x, y, 'Without changing either. if(x ::::> y) taskO; / / Testing whether x is greater than or equal to y. if(xly) taskO; / / Testing whether x divides y. if(x == y (mod p)) taskO; / / Testing whether x, y congruent modulo p. Note that a congruence conditional does not take the form x ==== y (mod p), because there need not be any confusion with assignment in such cases. However, it may be possible to have the construction x == y mod p, since as is explained in the text, the notation y mod p refers to the integer y - p Ly / p J. Thus, it may be that x is equal to this integer, or it may be that we wish to assign x this value (in which case we would write x = y mod p). Another conditional form is the whileO statement, exemplified by while(x =f. 0) { task1 0; task20; 500 Appendix BOOK PSEUDOCODE } which means that x is checked for zero value upon entry of the whole loop, then all the interior tasks are to be performed until, after some complete pass of the interior tasks, x is found to be zero and that ends the whileO looping. Operations that change the value of a single variable include these: x = x+c; / / x is increased by c. x = cx; / / x is multiplied by c. x=x«3; / / Shift (integer only) x left by 3 bits, same as x = 8x. x = x» 3; / / Shift right, same as x = Lx/8J. x = x /\ 37; / / Exclusive-or bits of x with O ... 0100101 binary. x = x & 37; / / And bits of x with O... 0100101 binary. ForO loops The forO loop is ubiquitous in this book, in being our primary automaton for executing tasks repeatedly. Again we defer to a set of examples, not trying rigorously to exhaust all possible forms of English-C loops possible, rather, covering some of the common styles appearing in the book. for(a s: x < b) taskO; / / For all integers x E [a, b), ascending order. for(a :::> x :::> b) taskO; / / For all integers x E [b, a], descending order. for(x E [a, b)) taskO; / / For all integers x E [a, b), ascending order. Note that the relative magnitudes of a, b in the above are assumed correct to imply the ascending or descending order; e.g., if a loop starts for ( a :::> ... ), then b should not exceed a (or if it does, the loop is considered empty). Note also that the first and third forO examples above are equivalent; we are just saying that the third form is allowed under our design rules. Note further that neither a nor b is necessarily an integer. This is why we cannot have put in a comment in the first forO example above like: "For x = a, a+l, a+2, ... , b-l," although such a comment does apply if both a, b are integers with a < b. Along such lines, an example of how forO conditionals get into more traditional mathematical notation is for(1 s: a and a2 s: m) taskO; / / Perform task for a = 1,2, ... LrmJ· of which Algorithm 7.5.8 is an example. Other examples of mixed English-C are: for(prime pIP) taskO; / / Perform for all primes p dividing F.