



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




H. J. W E G S T E N ,^ E d i t o r
A L G O R I T I t M 93 G E N E R A L O R D E R A R I T H M E T I C MILLARD H. PERSTEIN C o n t r o l D a t a C o r p. , P a l o A l t o , Calif.
p r o c e d u r e arithmetic (a, b, c, op); i n t e g e r a, b, c, op; ¢ o n l m e n t This procedure will perform different order arithmetic operations with b and c, p u t t i n g the result in a. The order of the operation is given by op. For op = 1 addition is performed. For op = 2 multiplicaLion, repeated addition, is done. Beyond these the operations are non-commutative. For op = 3 exponentiation, repeated multiplication, is done, raising b to the power c. Beyond these the question of grouping is i m p o r t a n t. The innermost implied parentheses are at the right. The hyper-exponent is always c. For op = 4 t e t r a t i o n , repeated exponentiation, is done. For op = 5, 6, 7, etc., the procedure performs p e n t a t i o n , hexation, heptation, etc., respectively. The routine was originally programmed in FORTRAN for the Control D a t a 160 desk-size computer. The original program was limited to t e t r a t i o n because subroutine recursiveness in Control D a t a 160 FORTRAN has been held down to four levels in the interests of economy. The input parameter, b, c, and op, must be positive integers, not zero; b e g i n o w n i n t e g e r d, e, f, drop; i f o p = 1 t h e n b e g i n a := h-4- c; go t o l e n d i f o p = 2 t h e n d := 0; else d := 1; e := c; drop := op - 1; for f := I s t e p 1 u n t i l e do b e g i n arithmetic (a, b, d, drop); d : ~ a e n d ; 1 : e n d arithmetic
B u r r o u g h s C o r p. , B u r r o u g h s L a b o r a t o r i e s , P a o l i , P a.
p r o c e d u r e C O M B I N A T I O N (J, N, :K); v a l u e N, K; i n t e g e r a r r a y J; i n t e g e r N , K; c o l n m e n t This procedure generates the next combination of N integers taken K at a time upon being given N , K and the pre- vious combination. The K integers in the vector J(1)... J ( K ) range in value from 0 to N -- 1, and are always monotonically strictly increasing with respect to themselves in input and output format. If the vector J is set equal to zero, the first combination produced is N - - K , .. •, N - 1. T h a t initial combina- tion is also produced after 0, 1, • - • , N - l , the last value in t h a t cycle; b e g i n i n t e g e r B: L; B : = I ;
mainb0dy: i f J ( B ) > = B t h e n b e g i n A := J(B) -- B - l ; for L := 1 s t e p 1 u n t i l B do J(L) := L-4- A; go t o exit e n d ; i f B = K t h e n go t o initiate; B := B + 1; go t o mainb0dy; initiate: f o r B := 1 s t e p 1 u n t i l K d o J(B) := N - K - 1 + B exit: e n d C O M B I N A T I O N
A L G O R I T H M 95 G E N E R A T I O N O F P A R T I T I O N S I N P A R T - C O U N T F O R M FRANK STOCKMAL S y s t e m D e v e l o p m e n t C o r p. , S a n t a M o n i c a , Calif.
p r o c e d u r e partgen(c,N,K,G); i n t e g e r N,]:(; i n t e g e r a r r a y c; B o o l e a n G ; c o m m e n t This p r o c e d u r e operates on a given p a r t i t i o n of the positive integer N into parts ~ K , to produce a consequent partition if mm exists. Each partition is represented by the integers c[1] thru c[K], where c[j] is the number of parts of the partition equal to the integer j. If entry is made with G = false, p r o c e d u r e ignores the input array c, sets G = t r u e , and pro- duces the first partition of N ones. Upon each successive e n t r y with G = t r u e , a consequent partition is stored in c[1] t h r u c[K]. For N = K X , the final partition is c[K] = X. For N = K X + r , 1 -< r -< K - - l , final p a r t i t i o n is c[K] = X , c[r] = 1. When e n t r y is made with a r r a y c = final p a r t i t i o n , c is left unchanged and G is reset to f a l s e ; b e g i n i n t e g e r a,i,j ; i f -~ G t h e n go t o first; j : = 2 ; a := C[1]; test: i f a < j t h e n go t o B; c[j] := 1 "4- c[j]; c[1] := a - j ; zero: f o r i := 2 s t e p l u n t i l j - 1 do c[i] := 0; go t o E X I T ; B: i f j = K t h e n go t o last; a : = a + j × c[jJ; j : = j + l ; go t o test; fiist : G : = t r u e ; c[1] := N; j := : K + 1; go t o zero; last: G := f a l s e ; E X I T : e n d partgen
A L G O R I T H M 96 A N C E S T O R ROBERT W. FLOYD A r m o u r R e s e a r c h F o u n d a t i o n , C h i c a g o , Ill
p r o c e d u r e ancestor (in, n); v a l u e n; i n t e g e r n; B o o l e a n a r r a y m;
c o m m e n t Initially m [i, j] is t r u e if individual i is a parent of individual j. At completion, m [i, j] is t r u e if individual i is an ancestor of individual j. T h a t is, at completion re[i, j] is t r u e if there are k, l, etc. such t h a t initially m[i, k], m[k, 1],. •., m[p, j]
matrices, J. A C M 9(1962), 11-12; b e g i n i n t e g e r i, j, k; f o r i := 1 s t e p 1 u n t i l n do f o r j := 1 s t e p 1 u n t i l n do i f In [j, i] t h e n f o r k := 1 s t e p 1 u n t i l n do i f m [i, k] t h e n m [j, k] := t r u e e n d ancestor
A r m o u r R e s e a r c h F o u n d a t i o n , C h i c a g o , Ill.
p r o c e d u r e shortest path (m,n); v a l u e n; i n t e g e r n; a r r a y In; c o m m e n t Initially m[i, j] is the length of a direct link from point i of a network to point j. If no direct link exists, m [i, j] is initially M0. At completion, m [i, j] is the length of the shortest p a t h from i to j. If none exists, m [i, j] is 1010. Reference: WAR- SHALL, S. A theorem on Boolean matrices. J, A C M 9 (1962), 11-12; b e g i n i n t e g e r i, j, k; r e a l inf, s; inf := M0; f o r t := 1 s t e p 1 u n t i l n do f o r j := 1 s t e p 1 u n t i l n do i f m [j, i] < inf t h e n f o r k := 1 s t e p 1 u n t i l n do i f m It, k~ < inf t h e n b e g i n s := in [j, il + m [i, k]; i f s < m [j, k] t h e n m [j, k] := s e n d e n d shortest p a t h
Contributions to this d e p a r t m e n t must be in the form s t a t e d in the Algorithms D e p a r t m e n t policy s t a t e m e n t (Communications, February, 1960) except t h a t ALGOL 60 notation should be used (see Communications, May 1960). Contributions should be sent in duplicate to J. H. Wegstein, Computation Laboratory, National Bureau of Standards, Washington 25, D. C. Algorithms should be in the Reference form of ALGOL 60 and w r i t t e n in a style p a t t e r n e d after the most recent algorithms appearing in this d e p a r t m e n t. For the convenience of the printer, please underline words t h a t are delimiters to appear in boldface type. Although each algorithm has been tested by its contrib- utor, no warranty, expressed or implied, is made by the con- tributors, the editor, or the Association for Computing Machinery as to the accuracy and functioning of the algo- rithm and related algorithm material, and no responsi- bility is assumed by the contributor, the editor, or the association for Computing Machinery in connection there- with. The reproduction of algorithms appearing in this depart- ment is explicitly p e r m i t t e d without any charge. When re- production is for publication purposes, reference must be made to the algorithm author and to the Communications issue bearing the algorithm.
S y r a c u s e U n i v e r s i t y C o m p u t i n g C e n t e r , S y r a c u s e , N. Y.
p r o c e d u r e C O M P L I N E I N T G R L ( A , B, N, RSSUM); v a l u e A, B, N; r e a l A, B, N ; a r r a y RSSUM; c o m m e n t C O M P L I N E I N T G R L approximates the complex line integral by evaluating the partial Riemann-Stieltjes sum ~ L l f(zk)[zt -- zt-l] where a =< t #_ b and zk ~ (Zt_l , zt). The programmer must provide 1) the procedures GAMMA(T, Z) to calculate z(t) on P, and F U N C T ( Z , F) to calculate function values, and 2) the end points A and B of the parametric interval and N the number of subintervals into which [a, b] is to be partitioned ; b e g i n i n t e g e r I; r e a l T, D E L T ; r e a l a r r a y ZT, ZTL, D E L Z , ZK, PART[l:2]; RSSUM[1] := 0.0; RSSUM[2] := 0.0; D E L T := (B - A ) / N ; T := A; line: GAMMA(T, ZT); i f T = A t h e n go t o next; f o r I := 1 s t e p 1 u n t i l 2 do b e g i n DELZ[I] := ZT[I] -- ZTL[I]; e n d ; f o r I := 1 s t e p 1 u n t i l 2 do b e g i n ZK[I] := ZTL[I] + DELZ[I]/2.0; e n d ; F U N C T ( Z K , FZ); PART[l] := FZ[1] X DELZ[1] -- FZ[2] X DELZ[2]; PART[2] := FZ[1] X DELZ[2] + FZ[2] X DELZ[1]; f o r I := 1 s t e p 1 u n t i l 2 do b e g i n RSSUM[I] := RSSUM[I] + PART[I]; e n d ; i f T < B - (0.25 X D E L T ) t h e n go t o next e l s e go to, exit; next: f o r I := 1 s t e p 1 u n t i l 2 do b e g i n ZTL[I] := ZT[I]; e n d ; T := T + D E L T ; go t o line; exit: e n d C O M P L I N E I N T G R L.
D a r t m o u t h C o l l e g e , H a n o v e r , N. H.
p r o c e d u r e Jacobi (n,m,r); v a l u e n,m; i n t e g e r n, In, r; c o m m e n t Jacobi computes the value of the Jacobi symbol (n/m), where m is odd, by the law of quadratic reciprocity. The param- eter r is assigned one of the vahms --1, 0, or 1 if m is odd. If m is even, the symbol is undefined and r is assigned the vahm 2. For odd m the routine provides a t e s t of whether m and n are relatively prime. The value of r is 0 if and only if m and n have a nontrivial common factor. In the special case where m is prime. r = - 1 if and only if n is a quadratic nonresidue of m; b e g i n i n t e g e r s ; B o o l e a n p, q; B o o l e a n p r o c e d u r e parity (x) ; v a l u e x; i n t e g e r x; c o m m e n t The value of the function p a r i t y is t r u e if x is odd, f a l s e if x is even; b e g i n parity := x + 2 X 2 # x e n d parity;
ALGORITHM 103
SIMPSON'S RULE INTEGRATOR
GuY F. KUNClR UNIVAC Division, Sperry Rand Corp., San Diego, Calif.
v a l u e a, b, eps, N ; i n t e g e r N ;
c o m m e n t T h i s p r o c e d u r e i n t e g r a t e s t h e f u n c t i o n f i x ) u s i n g a modified S i m p s o n ' s R u l e q u a d r a t u r e f o r m u l a. T h e q u a d r a t u r e is p e r f o r m e d o v e r j s u b i n t e r v a l s of [a,bJ f o r m i n g t h e t o t a l a r e a I. C o n v e r g e n c e in e a c h s u b i n t e r w d of l e n g t h (b--a)~2 '~ is i n d i c a t e d w h e n t h e r e l a t i v e difference b e t w e e n s u c c e s s i v e t h r e e - p o i n t a n d f i v e - p o i n t a r e a a p p r o x i m a t i o n s A3,j = ( b - a ) ( g o + 4g2 + g4)/(3"2 "+~) As, j = ( b - a ) ( g o + 4gt + 292 + 493 + g4)/(3.2 '~+2) is less t h a n or e q u a l to a n a p p r o p r i a t e p o r t i o n of t h e over-all t o l e r a n c e eps (i.e., I(A~,j -- A~,j)/A~,i [ ~ e p s / 2 " w i t h n ~ N ). S I M P S O N will r e d u c e t h e size of e a c h i n t e r v a l u n t i l t h i s con- d i t i o n is satisfied. C o m p l e t e i n t e g r a t i o n o v e r [aft] is i n d i c a t e d b y i = b. A w d u e a =< i < b is i n d i c a t e s t h a t t h e i n t e g r a t i o n w a s t e r m i n a t e d , l e a v - i n g I t h e t r u e a r e a u n d e r f in [aft]. F u r t h e r i n t e g r a t i o n o v e r [i,b] will n e c e s s i t a t e e i t h e r t h e a s s i g n m e n t of a l a r g e r N , a l a r g e r eps, or a n i n t e g r a l s u b s t i t u t i o n r e d u c i n g t h e slope of t h e i n t e g r a n d in t h a t i n t e r v a l. I t is r e c o m m e n d e d t h a t t h i s p r o c e d u r e be u s e d b e t w e e n k n o w n i n t e g r a n d m a x i m a a n d m i n i m a. ;
I : = i : = m : = n : = 0 ; g[0] : = f ( a ) ; g[2] : = f ( ( a + b ) / 2 ) ; g[4] : = f i b ) ; A[0] : = (b -- a) X (g[0] "-4- 4 X g[2] + g[4])/2; A A : d : = 2Tn; h : = (b - a ) / 4 / d ; g[1] : = f(a + h X (4 X m + 1)); g[3] : = f ( a + h X (4 X m + 3)); All] : = h × (g[0] + 4 × g[ll + g[2]); A[2] : = h × (g[2] + 4 × g[3] + g[4]); i f abs (((All] + A[2]) -- A[0])/(A[1] + A[2])) > e p s / d t h e n b e g i n m : = 2 X m ; n : = n + 1; i f n > N t h e n g o t o C C ; A[0] : = All]; S[n,ll : = A[2]; S[n,2] : = g[3]; S[n,3] : = g[4]; g[4] : = g[2]; g[2] : = g[1]; g o t o A A e n d e l s e b e g i n I : = I + (All] + A[2])/3; m : = m + 1; i : = a + m × ( b - - a ) / d ; B B : i f n l = 2 X (m + 2) t h e n b e g i n m : = m + 2; n : = n -- 1; g o t o B B e n d i f (in # 1) V (n # 0) t h e n b e g i n A[0] : = S[n,l]; g[0] : = g[4]; g [ 2 ] : = S[n,2]; g [ 4 ] : = S[n,3]; g o t o A A e n d e n d C C : e n d S I M P S O N
REMARK ON ALGORITHM 19 RINOMIAL COEFFICIENTS (Richard R. Kenyon, Comm. ACM, Oct. 1960) B I C H A R D S T E C K Armour Research Foundation, Chicago 16, Ill.
T h e f o r c l a u s e of A l g o r i t h m 19 s h o u l d r e a d :
f o r i : = 0 s t e p 1 u n t i l b - 1 d o W i t h t h i s c o r r e c t i o n t h e a l g o r i t h m was certified on t h e A r n i o u r R e s e a r c h F o u n d a t i o n UNIVAC 1105. T h e r e c u r s i o n f o r m u l a s t a t e d in t h e c o m m e n t s h o u l d r e a d : Ch~ = ( n - i ) C ~ ' ~ / ( i + i ).
CERTIFICATION OF ALGORITHM 46 EXPONENTIAL OF A COMPLEX NUMBER (J. R. Herndon, Comm. ACM 4 (Apr., 1961), 178) A. P. RELPH Atomic Power Div., The English Electric Co., Whetstone, England A l g o r i t h m 46 w a s t r a n s l a t e d u s i n g t h e DEUCE ALGOL c o m p i l e r , no c o r r e c t i o n s b e i n g r e q u i r e d , a n d g a v e s a t i s f a c t o r y r e s u l t s.
CERTIFICATION OF ALGORITHM 48 LOGARITHM OF A COMPLEX NUMBER (J. R. Herndon, Comm. ACM 4 (Apr., 1961), 179) A. P. R E L P H Atomic Power Div., The English Electric Co., Whetstone, England A l g o r i t h m 48 was t r a n s l a t e d u s i n g t h e DEUCE ALGOL c o m p i l e r , a f t e r c e r t a i n m o d i f i c a t i o n s h a d b e e n i n c o r p o r a t e d , a n d t h e n g a v e s a t i s f a c t o r y r e s u l t s. T h e original v e r s i o n will fail if a = 0 w h e n t h e p r o c e d u r e for a r c t a n is e n t e r e d. It also a s s u m e s t h a t - ~ - / 2 < d <3~-/2, w h e r e a s t h e p r i n c i p a l v a l u e for l o g a r i t h m of a c o m p l e x n u m b e r a s s u m e s --~r<d~Tr. I n c i d e n t a l l y , t h e ALGOL 60 i d e n t i f i e r for n a t u r a l l o g a r i t h m is In, n o t log. T h e m o d i f i e d p r o c e d u r e is as follows:
c o m m e n t T h i s p r o c e d u r e c o m p u t e s t h e n u m b e r c + di w h i c h is e q u a l to t h e p r i n c i p a l v a l u e of log~ (a + bi). If a = 0 t h e n c is p u t e q u a l to --,047 w h i c h is u s e d to r e p r e s e n t " - - i n f i n i t y " ; b e g i n i n t e g e r m , n m : = s i g n (a); n : = sign (b); i f a = 0 t h e n b e g i n c : = --1047; d : = 1.5707963 × n ; g o t o k e n d ; c : = s q r t ( a X a + b X b); c : = In (c); d : = 1.5707963 X ( l - m ) X ( 1 - 4 - n - n X n ) + a r c t a n ( b / a ) ; k: e n d L O G C ;
CERTIFICATION OF ALGORITHM 58 MATRIX INVERSION (Donald Cohen, Comm. A CM 4, May 1961) R I C H A R D A. C O N G E R Yalem Computer Center, St. Louis University, St. Louis, Mo.
I n v e r t w a s h a n d - c o d e d in FORTRAN for t h e I B M 1620. T h e f o l l o w i n g c o r r e c t i o n s were f o u n d n e c e s s a r y : T h e s t a t e m e n t ak,j : = ak,i -- bj X Ck should be ak.j : = ak.j -- bj × Ck
T h e s t a t e m e n t g o t o b a c k should be changed to i : = Z k ; Zk : = Zj ; Z~ : = i; g o t o b a c k
A f t e r t h e s e c o r r e c t i o n s were m a d e , t h e p r o g r a m w a s c h e c k e d by i n v e r t i n g a 6 × 6 m a t r i x a n d t h e n i n v e r t i n g t h e r e s u l t. T h e s e c o n d r e s u l t was e q u a l to t h e original m a t r i x w i t h i n round-off.
INVRS was t r a n s l a t e d using the Burroughs 220 Algebraic Computer (BALcOM) at Stanford University, using 8-digit floating- point arithmetic. The misprint noted by Randell and Broyden (Comm. A C M , Jan. 1962, p. 50) was corrected, and the same example (Wilson's 4 X 4 matrix) was used as a test case. The resulting inverse was: 68.0000 --41.0000 --17.0000 10. 25.0000 10.0000 --6. 5.0000 --3. 2.0OO It may also be useful to note t h a t the d e t e r m i n a n t of the matrix may be obtained as the successive product of the pivots• T h a t is, if t~ ( = T ( 1 , 1)) is the i t h pivot of a matrix of order n, d e t e r m i n a n t = 1 ~ t~• For the above input example, d e t e r m i n a n t = 1. Randell and Broyden's observation concerning the apparent limitation of I N V R S to positive definite cases is correct: T h a t is, any nonsilagular real symmetric matrix (positive, indefinite, or negative) may be inverted using this algorithm. The original I N V R S should therefore be modified as follows: i f pivot = 0 t h e n go t o singular; Randell and Broyden's second example (of order 5) was also used as a t e s t case, with the resulting inverse:
-. 0000 .9999 .0000 .0000. 1.5333 -. 7333 -. 1333. -. 8666 - 1.0666 -. 5999 - 1.4666 ~. 1999 - d e t e r m i n a n t = -14. An a t t e m p t to invert the inverse of the 4 X 4 segment of the Hilbert matrix, as presented by Randell (Comm. A C M , Jan• 1962, p. 50), yielded the following results: •9999 .4999 .3333. •3333 .2499.
d e t e r m i n a n t = 6048020.
CRAM was t r a n s l a t e d using the DEUCE ALGOL compiler with the following corrections: V[i] = S was changed to V[i] := S f[k,j] = V[k] was changed to f[k,j] := V[k] It is quicker not to use the table of the C[i] in the " l o a d " sequence and instead use the following sequence: load: m := n X (nA-1)/2; f o r i := 1 s t e p 1 u n t i l m do R E A D (a[i]) ;
The following types of errors have been found in the Sorting
Procedures :
A V I N T was t r a n s l i t e r a t e d into BAC-220 (a dialect of ALGOL-58) and was t e s t e d on the Burroughs 220 computer. The following minor errors were found: