Sei sulla pagina 1di 40

The factorizations are determined simultaneously for all 20

Reference Guide to numbers (or


all 500 numbers, in the case of printing).
Turbo Pascal Programs See also Pi

ArFcnTab Car
Function PConstructs a TABle of values of the six ARithmetic Function Computes the CARmichael function c(m), which is

FunCtioNs !(n) = defined to be the

p|n 1, (n) = least positive integer c such that ac _ 1 (mod m) whenever

P (a,m) = 1.

pakn a, μ(n), d(n) = Syntax car [m]

P Restrictions 1 _ m < 1018

d|n P 1, _(n), and _(n) = Algorithm First the canonical factorization of m is determined

d|n d. by trial division. If

Syntax arfcntab p is an odd prime then c(pj) = pj−1(p − 1). Also, c(2) = 1, c(4)

Commands PgUp Display the preceding 20 values = 2,

PgDn Display the next 20 values and c(2j) = 2j−2 for j _ 3. Finally, c(m) is the least common

J Jump to a new point in the table multiple

P Print 500 values, starting at the top of the displayed screen of the numbers c(p_) for p_km.

Esc Escape from the environment Reference Guide to Turbo Pascal Programs 69

Restrictions 1 _ n < 109 See also Phi

Algorithm When the program begins execution, it first Comments This program provides a user interface for the

constructs a list of the primes function Carmichael found

not exceeding 109/2 , by sieving. These primes are used for in the NoThy unit. To see how the algorithm is implemented,

trial division. examine


the file nothy.pas. first screen of values appears. This may take several minutes

ClaNoTab on a slow
machine.
Function Constructs a TABle of CLAss Numbers of positive
See also QFormTab, Reduce
definite binary quadratic
Comments The time required to calculate class numbers in
forms. The number H(d) is the total number of equivalence
this manner in the range
classes of such forms of discriminant d, while h(d) counts only
−D _ d < 0 is roughly proportional to D3/2 , and roughly D
those
numbers
equivalence classes consisting of primitive forms.
must be stored. By adopting a more sophisticated algorithm,
Syntax clanotab
one could
Commands PgUp Display the preceding 40 values
calculate only those values that are to appear on a given
PgDn Display the next 40 values
screenful, and
J Jump to a new point in the table
the time required for the calculation would be much smaller,
P Print h(d) and H(d) for −2400 _ d < 0
making it
Esc Escape from the environment
feasible to construct a program of this sort that would
Restrictions −104 _ d < 0
accommodate d in
Algorithm All reduced triples (a, b, c) are found, with 0 < a <
the range −109 _ d < 0, say. For faster algorithms, see D.
p
Shanks, Class
104/3. When
number, a theory of factorization, and genera, Proc. Sympos.
a reduced triple is located, the value d = b2 − 4ac is
Pure Math.
calculated, and
20, Amer. Math. Soc. Providence, 1970, 415–440. For a
the count of H(d) is increased by 1. If gcd(a, b, c) = 1 then
method that is
the count
theoretically still faster, but that may be challenging to
of h(d) is also increased by 1. The entire table is calculated
implement, see
before the
J. L. Hafner and K. S. McCurley, A rigorous subexponential Function Determines the intersection of two arithmetic
algorithm progressions. Let g =
for computation of class groups, J. Amer. Math. Soc. 2 (1989), (m1,m2). The set of x such that x _ a1 (mod m1), x _ a2 (mod
837–850. m2)

CngArTab is empty if a1 6_ a2 (mod g). Otherwise the intersection is an


arithmetic
Function Displays the addition and multiplication TABles for
progression a (mod m). In the Chinese Remainder Theorem it
CoNGruence ARithmetic
is
(mod m).
required that g = 1, and then m = m1m2 . In general, m =
70 Reference Guide to Turbo Pascal Programs
m1m2/g .
Syntax cngartab
Syntax crt [a1 m1 a2 m2 ]
Commands " Move up
Restrictions |ai| < 1018 , 1 _ mi < 1018
# Move down
Algorithm First the linear congruence m1y _ a2 − a1 (mod m2)
Move left
is solved. If a1 6_
! Move right
a2 (mod g), then this congruence has no solution, and the
a Start at column a
intersection of
b Start at row b
the two given arithmetic progressions is empty. Otherwise,
m Set modulus m
let y denote
s Switch between addition and multiplication
the unique solution of this congruence in the interval 0 _ y <
r Display only reduced residues (in multiplication table)
m2/g .
p Print the table (if m _ 24)
Then the intersection of the two given arithmetic
Esc Escape from the environment
progressions is the set
Restrictions 1 _ m < 109
of integers x _ a (mod m) where a = ym1 + a1 and m =
See also PowerTab
m1m2/g .
CRT See also CRTDem, IntAPTab, LinCon, LnCnDem
Comments This program provides a user interface for the Function Determines det(A) (mod m).
procedure CRThm found Syntax detmodm
in the NoThy unit. To see how the algorithm is implemented, Commands A Assign dimension of matrix
examine B Build matrix
the file nothy.pas. C Choose modulus

CRTDem D Determine value of det(A) (mod m)


E Exit
Function Demonstrates the method employed to determine
F Form altered matrix
the intersection of two
Restrictions 0 < m < 109 , A = [aij ] is n × n with 1 _ n _ 9, |aij |
given arithmetic progressions.
< 109
Reference Guide to Turbo Pascal Programs 71
Algorithm Row operations are performed until the matrix is
Syntax crtdem [a1 m1 a2 m2 ]
upper-triangular. After
Restrictions |ai| < 1018 , 1 _ mi < 1018
each row operation, the elements of the new matrix are
Algorithm See the description given for the program CRT.
reduced modulo
See also CRT, IntAPTab, LnCnDem
m. The row operations used are of the following two types: (i)
DetDem Exchange
Function Demonstrates the method used to evaluate det(A) two rows (which multiplies the determinant by −1); (ii) Add
(mod m). an integral
Syntax detdem multiple of one row to a different row (which leaves the
Restrictions 0 < m < 109 , A = [aij ] is n × n with 1 _ n _ 9, |aij | determinant
< 109 unchanged).
Algorithm See description for the program DetModM. See also DetDem, SimLinDE
See also DetModM, SimLinDE Comments This program provides a user interface for the
DetModM function DetModM, which
is defined in the file det.i.
EuAlDem1 c Enter a new value of c
P Print the table
Function Demonstrates the calculation of (b, c) by using the
Esc Escape from the environment
identities (b, c) =
Restrictions 0 < b < 1018 , 0 < c < 1018
(−b, c), (b, c) = (c, b), (b, c) = (b + mc, c), (b, 0) = |b| .
See also EuAlDem1, EuAlDem3, FastGCD, GCD, GCDTab,
72 Reference Guide to Turbo Pascal Programs
LnComTab, SlowGCD
Syntax eualdem1
Restrictions |b| < 1018 , |c| < 1018 EuAlDem3
Algorithm The number m is chosen so that b + mc lies Function Demonstrates the extended EUclidean ALgorithm in
between 0 and c. The the same manner as
systematic use of the Division Algorithm in this way is known EuAlDem2, but with rounding to the nearest integer instead
as the of rounding
Euclidean Algorithm. down.
See also EuAlDem2, EuAlDem3, FastGCD, GCD, GCDTab, Syntax eualdem3
LnComTab, SlowGCD Commands PgUp Display the top portion of the table

EuAlDem2 PgDn Display the bottom portion of the table


