Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ArFcnTab Car
Function PConstructs a TABle of values of the six ARithmetic Function Computes the CARmichael function c(m), which is
P (a,m) = 1.
d|n P 1, _(n), and _(n) = Algorithm First the canonical factorization of m is determined
Syntax arfcntab p is an odd prime then c(pj) = pj−1(p − 1). Also, c(2) = 1, c(4)
PgDn Display the next 20 values and c(2j) = 2j−2 for j _ 3. Finally, c(m) is the least common
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
Algorithm When the program begins execution, it first Comments This program provides a user interface for the
not exceeding 109/2 , by sieving. These primes are used for in the NoThy unit. To see how the algorithm is implemented,
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)
next prime after this is 31621. When N is specified, the odd Fractions are displayed
in the interval [10N, 10N + 200] are sieved by those small 74 Reference Guide to Turbo Pascal Programs
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
subdivisions) to see whether the residue class gjs (mod p) is in values are
finding a match, then a is not a power of g (mod p). Thus the PgDn View the next 200 entries
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
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
found in the unit NoThy. To see how the algorithm is MULT, in which the
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).
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
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
of proving primality have been developed that are faster than algorithm.
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
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
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.
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
n (mod m), in connection with WARing’s problem. routines are accessed in one of two ways. First, there are files
n First line displayed is n program you may type {$I timer.i }. (The space after the .i is
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
one must multiply by the appropriate multinomial coefficient unit, the initial lines of your program should include
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
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