b Enter a new value of b
Function Demonstrates the extended EUclidean ALgorithm by
c Enter a new value of c
exhibiting a table
P Print the table
of the quotients qi , remainders ri , and the coefficients xi , yi
Esc Escape from the environment
in the
Restrictions 0 < b < 1018 , 0 < c < 1018
relations ri = xib + yic.
Reference Guide to Turbo Pascal Programs 73
Syntax eualdem2
See also EuAlDem1, EuAlDem2, FastGCD, GCD, GCDTab,
Commands PgUp Display the top portion of the table
LnComTab, SlowGCD
PgDn Display the bottom portion of the table
b Enter a new value of b FacTab
Function Constructs a TABle of the least prime FACtor of odd 10N + 200; least prime factors are noted as they are found.
integers from See also Factor, GetNextP
10N + 1 to 10N + 199. Factor
Syntax factab
Function FACTORs a given integer n.
Commands
Syntax factor [n]
PgUp Display the preceding 100 values (i.e. decrease N by
Restrictions |n| < 1018
20)
Algorithm Trial division. After powers of 2, 3, and 5 are
PgDn Display the next 100 values (i.e. increase N by 20)
removed, the trial divisors
N New N ; view table starting at 10N + 1
are reduced residues modulo 30.
Esc Escape from the environment
See also P-1, P-1Dem, Rho, RhoDem
Restrictions Integers not exceeding 109 + 189 (i.e. 0 _ N _
Comments Factors are reported as they are found. The
99999999).
program can be interrupted
Algorithm When the program begins execution, it first
by touching a key. This program provides a user interface for
constructs a list of the odd
the procedure
primes not exceeding
Canonic found in the NoThy unit. To view the source code,
p
examine the file nothy.pas.
109 + 200, by sieving. We call these the “small
primes.” There are 15803 such primes, the last one being
FareyTab
31607. The Function Constructs a TABle of FAREY fractions of order Q.

next prime after this is 31621. When N is specified, the odd Fractions are displayed

integers in both rational and decimal form, up to 20 of them at a time.

in the interval [10N, 10N + 200] are sieved by those small 74 Reference Guide to Turbo Pascal Programs

primes not Syntax fareytab

exceeding Commands PgUp View the next 19 smaller entries

p PgDn View the next 19 larger entries


D Center the display at a decimal x Syntax fastgcd
R Center the display at a rational number a/q Restrictions |b| < 1018 , |c| < 1018
P Print the table (allowed for Q _ 46) Algorithm Euclidean algorithm, rounding down.
Esc Escape from the environment See also GCD, SlowGCD
Restrictions 1 _ Q < 109 FctrlTab
Algorithm If a/q and a0/q0 are neighboring Farey fractions of
Function Provides a table of n! (mod m). Each screen displays
some order Q, say
100 values.
a/q < a0/q0 , then a0q − q0a = 1. By the extended Euclidean
Syntax fctrltab
algorithm,
Commands PgUp View the preceding 100 entries
for given relatively prime a and q we find x and y such that
PgDn View the next 100 entries
xq−ya = 1.
J Jump to a new position in the table
Then q0 = y + kq , a0 = x + ka where k is the largest integer
M Enter a new modulus
such that
P Print the first 60 lines of the table
y + kq _ Q. With a/q given, the next smaller Farey fraction
Esc Escape from the environment
a00/q00 is
Restrictions 0 _ n _ 10089, 0 < m < 106
found similarly. The Farey fractions surrounding a given
Reference Guide to Turbo Pascal Programs 75
decimal number
Algorithm All 10089 values are calculated as soon as m is
x are found by the continued fraction algorithm. Fractions are
specified, unless m <
computed
10089, in which case only m values are calculated.
only as needed by the screen or the printer.
GCD
FastGCD
Function Calculates the Greatest Common Divisors of two
Function Times the execution of the Euclidean algorithm in
given integers.
calculating the Greatest
Syntax gcd [b c]
Common Divisor of two given integers.
Restrictions |b| < 1018 , |c| < 1018
Algorithm Euclidean algorithm with rounding to the nearest Function Finds the least Prime larger than a given integer x, if
integer. x _ 109 . If
See also EuAlDem1, EuAlDem2, EuAlDem3, FastGCD, 109 < x < 1018 , it finds an integer n, n > x, such that the
GCDTab, LnComTab, interval
SlowGCD (x, n) contains no prime but n is a strong probable prime to
Comments This program provides a user interface for the bases 2,
function of the same name 3, 5, 7, and 11. A rigorous proof of the primality of n can be
in the unit NoThy. To see how the algorithm is implemented, obtained
inspect by using the program ProveP.
the file nothy.pas. 76 Reference Guide to Turbo Pascal Programs

GCDTab Syntax getnextp [x]


Restrictions 0 _ x < 1018
Function Displays (b, c) for pairs of integers.
Algorithm If 0 _ x _ 109 then the least prime larger than x is
Syntax gcdtab
found by sieving.
Commands " Move up
If 109 < x < 1018 then strong probable primality tests are
# Move down
performed.
Move left
See also FacTab, ProveP
! Move right
Comments For 0 _ x _ 109 , this program provides a user
b Center table on column b
interface for the function
c Center table on row c
of the same name in the unit NoThy. To see how the
Esc Escape from the environment
algorithm is
Restrictions |b| < 1018 , |c| < 1018
implemented, inspect the file nothy.pas. For 109 < x < 1018
Algorithm Euclidean algorithm.
this
See also GCD, EuAlDem1, EuAlDem2, EuAlDem3, LnComTab
program uses the function SPsP, which is found in the unit
GetNextP NoThy, with
source code in the file nothy.pas. N Switch to non-singular roots

Hensel D Define the polynomial


p Choose the prime modulus
Function Provides a table of solutions of f(x) _ 0 (mod pj), in
Esc Escape from the environment
the manner of
Restrictions 2 _ p < 2000, pj _ 1018 , f(x) must be the sum of
HENSEL’s lemma. All roots (mod p) are found, by trying every
at most 20 monomials
residue
Algorithm The polynomial f(x) is evaluated at every residue
class. If f(a) _ 0 (mod p) and f0(a) 6_ 0 (mod p), then a tower
class, and an array
of roots
is formed of the roots. For each root found, the quantity f0(x)
lying above a is displayed. If f0(a) _ 0 (mod p) then roots lying
is calculated,
above
in order to determine whether the root is singular or not.
a are exhibited only one at a time. Roots (mod pj ) are
See also PolySolv
displayed both in
Reference Guide to Turbo Pascal Programs 77
decimal notation and in base p, a =
P HSortDem
i_1 cipi−1 . The user must choose Function DEMonstrates the HeapSORT algorithm of J. W. J.
between viewing singular or non-singular roots. The display Williams, by applying
starts with the algorithm to n randomly chosen integers taken from the
a non-singular root, if there are any. interval
Syntax hensel [0, 99]. This algorithm is employed in the programs Ind and
Commands " Lift to larger values of j IndDem.
# Drop to smaller values of j Syntax hsortdem
Shift left in the table Restrictions 1 _ n _ 31
! Shift right in the table Ind
S Switch to singular roots
Function Given g , a, and p, finds the least non-negative _ found in time O(p1/2 log p). This method was suggested by D.
such that g_ _ a Shanks.
(mod p), if such a _ exists. Thus, if g is a primitive root of p, See also IndDem, IndTab, Power, PowerTab
then IndDem
_ = indg a.
Function DEMonstrates procedure used to compute indg a
Syntax ind [g a p]
(mod p).
Restrictions |g| < 109 , |a| < 109 , 1 < p < 109 , (g, p) = 1
Syntax inddem [g a p]
Algorithm First LinCon is used to find g (mod p) so that gg _ 1
Restrictions |g| < 109 , |a| < 109 , 1 < p < 109
(mod p). The
Algorithm See the description of the program Ind.
number s is taken to be either the integer nearest
See also Ind, IndTab, Power, PowerTab
p
p or else 10000,
IndTab
which ever is smaller. A table is made of the residue classes Function Generates a TABle of INDices of reduced residue

agj (mod classes modulo a prime

p) for 0 _ j < s. This table is sorted by the HeapSort algorithm number p, with respect to a specified primitive root. Also

into generates a

increasing order. For j = 0, 1, . . . , a search is conducted (by 78 Reference Guide to Turbo Pascal Programs

binary table of powers of the primitive root, modulo p. Up to 200

subdivisions) to see whether the residue class gjs (mod p) is in values are

the displayed a one time.

table. If a match is found, then _ = is + j . If j reaches p/s Syntax indtab

without Commands PgUp View the preceding 200 entries

finding a match, then a is not a power of g (mod p). Thus the PgDn View the next 200 entries

index is J Jump to a new position in the table


E Switch from indices to exponentials
I Switch from exponentials to indices a Start at row a
M Enter a new prime modulus b Sart at column b
B Choose a new primitive root to use as the base m Set modulus m
P Print table(s) n Set modulus n
Esc Escape from the environment p Print (when table is small enough)
Restrictions p < 104 Esc Escape from the environment
Algorithm The least positive primitive root g of p is found Restrictions m < 104 , n < 104
using the program Algorithm Chinese Remainder Theorem
PrimRoot. The powers of g modulo p and the indices with See also CRT, CRTDem
respect to Comments Reduced residues are written in white, the others
g are generated in two arrays. in yellow.
See also PowerTab, PrimRoot Reference Guide to Turbo Pascal Programs 79

IntAPTab Jacobi
Function Creates a TABle with rows indexed by a (mod m) Function Evaluates the JACOBI symbol
and columns indexed �P
by b (mod n). The INTersection of these two Arithmetic Q

_
Progressions
.
is displayed (if it is nonempty) as a residue class (mod [m,
Syntax jacobi [P Q]
n]).
Restrictions |P| < 1018 , 0 < Q < 1018
Syntax intaptab
Algorithm Modified Euclidean algorithm, using quadra- tic
Commands " Move up
reciprocity.
# Move down
See also JacobDem, JacobTab
Move left
! Move right
Comments This program provides a user interface for the PgDn View the next 200 entries
function of the same name J Jump to a new position in the table
found in the unit NoThy. To see how the algorithm is Q Enter a new denominator Q
implemented, P Print 500 lines, starting with the top line displayed
inspect the file nothy.pas. Esc Escape from the environment

JacobDem Restrictions |P| < 1018 , 0 < Q < 1018


Algorithm Values are calculated as needed, using the function
Function DEMonstrates the use of quadratic reciprocity to
Jacobi.
calculate the JACOBi
See also Jacobi, JacobDem
symbol
80 Reference Guide to Turbo Pascal Programs
�P
Q LinCon
_
Function Finds all solutions of the LINear CONgruence ax _ b
.
(mod m).
Syntax jacobdem [P Q]
Syntax lincon [a b m]
Restrictions |P| < 1018 , 0 < Q < 1018
Restrictions |a| < 1018 , |b| < 1018 , 0 < m < 1018
Algorithm Modified Euclidean algorithm, using quadratic
Algorithm The extended Euclidean algorithm is used to find
reciprocity.
both the number g =
See also Jacobi, JacobTab
(a,m) and a number u such that au _ g (mod m). If g6 |b then
JacobTab there
Function Generates a TABle of values of the JACOBi function, is no solution. Otherwise, the solutions are precisely those x
with 200 values such that
displayed at one time. x _ c (mod m/g) where c = ub/g .
Syntax jacobtab See also LnCnDem
Commands PgUp View the preceding 200 entries
Comments This program provides a user interface for a b Set value of b
function of the same name c Set value of c
in the unit NoThy. To see how the algorithm is implemented, Esc Escape from the environment
inspect Reference Guide to Turbo Pascal Programs 81
the file nothy.pas. Restrictions |b| < 109 , |c| < 109 , |x| < 109 , |y| < 109

LnCnDem See also GCD, GCDTab, EuAlDem1, EuAlDem2, EuAlDem3

Function DEMonstrates the method used to find all solutions Lucas


to the LiNear CoNgruence Function Calculates the LUCAS functions Un, Vn (mod m). Here
ax _ b (mod m). the Un are
Syntax lncndem [a b m] generated by the linear recurrence Un+1 = aUn +bUn−1 with
Restrictions |a| < 1018 , |b| < 1018 , 0 < m < 1018 the initial
Algorithm See the description given for LinCon. conditions U0 = 0, U1 = 1. The Vn satisfy the same linear
See also LinCon recurrence,

LnComTab but with the initial conditions V0 = 2, V1 = a.


Syntax lucas [n [a b] m] If n,m are specified on the command
Function Creates a TABle of the LiNear COMbinations bx + cy
line, but not
of b and c, with
a, b, then by default a = b = 1.
columns indexed by x and rows indexed by y .
Restrictions 0 _ n < 1018 , |a| < 1018 , |b| _ 1018 , 0 < m _ 1018
Syntax lncomtab
Algorithm To calculate Un (mod m), the pair of residue classes
Commands " Move up
Uk−1,Uk (mod m)
# Move down
is determined for a sequence of values of k , starting with k =
Move left
1. If this
! Move right
pair is known for a certain value of k , then it can be found
x Left column is x
with k
y Bottom row is y
replaced by 2k , by means of the duplication formulae V2k+1 = VkVk+1 − a(−b)k,
U2k−1 = U2 V2k+2 = V 2

k + bU2 k+1

− 2(−b)k+1.
k−1,
By employing these transformations we eventually reach k =
U2k = 2bUk−1Uk + aU2
n.
k .
The k that arise have binary expansions that form initial
This is called “doubling.” Alternatively, the value of k can be
segments
increased
of the binary expansion of n, in the same manner as in the
by 1 by using the defining recurrence. This is called
alternative
“sidestepping.” By
powering algorithm discussed in the program PwrDem2.
repeatedly doubling, with sidesteps interspersed as
The system of calculation here is superior to that found in the
appropriate, eventually
Fifth
k = n.
82 Reference Guide to Turbo Pascal Programs
To calculate Vn (mod m), the pair Vk, Vk+1 of residue classes
Edition of NZM, where the sidestep formula involves division
(mod
by 2 and
m) is determined for a sequence of values of k , starting with
is therefore appropriate only for odd moduli.
k = 0. The
See also LucasDem, LucasTab, PwrDem2
duplication formulae are now
Comments If a = b = 1 then Un, Vn are the familiar Fibonacci
V2k = V 2

k and Lucas sequences


− 2(−b)k, Fn,Ln , respectively. This program provides a user interface for
V2k+1 = VkVk+1 − a(−b)k. the
Instead of sidestepping separately, an arithmetic economy is functions LucasU and LucasV found in the unit NoThy. To see
obtained by how the
doubling with sidestep included by means of the formulae algorithm is implemented, inspect the file nothy.pas.
LucasDem See also Lucas, LucasDem

Function DEMonstrates the method used to calculate the Mult


LUCAS functions Un , Function MULTiplies residue classes. If a, b, and m are given
Vn (mod m). with m > 0, then
Syntax lucasdem [n [a b] m] c is found so that c _ ab (mod m) and 0 _ c < m.
Restrictions 0 _ n < 1018 , |a| < 1018 , |b| < 1018 , 0 < m < Reference Guide to Turbo Pascal Programs 83
1018 Syntax mult [a b m]
Algorithm See the description given for the program Lucas. Restrictions |a| < 1018 , |b| < 1018 , 0 < m < 1018
See also Lucas, LucasDem, PwrDem2 Algorithm If m _ 109 then ab is reduced modulo m. If 109 < m

LucasTab _ 1012 then


we write a = a1106 + a0 , and compute a1b106 + a0b modulo
Function Generates a TABle of values of the LUCAS functions
m, with
Un, Vn (mod m).
reductions modulo m after each multiplication. Thus all
Syntax lucastab
numbers encountered
Commands PgUp Display the preceding 100 values
have absolute value at most 1018 . If 1012 < m < 1018 then
PgDn Display the next 100 values
we write a = a1109 + a0 , b = b1109 + b0 ; we compute ab/m
U Switch from V to U
in floatingpoint
V Switch from U to V
real arithmetic and let q be the integer nearest this quantity;
n Move to a screen with n on the top line
we
a Choose a new value for the parameter a
write q = q1109 + q0 ; m = m1109 + m0 . Then
b Choose a new value for the parameter b
ab−qm =
M Choose a new modulus m
((a1b1−q1m1)109+a1b0+a0b1−q1m0−q0m1)109+a0b0−q0m0.
P Print the initial 60 rows of the table (0 _ n _ 599)
The right hand side can be reliably evaluated, and this
Esc Escape from the environment
quantity has
Restrictions 0 _ n < 106 , |a| < 106 , |b| < 106 , 0 < m < 106
absolute value less than m. If it is negative we add m to it to MultDem2
obtain
Function DEMonstrates the method used by the program
the final result. The assumption is that the machine will
MULT when 1012 <
perform integer
m < 1018 .
arithmetic accurately for integers up to 4 · 1018 in size. The
Syntax multdem2
object is
Restrictions |a| < 1018 , |b| < 1018 , 0 < m < 1018
to perform congruence arithmetic with a modulus up to 1018
84 Reference Guide to Turbo Pascal Programs
without
Algorithm See the description given for the program Mult.
introducing a full multiprecision package.
See also Mult, MultDem1, MultDem3
See also MultDem1, MultDem2, MultDem3
Comments This program provides a user interface for the
MultDem3
function of the same name Function DEMonstrates the method used by the program

found in the unit NoThy. To see how the algorithm is MULT, in which the

implemented, methods of MultDem1 and MultDem2 are merged.

inspect the file nothy.pas. Syntax multdem3


Restrictions |a| < 1018 , |b| < 1018 , 0 < m < 1018
MultDem1
Algorithm See the description given for the program Mult.
Function DEMonstrates the method employed by the program
See also Mult, MultDem1, MultDem2
MULT when 109 <
m < 1012 .
Order
Syntax multdem1 Function Calculates the ORDER of a reduced residue class a

Restrictions |a| < 1018 , |b| < 1018 , 0 < m < 1018 (mod m). That is,

Algorithm See Problem *21, Section 2.4, p. 83, of the Fifth it finds the least positive integer h such that ah _ 1 (mod m).

Edition of NZM. Syntax order [a m [c]]

See also Mult, MultDem2, MultDem3 Restrictions |a| < 1018 , 0 < m < 1018 , 0 < c < 1018
Algorithm The parameter c should be any known positive residue class a (mod m).
number such that ac _ 1 Syntax order [a m [c]]
(mod m). For example, if m is prime then one may take c = m Reference Guide to Turbo Pascal Programs 85
− 1. Restrictions |a| < 1018 , 0 < m < 1018 , 0 < c < 1018
If a value of c is not provided by the user, or if the value Algorithm See the description given for the program Order.
provided is See also Order
incorrect, then the program assigns c = Carmichael(m). (This P–1
involves
Function Factors a number n using the Pollard p − 1 method.
factoring m by trial division.) Once c is determined, then c is
Syntax p-1 [n [a]] If n is specified on the command line, but
factored
not a, then by
by trial division. Prime divisors of c are removed, one at a
default a = 2.
time, to
Restrictions 1 < n < 1018 , 1 < a < 1018
locate the smallest divisor d of c for which ad _ 1 (mod m).
Algorithm The powering algorithm is used to calculate ak!
This
(mod n) for increasingly
number is the order of a modulo m.
large k , in the hope that a k will be found such that 1 < (ak!
See also OrderDem
−1, n) < n.
Comments This program provides a user interface for a
This method is generally fast for those n with a prime factor p
function of the same name
such that
found in the unit NoThy. To see how the algorithm is
p − 1 is composed only of small primes.
implemented,
See also P-1Dem, Rho, RhoDem, Factor
inspect the file nothy.pas.
P–1Dem
OrderDem
Function Demonstrates the method used by the Pollard p − 1
Function DEMonstrates the method used to calculate the
factoring scheme.
order of a reduced
Syntax p-1dem
Restrictions 1 < n < 1018 , 1 < a < 1018 � n

Algorithm See the description given for the program P-1. k−1

_
See also P-1
+
PascalsT
�n
Function Constructs a table of PASCAL’S Triangle k

�n _
k �= n+1
_ k

(mod m). Rows are indexed _


by n, columns by k . Up to 20 rows and 18 columns are . The entire nth row is calculated, where n is the top row on
displayed the
at one time. current screen. Other entries in the screen are calculated
Syntax pascalst from the top
Commands " Display the preceding 20 rows row.
# Display the next 20 rows Phi
Display the preceding 20 columns
Function Calculates the Euler PHI function of n.
! Display the next 20 columns
Syntax phi [n]
T Move to the top of the triangle
Restrictions 1 _ n < 1018
M Choose a new modulus
Algorithm The canonical factorization of n is found by trial
Esc Escape from the environment
division, and then _(n)
86 Reference Guide to Turbo Pascal Programs
is found by means of the formula _(n) =
Restrictions 0 _ k _ n < 104 , 0 < m < 103
Q
Algorithm The rows are calculated inductively by the
p_kn p_−1(p − 1).
recurrence
Comments This program provides a user interface for a J. C. Lagarias and A. M. Odlyzko, New algorithms for
function of the same name computing _(x),
found in the unit NoThy. To see how the algorithm is Number Theory: New York 1982, D. V. Chudnovsky, G. V.
implemented, Chudnovsky,
inspect the file nothy.pas. H. Cohn and M. B. Nathanson, eds., Lecture Notes in

Pi Mathematics 1052,
Springer-Verlag, Berlin, 1984, pp. 176–193.
Function Determines the number _(x) of primes not
J. C. Lagarias and A. M. Odlyzko, Computing _(x): an analytic
exceeding an integer x.
method,
Syntax pi [x]
J. Algorithms 8 (1987), 173–191.
Restrictions 2 _ x106
Reference Guide to Turbo Pascal Programs 87
Algorithm Primes up to 31607 are constructed, by sieving.
These primes are used PolySolv
as trial divisors, to sieve intervals of length 104 until x is Function Finds all solutions of a given polynomial congruence
reached. P(x) _ 0 (mod m).
Comments This program would run perfectly well up to 109 , Syntax polysolv
but as the the running Commands C Count the zeros
time is roughly linear in x, the smaller limit is imposed to D Define the polynomial
avoid excessive M Choose the modulus
running times. For faster methods of computing _(x), see the Esc Escape from the environment
following Restrictions 1 _ m < 104 , P(x) must be the sum of at most 20
papers. monomials, only
J. C. Lagarias, V. S. Miller, and A. M. Odlyzko, Computing _(x): the first 100 zeros found are displayed on the screen
The Algorithm The polynomial is evaluated at every residue class
Meissel-Lehmer method, Math. Comp. 44 (1985), 537–560. modulo m.
See also SqrtModP algorithm for factoring polynomials over finite fields, Math.
Comments The running time here is roughly linear in m. Comp. 36
When m is large there is (1981), 587–592.
a much faster way. By the Chinese Remainder Theorem it is Power
enough to
Function Computes ak (mod m) in the sense that it returns a
consider primepower values of m. By Hensel’s lemma, this in
number c such
turn can
that 0 _ c < m and c _ ak (mod m).
be reduced to the consideration of prime moduli. In the case
Syntax power [a k m]
of a prime
Restrictions |a| < 1018 , 0 _ k < 1018 , 0 < m < 1018
modulus p, the roots of P(x) modulo p can be found by
Algorithm Write k in binary, say k =
calculating
P
(P(x), (x−a)(p−1)/2−1) for various values of a. Here the gcd
j2J 2j . The numbers a2j (mod m) are
being calculated
constructed by repeated squaring; whenever a j 2 J is
is that of two polynomials defined mod p. In the first step of
encountered, the
the
existing product is multiplied by the factor a2j .
Euclidian algorithm, the remainder when (x−a)(p−1)/2−1 is
See also PowerTab, PwrDem1a, PwrDem1b, PwrDem2
divided by
88 Reference Guide to Turbo Pascal Programs
P(x) should be calculated by applying the powering algorithm
Comments This program provides a user interface for a
to determine
function of the same name
(x − a)(p−1)/2 (modd p, P(x)). This approach extends to provide
in the unit NoThy. To see how the algorithm is implemented,
an efficient method of determining the factorization of P(x)
inspect
(mod p).
the file nothy.pas.
For more information, see David G. Cantor and Hans
Zassenhaus, A new
PowerTab
Function Constructs a TABle of POWERs ak (mod m). Up to Algorithm The prime factors q1, q2, . . . , qr of p − 1 are found
100 powers are by trial division.
displayed at a time. Then g is a primitive root of p if and only if both gp−1 _ 1 (mod
Syntax power p)
Commands PgUp Display the preceding 10 rows and g(p−1)/qi 6_ 1 (mod p) for all i , 1 _ i _ r . When a g is found
PgDn Display the next 10 rows that
B Change the base satisfies these conditions, not only is g a primitive root of p,
E Move to a new exponent but also the
M Change the modulus primality of p is rigorously established. The algorithm
P Print the first 60 lines of the table employed by the
Esc Escape from the environment program ProveP proceeds along these lines, but with some
Restrictions |a| < 106 , 0 _ k < 106 , 0 < m < 106 short cuts.
Algorithm The first entry on the screen is computed by the See also Order, OrderDem, ProveP
powering algorithm. Comments This program provides a user interface for a
Then the remaining entries on the screen are determined function of the same name
inductively. in the unit NoThy. To see how the algorithm is implemented,
See also CngArTab, Power, PwrDem1a, PwrDem1b, PwrDem2 inspect

PrimRoot the file nothy.pas.


Reference Guide to Turbo Pascal Programs 89
Function Finds the least primitive root g of a prime number p,
such that g > a. ProveP
Syntax primroot [p [a]] If p is specified on the command line Function PROVEs that a given number p is Prime.
but not a, then Syntax provep [p]
by default a = 0. Restrictions 2 _ p < 1018
Restrictions 2 _ p < 1018 , |a| < 1018
Algorithm Trial division is applied to p−1. Whenever a prime there can be at most two such primes, say p = p1p2 . Write pi
factor q of p−1 is in base
found, say qkk(p−1), attempts are made to find an a such s, pi = ris + 1. Then p = r1r2s2 + (r1 + r2)s + 1, and the
that ap−1 _ 1 coefficients
(mod p) but (a(p−1)/q−1, p) = 1. Suppose that such an a is of this polynomial in s can be found by expanding p in base s,
found, and say
that p0|p. Let d denote the order of a modulo p0 . Then d|(p − p = c2s2 + c1s + 1. Then r1 and r2 are roots of the quadratic
1) but equation
d6 |(p − 1)/q , and hence qkkd. But by Fermat’s congruence (x − r1)(x − r2) = x2 − c1x + c2 , and hence the discriminant
d|(p0 − 1), c21
and hence it can be asserted that qk|(p0 − 1) for every prime − 4c2
factor p0 must be a perfect square. In the unlikely event that this
of p. In other words, all prime factors p0 of p are _ 1 (mod qk). quantity is a
If, for perfect square, we are led to a factorization of p; otherwise
a given q , 200 unsuccessful attempts are made to find an we have a
admissible a, proof that p is prime.
then presumably p is composite, and the program quits. If a point is reached at which it would take less time to test p
Otherwise, the for
numbers qk found are multiplied together to form a product s. divisibility by numbers d _ 1 (mod s), d _
Every p
prime factor p0 of p is _ 1 (mod s). If s > p than has already been
p spent trying to factor p−1, then the program automatically
p then there can be at switches to
most one such prime, and the proof is complete. If p1/3 < s _ this latter approach.
p1/2 then
The trial division of p−1 can be interrupted by touching a PwrDem1a
key, and
Function DEMonstrates the powering algorithm.
the user can then supply a prime factor q of the remaining
Syntax pwrdem1a [a k m]
unfactored
Restrictions |a| < 1018 , 0 _ k < 1018 , 0 < m < 1018
portion. The user is responsible for verifying that q is prime.
Algorithm See the description given for the program Power.
By this method we see that proving the primality of p is no
See also Power, PwrDem1b, PwrDem2
harder
than factoring p − 1, and that for many p it is easier. Further
PwrDem1b
methods Function An alternative DEMonstration of the powering

of proving primality have been developed that are faster than algorithm.

the best Syntax pwrdem1b [a k m]

known factoring methods. The mathematics exploited by Restrictions |a| < 1018 , 0 _ k < 1018 , 0 < m < 1018

these methods Algorithm See the description given for the program Power.

is much more sophisticated. For more precise information, See also Power, PwrDem1a, PwrDem2

consult the PwrDem2


following papers. Function DEMonstrates an alternative powering algorithm.
A. O. L. Atkin and F. Morain, Elliptic curves and primality Syntax pwrdem2 [a k m]
proving, Restrictions |a| < 1018 , 0 _ k < 1018 , 0 < m < 1018
Math. Comp. 61 (1993), 29–68 . Algorithm A sequence of powers of a is generated, in which
A. K. Lenstra and H. W. Lenstra, Jr., Algorithms in number the binary expansions
theory, of the exponents form initial segments of the binary
Handbook of Theoretical Computer Science, Vol. A, J. van expansion of k . For
Leeuwen, example, if k = 10111 in binary, then (with all exponents
ed., Elsevier, Amsterdam, pp. 673–715. written in binary)
90 Reference Guide to Turbo Pascal Programs
we start with a1 , square to form a10 , square again to form Reference Guide to Turbo Pascal Programs 91
a100 , See also Power, PwrDem1a, PwrDem1b, LucasDem
multiply by a to form a101 , square this to form a1010 , multiply QFormTab
by a to
Function Generates a TABle of all reduced binary Quadratic
form a1011 , square this to form a10110 , and finally multiply by
FORMs f(x, y) =
a to form
ax2, bxy + cy2 of given discriminant. These forms are reduced
a10111 . Of course all multiplications are carried out modulo m.
only in
In the
the sense defined in §3.5 of NZM. Hence if d > 0 then the
original method used by the program Power, the binary
reduced forms
expansions of
are not necessarily inequivalent. For each form, the content
the exponents form terminal segments of the binary
(a, b, c) is
expansion of k . The
calculated.
number of multiplications is exactly the same in the two
Syntax qformtab
methods, but
Commands PgUp Display the preceding 20 rows
this alternative method has an advantage in situations in
PgDn Display the next 20 rows
which multiplication
d Choose a new discriminant
by a is fast for some reason. For example, in powering a
P Print the first 600 lines of the table
matrix
Esc Escape from the environment
A, multiplication by A is fast if A is sparse. Similarly, in
Restrictions |d| < 106 , at most 5000 forms are displayed
computing
Algorithm Detailed search for all triples satisfying the
P(x)k , multiplication by P(x) is fast if P(x) has few monomial
definition. Thus the running
terms.
time is essentially linear in |d| . This program could run for |d|
The repeated doubling from the top down seen here is also
up to
appropriate
to the calculation of solutions of linear recurrences.
109 , but the stricter limit is imposed to avoid excessive Reduce
running times.
Function REDUCEs a binary quadratic form f(x, y) =
For faster methods, see the discussion of the program
ax2+bxy+cy2 . If the three
ClaNoTab.
coefficients are given on the command line, then a reduced
See also ClaNoTab, Reduce
form g(x, y)
Rat 92 Reference Guide to Turbo Pascal Programs
Function Finds the RATional number a/q with least q such that is found, with g equivalent to f . The discriminant d of these
the initial decimal forms is
digits of a/q coincide with those of a given real number x. also reported. A proper representation of a by g is also noted,
Syntax rat [x] and then
Restrictions |a| _ 1018 , 1 _ q _ 1018 the program terminates. If the coefficients are not given on
Algorithm Suppose that k decimal digits of x are given after the command
the decimal point. line, then an environment for manipulating forms is entered.
Put _ = 0.5 · 10−k . We want to find a/q with q minimal such When a
that form is being reduced in this environment, a chain of
|x − a/q| _ _ . By the continued fraction algorithm the least i is equivalences is
found displayed, along with the matrix M that gives the
such that |x−hi/ki| _ _ . Then the desired rational number is equivalence, and the
given by operation S or Tm that was applied to derive the new form
a = chi−1 + hi−2 , q = cki−1 + ki−2 where c is the least positive from that in
integer the preceding row of the table. Here S =
such that a/q lies in the specified interval. Since this _
inequality holds 01
when c = ai , it suffices to search the interval [1, ai]. −1 0
_ I Apply the transformation T−1
and T = M Toggle between displaying M:g ! f and M:f ! g
_ P Print the table
11 Esc Escape from the environment
01 See also ClaNoTab, QFormTab
_ Rho
.
Function Factors a given composite integer n by using
The user also has the option of applying the operations S , T ,
Pollard’s RHO method.
and T−1 ,
This program should only be applied to numbers that are
one at a time. The table will hold up to 500 forms.
already known
In the case that d > 0, the form is reduced only to the extent
to be composite; if it is applied to a prime number then it will
that
run endlessly
|a| < b _ |a| < |a| or 0 _ b _ |a| = |c| , and consequently two
without reaching any conclusion. The program can be
reduced
interrupted
forms may be equivalent.
by touching any key on the keyboard.
Syntax reduce [a b c]
Syntax rho [n [c]] If n is specified on the command line but
Restrictions |a| < 1018 , |b| < 1018 , |c| < 1018
not c, then c = 1
Commands PgUp Display the preceding 6 rows
by default.
PgDn Display the next 6 rows
Restrictions 1 < n < 1018 , |c| < 1018
a Enter a new coefficient a
Reference Guide to Turbo Pascal Programs 93
b Enter a new coefficient b
Algorithm Let u0 = 0, and for i _ 0 let ui+1 = u2i
c Enter a new coefficient c
+ c. The ui are calculated
R Reduce the form at the bottom of the table
modulo n, and for each i the quantity (u2i−ui, n) is
S Apply the transformation S
determined, in the
T Apply the transformation T
hope of finding a proper divisor of n. The numbers ui are not R Enter a message as a sequence of residue classes
stored: T Enter a message in text form
At any one time only ui and u2i are known. If a proper divisor V Choose variables: modulus m, exponent k , etc.
is found, Esc Escape from the environment
it is not necessarily prime, and if it is prime it is not Restrictions The block size must lie between 1 and 17, the
necessarily the least text must consist of at
prime divisor of n. Various values of c may be used, but c = 0 most 80 characters, 0 < k < m < 1018
and Algorithm Each residue class a (mod m) is replaced by b _ ak
c = −2 should be avoided. (mod m). To
See also RhoDem, P-1, P-1Dem, Factor decode, replace b by bk0 (mod m) where 0 < k0 < m and kk0 _

RhoDem 1
(mod _(m)).
Function DEMonstrates the Pollard RHO factoring scheme.
Syntax rhodem [n] SimLinDE
Restrictions 1 < n < 1018 , |c| < 1018 Function Gives a complete parametric representation of the
Algorithm See description given for the program Rho. solutions to a system
See also Rho, P-1, P-1Dem, Fac of SIMultaneous LINear Diophantine Equations Ax = b. The

RSA user may


request that the calculations be displayed.
Function Provides an environment for encrypting messages
94 Reference Guide to Turbo Pascal Programs
by means of the RSA
Syntax simlinde
method. The encrypting history is displayed.
Restrictions A is m×n where 1 _ m _ 10, 1 _ n _ 10, all
Syntax rsa
numbers occurring must
Commands B Set the size of the blocks
have absolute value not exceeding 1018
E Encode
P Print the data
Algorithm Row operations and changes of variable are Function Executes the Strong PseudoPrime test base a to the
performed until the system number m. This
is in diagonal form. The full Smith normal form is not reached. provides a rigorous proof of compositeness. If m survives
This such a test then
method is prone to overflow. The program as written makes it is not necessarily prime, but it is called a “probable prime”
no special because
effort to avoid overflow, but reports when it has occurred. pseudoprimes (i.e., composite probable primes) seem to form

SlowGCD a sparse
set.
Function Times the calculation of the greatest common
Syntax spsp [[a] m] If m is specified on the command line,
divisor of two numbers b
but not a, then by
and c, when only the definition is used. The only purpose in
default a = 2.
this is to
Restrictions |a| < 1018 , 2 < m < 1018
provide a comparison with FastGCD.
Algorithm The strong pseudoprime test, as invented by John
Syntax slowgcd
Selfridge and others.
Restrictions 1 _ b < 109 , 1 _ c < 109
For a full description see NZM, p. 78.
Algorithm For each d, 1 _ d _ min(|b|, |c|), trial divisions are
See also SPsPDem, ProveP
made to determine
whether d|b and d|c. A record is kept of the largest such d SPsPDem
found. Function DEMonstrates the Strong PSeudoPrime test.
Since the running time is essentially linear in min(|b|, |c|), Reference Guide to Turbo Pascal Programs 95
only small Syntax spsp [[a] m] If m is specified on the command line,
arguments should be used. but not a, then by
See also FastGCD, GCD default a = 2.

SPsP Restrictions |a| < 1018 , 2 < m < 1018


See also SPsP, ProveP to the theory of numbers, Studies in Number Theory, W. J.

SqrtDem LeVeque,
ed., Math. Assoc. Amer., Washington, 1969, pp. 117–151.
Function DEMonstrates the calculation executed by the
See also SqrtDem
program SqrtModP.
Comments This program provides a user interface for a
Syntax sqrtdem [a p]
function of the same name
Restrictions |a| < 1018 , 2 _ p < 1018
in the unit NoThy. To see how the algorithm is implemented,
Algorithm See the description given for the program
inspect
SqrtModP
the file nothy.pas.
See also SqrtModP
SumsPwrs
SqrtModP
Function Finds all representations of n as a sum of s k -th
Function Calculates the SQuareRooT Modulo a given Prime
powers, and counts
number p. If the
them in various ways.
congruence x2 _ a (mod p) has a solution, then the unique
Syntax sumspwrs [n s k]
solution x
Restrictions 1 _ n < 1011 , 2 _ s _ 75, 2 _ k _ 10
such that 0 _ x _ p/2 is returned.
96 Reference Guide to Turbo Pascal Programs
Syntax sqrtmodp [a p]
Algorithm After s − 1 summands have been chosen, a test is
Restrictions |a| _ 1018 , 2 _ p _ 1018
made as to whether
Algorithm Uses the RESSOL algorithm of Dan Shanks. This is
the remainder is a k -th power. Summands are kept in
described in §2.9 of
monotonic order;
NZM. A different method, which depends on properties of the
the multiplicity is recovered by computing the appropriate
Lucas sequences,
multinomial
has been given by D. H. Lehmer, Computer technology
coefficient. In some cases, such as sums of two squares,
applied
much faster
methods exist for finding all representations. See also SumsPwrs, Wrg2Tab, WrgStTab, WrgCnTab
See also Wrg1Tab, Wrg2Tab, WrgStTab, WrgCnTab Wrg2Tab
Wrg1Tab Function Creates a TABle of the least number s of k -th
Function Creates a TABle of the number r(n) of powers required to represent
representations of n = n, in connection with WARing’s problem.
Ps Syntax wrg2tab
i=1 xsi Commands PgUp Move up
as a sum of s k -th powers, as in WARing’s problem. If k > 2 PgDn Move down
then the k Set k , the exponent
xi are non-negative, but for k = 2 the xi are arbitrary integers. N Start the table at 10n
Syntax wrg1tab p Print the table
Commands PgUp Move up Esc Escape from the environment
PgDn Move down Restrictions 2 _ k _ 10, 1 _ n _ 104
s Set s, the number of summands Reference Guide to Turbo Pascal Programs 97
k Set k , the exponent Algorithm For s _ k , the numbers represented are found by
N Start the table at 10n allowing a k -tuple
p Print the table of variables run over all possible values, with coordinates in
Esc Escape from the environment monotonic
Restrictions 1 _ s _ 75, 2 _ k _ 10, 1 _ n _ 1011 order. For s > k , all possible k -th powers are added to
Algorithm Search for representations, with summands in numbers already
monotonic order. The represented, until more than half the numbers have been
multiplicity of a representation is recovered by multiplying by represented.
the appropriate Then all possible k -th powers are subtracted from numbers
multinomial coefficient. not represented.
See also SumsPwrs, Wrg1Tab, Wrg2Tab, WrgCnTab 98 Reference Guide to Turbo Pascal Programs

WrgCnTab Turbo Pascal


Function Creates a TABle of the number of solutions of the
congruence Programming Resources
Ps
i=1 xki A collection of basic routines are provided for use in more

_ advanced programs. These

n (mod m), in connection with WARing’s problem. routines are accessed in one of two ways. First, there are files

Syntax wrgcntab with the extension .i that

Commands PgUp Move up may be included in another program. For example, to

PgDn Move down measure the running time of a

n First line displayed is n program you may type {$I timer.i }. (The space after the .i is

m Set the modulus m essential here.) The

p Print the table effect will be the same as if the text of the file timer.i had

Esc Escape from the environment been pasted into your program

Restrictions 1 _ s _ 75, 2 _ k _ 10, 1 _ m < 5000 at this point. Second, a library of 17 number-theoretic

Algorithm First a list of all k -th power residues r is routines is provided in the Turbo

constructed, with the number Pascal unit nothy.tpu. This is a compiled module that the

of solutions of xk _ r (mod m) is recorded. Summands run over compiler will use when your

monotonically ordered residues. To recover the multiplicity of program is compiled. The source code for this unit is in the

a representation, file nothy.pas. To invoke this

one must multiply by the appropriate multinomial coefficient unit, the initial lines of your program should include

and by the multiplicities of the summands. commands of the following sort:

See also SumsPwrs, Wrg1Tab, Wrg2Tab, WrgStTab


program TwoSquares; {Use the method of Problem 6 on p. is the number of distinct primes dividing n; these primes are
333 to stored, in
write a prime p as a sum of two squares} increasing order, in the array p. The multiplicity to which
{$N+,E+} these primes
uses nothy; divide n is recorded in the corresponding location in the array
Most of the routines in NoThy accept integers as variables of m. If Prog
type comp, with a size up to Turbo Pascal Programming Resources 99
1018 . This type is available only after the compiler directive = True then the progress in computing the factorization is
{$N+} has been given. Such reported to
variables are calculated on the arithmetic coprocessor, in the screen. Since the underlying method is trial division,
floating point. If no coprocessor performance
is found, then the program will crash, unless the compiler will be slow whenever n has a very large prime factor. In such
directive {$E+} has also been a case,
given, in which case the numerical work of the coprocessor execution may be interrupted by typing any key.
will be emulated in software. Restrictions 1 _ n _ 1018

Canonic procedure NoThy Carmichael function NoThy


Function Calculates the canonical factorization of an integer. Function Computes the Carmichael function of n. That is, the
Declaration canonic(n: comp; var k: integer; var p: primes; least positive
var m: integer c such that ac _ 1 (mod n) whenever (a, n) = 1.
multiplicity; var Prog: Boolean) Declaration carmichael(n: comp)
Remarks This procedure uses two variable types defined Result type comp
within the NoThy unit: Remarks Since n is factored by trial division, performance will
primes = array[1..15] of comp; multiplicity = array[1..15] of be slow if n has a
integer. k
very large prime factor. In such a case, the execution may be 100 Turbo Pascal Programming Resources
interrupted Declaration det(A: matrix; n: integer; m: comp)
by typing any key. Result type comp
Restrictions 1 _ n _ 1018 Remarks Before this function is called, the following variable
See also Phi type must be defined:

Condition function NoThy matrix = array[1..9] of array[1..9] of comp.


Restrictions |aij | _ 1018 , 1 _ n _ 9, 1 _ m _ 1018
Function Given a and m, the number b is returned where b _
a (mod m) and GCD function NoThy
0 _ b < m. Function Calculates the greatest common divisor of two given
Declaration condition(a, m: comp) integers b and c.
Result type comp Declaration gcd(b, c: comp)
Restrictions |a| _ 1018 , 1 _ m _ 1018 Result type comp

CRThm procedure NoThy Remarks The gcd is undefined when b = c = 0.


Restrictions |b| _ 1018 , |c| _ 1018
Function Determines the intersection of two given arithmetic
progressions. GetInput function GetInput.i
Declaration CRThm(a1, m1, a2, m2: comp; var a, m: comp) Function Moves the cursor to a specified location (x, y), and
Remarks If the intersection is empty then the value m = 0 is prompts the user for
returned. an integral input. On the line just below, a comment is
Restrictions |ai| _ 1018 , 1 _ mi _ 1018 provided, which

DetModM function det.i typically concerns the range in which the input must lie. The
input is
Function Calculates the determinant of an n×n integral
accepted only when it lies in a specified interval [a, b].
matrix A = [aij ] modulo
Declaration getinput(x, y: integer; prompt, comm : string; a, b
m.
: comp)
Result type comp Result type integer
Remarks This function may be modified for more specialized Restrictions |P| _ 1018 , 1 _ Q _ 1018 , Q odd.
tasks, as is the case LinCon procedure NoThy
with the function GetDisc found in the program QFormTab.
Function Solves the linear congruence a1x _ a0 (mod m). If
Any program
solutions exist then
using this function must declare the unit CRT in the uses
they form an arithmetic progression, x _ a (mod m1).
statement.
Declaration lincon(a1, a0, m: comp; var a, m1: comp)
Restrictions 1 _ x _ 80, 1 _ y _ 25, |a| _ 1018 , |b| _ 1018
Remarks If (a1,m)6 |a0 then the congruence has no solution,
Examples See the files factor.pas, phi.pas.
and the values a =
GetNextP function NoThy (a1,m), m1 = 0 are returned.
Function Given an integer x, finds the least prime p such that Restrictions |ai| _ 1018 , 1 _ m _ 1018
p > x. LucasU function NoThy
Declaration getnextp(x: longint)
Function Computes Un (mod m). Here Un is the Lucas
Result type longint
sequence with parameters
Remarks If x < 0 or x > 109 then the value 0 is returned.
a and b, defined by the recurrence Un+1 = aUn + bUn−1 , with
Turbo Pascal Programming Resources 101
initial
Restrictions 1 _ x _ 109
conditions U0 = 0, U1 = 1. If a = b = 1 then these are the
Jacobi function NoThy Fibonacci
Function Calculates the Jacobi symbol numbers Fn .
�P Declaration lucasu(n, a, b, m: comp)
Q Result type comp
_
Restrictions 0 _ n _ 1018 , |a| _ 1018 , |b| _ 1018 , 1 _ m _ 1018
.
See also LucasV
Declaration jacobi(p, q: comp)
LucasV function NoThy Order function NoThy
Function Computes Vn (mod m). Here Vn is the Lucas Function Given a, m, and c such that ac _ 1 (mod m), the least
sequence with parameters positive integer
a and b, defined by the recurrence Vn+1 = aVn + bVn−1 , with h such that ah _ 1 (mod m) is returned.
initial Declaration order(a, m, c: comp)
conditions V0 = 0, V1 = 1. If a = b = 1 then these are the Result type comp
Lucas Remarks If (a,m) > 1 then the value 0 is returned. If (a,m) =
numbers Ln . 1 but ac 6_ 1
Declaration lucasv(n, a, b, m: comp) (mod m) then an error message is printed and the program
102 Turbo Pascal Programming Resources halts. Since
Result type comp c is factored by trial division, performance will be slow if c has
Restrictions 0 _ n _ 1018 , |a| _ 1018 , |b| _ 1018 , 1 _ m _ 1018 a very
See also LucasU large prime factor. In such a case, execution may be

Mult function NoThy interrupted by


typing any key.
Function Given a, b, and m, returns the number c such that c
Restrictions |a| _ 1018 , 1 _ m _ 1018 , 1 _ c _ 1018
_ ab (mod m)
See also PrimRoot
and 0 _ c < m.
Declaration mult(a, b, m: comp) Phi function NoThy
Result type comp Function Computes the Euler phi function _(n).
Remarks This allows congruence arithmetic for m up to 1018 Declaration phi(n: comp)
without need for Result type comp
multiple precision arithmetic. Remarks Since n is factored by trial division, performance will
Restrictions |a| _ 1018 , |b| _ 1018 , 1 _ m _ 1018 be slow if n has
a very large prime factor. In such a case, execution may be Restrictions |a| _ 1018 , 2 _ p < 1018
interrupted ReadTimer procedure Timer.i
by typing any key.
Function Give the elapsed time since the timer was set.
Turbo Pascal Programming Resources 103
Declaration readtimer
Restrictions 1 _ n _ 1018
Remarks The elapsed time is stored in the variable
See also Carmichael
TimerString, which is defined
Power function NoThy to be of type string[35]. The timer must be set before it can
Function Given a, k , and m, returns c such that c _ ak (mod be read, by
m) and 0 _ c < using the procedure SetTimer. Any program employing the
m. timer must
Declaration power(a, k, m: comp) declare the unit DOS in the uses statement.
Result type comp Restrictions The TimerString records only hours, minutes and
Restrictions |a| _ 1018 , 0 _ k _ 1018 , 1 _ m _ 1018 seconds. If a program

PrimRoot function NoThy runs for more than 24 hours, the number of days must be
added to the
Function Given an integer a and a prime number p, returns
stated time.
the least primitive
See also SetTimer
root g of p such that g > a.
Examples See the files slowgcd.pas, factor.pas.
Declaration primroot(p, a: comp)
104 Turbo Pascal Programming Resources
Result type comp
Remarks Since p − 1 is factored by trial division, performance SetTimer procedure Timer.i
will be slow if Function Sets the timer.
p − 1 has a very large prime factor. In such a case, execution Declaration settimer
may be See also ReadTimer
interrupted by typing any key.
SPsP function NoThy
Function Applies the strong pseudoprime test base a to m.
Declaration spsp(a, m: comp)
Result type Boolean
Remarks If m is proved to be composite then the value False
is returned; otherwise
the calculation is consistent with the hypothesis that m is
prime, and
the value True is returned.
Restrictions |a| _ 1018 , 2 _ m _ 1018

SqrtModP function NoThy


Function Given an integer a and a prime number p, returns
the number x such
that x2 _ a (mod p), 0 _ x _ p/2.
Declaration sqrtmodp(a, p: comp)
Result type comp
Remarks If p is found to be composite, or if a is a quadratic
nonresidue of p,
then an error message is printed and the program halts. KONSEP DASAR PEMROGRAMAN
Restrictions |a| _ 1018 , 2 _ p _ 1018
Turbo Pascal Programming Resources 105
PASCAL
106 Turbo Pascal Programming Resources 1.1 Perkembangan Pascal
Pascal adalah bahasa tingkat tinggi yang orientasinya pada
segala tujuan, dirancang oleh Prof. Niklaus Wirth dari Technical
University di Zurich, Switzerland. Nama pascal diambil sebagai End .
penghargaan terhadap Blaise Pascal, ahli matematik dan Konsep dasar pemrograman pascal Algoritma & Pemrogram
philosophi terkenal abad 17 dari Perancis. 2A Contoh :
1.2 Struktur Program Pascal Program contoh_konstanta;
Struktur dari suatu program pascal terdiri dari sebuah judul Const
program dan suatu blok program atau badan program. Blok Potongan = 0.2 ;
program dibagi lagi menjadi dua bagian, yaitu : bagian deklarasi Gaji = 25000 ;
dan bagian pernyataan. Secara ringkas, struktur suatu program Namaperusahaan = ‘PT ABC’ ;

pascal dapat terdiri dari : Begin

1. Judul program Writeln ( ‘Potongan =’, Potongan ) ;


Writeln ( ‘Gaji =’, Gaji ) ;
2. Blok program
Writeln ( ‘Nama =’, Namaperusahaan) ;
a. Bagian deklarasi
End .
- deklarasi label
1.3.2 Deklarasi Variabel
- deklarasi konstanta
Variabel adalah identifier yang berisi data yang dapat diubah-ubah nilainya
- deklarasi tipe
didalam program. Menggunakan kata cadangan Var sebagai judul didalam
- deklarasi variable
bagian deklarasi variable dan diikuti oleh satu atau lebih identifier yang
- deklarasi prosedur
dipisahkan koma, diikuti dengan titik dua dan tipe dari datanya diakhiri dengan
- deklarasi fungsi titik koma.
b. Bagian pernyataan
Program pascal yang paling sederhana adalah program yang hanya terdiri dari Contoh :
sebuah bagian pernyataan saja. Var
Begin Total, Gaji, Tunjangan : real ;
End. Jumlahanak : integer ;
Bentuk umum dari bagian pernyataan adalah : Keterangan : string [25] ;
Begin Begin
Statement; Gaji : = 500000 ;
Jumlahanak : = 3 ; ……..
Tunjangan : = 0.25 * Gaji + Jumlahanak * 30000 ; ……..
Total : = Gaji + Tunjangan ; ……..
Keterangan : = ‘Karyawan Teladan’ ; End .
Writeln ( ‘ Gaji bulanan = Rp. ‘ , Gaji ) ; 1.3.4 Deklarasi Label
Writeln ( ‘ Tunjangan = Rp. ‘ , Tunjangan ) ; Jika program menggunakan statement Goto untuk meloncat ke suatu statement
Writeln ( ‘ Total gaji = Rp. ‘ , Total ) ; yang tertentu, maka dibutuhkan suatu label pada statement yang dituju dan label
Writeln ( ‘ Keterangan = Rp. ‘ , Keterangan ) ; tersebut harus di deklarasikan terlebih dahulu pada bagian deklarasi.
End . Menggunakan kata cadangan Label diikuti oleh kumpulan identifier label
1.3.3 Deklarasi Tipe dengan dipisahkan oleh koma dan diakhiri dengan titik koma.
Pascal menyediakan beberapa macam tipe data, yaitu : Contoh :
1. tipe data sederhana, terdiri dari :
a. tipe data standar : integer, real, char, string, Boolean. Label
100, selesai ;
Konsep dasar pemrograman pascal Algoritma & Pemrogram Begin
2A Writeln ( ‘Bahasa’ ) ;
b. Tipe data didefinisikan pemakai : enumerated atau scalar, Goto 100 ;
subrange Writeln ( ‘Cobol’ ) ;
2. Tipe data terstruktur : array, record, file, set 100 :
3. Tipe data penunjuk Writeln ( ‘Pascal’ ) ;
Goto selesai ;
Contoh :
Writeln ( ‘Fortran’ ) ;
Type
Selesai :
Pecahan = real ;
End .
Bulat = integer ;
1.3.5 Deklarasi Prosedur
Huruf = string [25] ;
Begin
Prosedur merupakan bagian yang terpisah dari program dan dapat diaktifkan
dimanapun didalam program. Prosedur dibuat sendiri bilamana program akan
dibagi-bagi menjadi beberapa blok-blok modul. Prosedur dibuat didalam
program dengan cara mendeklarasikannya dibagian deklarasi prosedur.
Menggunakan kata cadangan Procedure.

Potrebbero piacerti anche