1% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
2% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
3\newcommand{\IntegerXmpTitle}{Integer}
4\newcommand{\IntegerXmpNumber}{9.38}
5%
6% =====================================================================
7\begin{page}{IntegerXmpPage}{9.38 Integer}
8% =====================================================================
9\beginscroll
10
11%
12%
13%
14%
15%
16
17
18
19%% int.htex
20%%
21%% Integer examples
22%%
23
24\Language{} provides many operations for manipulating arbitrary
25precision integers.
26In this section we will show some of those that come from \spadtype{Integer}
27itself plus some that are implemented in other packages.
28More examples of using integers are in the following sections:
29\downlink{``\ugIntroNumbersTitle''}{ugIntroNumbersPage} in section \ugIntroNumbersNumber
30\downlink{`IntegerNumberTheoryFunctions'}{IntegerNumberTheoryFunctionsXmpPage}\ignore{IntegerNumberTheoryFunctions},
31\downlink{`DecimalExpansion'}{DecimalExpansionXmpPage}\ignore{DecimalExpansion},
32\downlink{`BinaryExpansion'}{BinaryExpansionXmpPage}\ignore{BinaryExpansion},
33\downlink{`HexadecimalExpansion'}{HexadecimalExpansionXmpPage}\ignore{HexadecimalExpansion}, and
34\downlink{`RadixExpansion'}{RadixExpansionXmpPage}\ignore{RadixExpansion}.
35
36\beginmenu
37    \menudownlink{{9.38.1. Basic Functions}}{ugxIntegerBasicPage}
38    \menudownlink{{9.38.2. Primes and Factorization}}{ugxIntegerPrimesPage}
39    \menudownlink{{9.38.3. Some Number Theoretic Functions}}{ugxIntegerNTPage}
40\endmenu
41\endscroll
42\autobuttons
43\end{page}
44%
45%
46\newcommand{\ugxIntegerBasicTitle}{Basic Functions}
47\newcommand{\ugxIntegerBasicNumber}{9.38.1.}
48%
49% =====================================================================
50\begin{page}{ugxIntegerBasicPage}{9.38.1. Basic Functions}
51% =====================================================================
52\beginscroll
53
54\xtc{
55The size of an integer in \Language{} is only limited by the amount of
56computer storage you have available.
57The usual arithmetic operations are available.
58}{
59\spadpaste{2^(5678 - 4856 + 2 * 17)}
60}
61
62\xtc{
63There are a number of ways of working with the sign of an integer.
64Let's use this \spad{x} as an example.
65}{
66\spadpaste{x := -101 \bound{x}}
67}
68\xtc{
69First of all, there is the absolute value function.
70}{
71\spadpaste{abs(x) \free{x}}
72}
73\xtc{
74The \spadfunFrom{sign}{Integer} operation returns \spad{-1} if its argument
75is negative, \spad{0} if zero and \spad{1} if positive.
76}{
77\spadpaste{sign(x) \free{x}}
78}
79%
80\xtc{
81You can determine if an integer is negative in several other ways.
82}{
83\spadpaste{x < 0 \free{x}}
84}
85\xtc{
86}{
87\spadpaste{x <= -1 \free{x}}
88}
89\xtc{
90}{
91\spadpaste{negative?(x) \free{x}}
92}
93%
94\xtc{
95Similarly, you can find out if it is positive.
96}{
97\spadpaste{x > 0 \free{x}}
98}
99\xtc{
100}{
101\spadpaste{x >= 1 \free{x}}
102}
103\xtc{
104}{
105\spadpaste{positive?(x) \free{x}}
106}
107\xtc{
108This is the recommended way of determining whether an integer is zero.
109}{
110\spadpaste{zero?(x) \free{x}}
111}
112%
113
114\beginImportant
115Use the \spadfunFrom{zero?}{Integer} operation whenever you are testing any
116mathematical object for equality with zero.
117This is usually more efficient that using \spadop{=} (think of matrices:
118it is easier to tell if a matrix is zero by just checking term by term
119than constructing another ``zero'' matrix and comparing the two
120matrices term by term) and also avoids the problem that \spadop{=} is
121usually used for creating equations.
122\endImportant
123
124\xtc{
125This is the recommended way of determining
126whether an integer is equal to one.
127}{
128\spadpaste{one?(x) \free{x}}
129}
130
131\xtc{
132This syntax is used to test equality using \spadopFrom{=}{Integer}.
133It says that you want a \spadtype{Boolean} (\spad{true} or
134\spad{false}) answer rather than an equation.
135}{
136\spadpaste{(x = -101)@Boolean \free{x}}
137}
138
139\xtc{
140The operations \spadfunFrom{odd?}{Integer} and
141\spadfunFrom{even?}{Integer} determine whether an integer is odd or even,
142respectively.
143They each return a \spadtype{Boolean} object.
144}{
145\spadpaste{odd?(x) \free{x}}
146}
147\xtc{
148}{
149\spadpaste{even?(x) \free{x}}
150}
151\xtc{
152The operation \spadfunFrom{gcd}{Integer} computes the greatest common divisor of
153two integers.
154}{
155\spadpaste{gcd(56788,43688)}
156}
157\xtc{
158The operation
159\spadfunFrom{lcm}{Integer} computes their least common multiple.
160}{
161\spadpaste{lcm(56788,43688)}
162}
163\xtc{
164To determine the maximum of two integers, use \spadfunFrom{max}{Integer}.
165}{
166\spadpaste{max(678,567)}
167}
168\xtc{
169To determine the minimum, use \spadfunFrom{min}{Integer}.
170}{
171\spadpaste{min(678,567)}
172}
173
174\xtc{
175The \spadfun{reduce} operation is used to extend
176binary operations to more than two arguments.
177For example, you can use \spadfun{reduce} to find the maximum integer in
178a list or compute the least common multiple of all integers in the list.
179}{
180\spadpaste{reduce(max,[2,45,-89,78,100,-45])}
181}
182\xtc{
183}{
184\spadpaste{reduce(min,[2,45,-89,78,100,-45])}
185}
186\xtc{
187}{
188\spadpaste{reduce(gcd,[2,45,-89,78,100,-45])}
189}
190\xtc{
191}{
192\spadpaste{reduce(lcm,[2,45,-89,78,100,-45])}
193}
194
195\xtc{
196The infix operator ``/'' is {\it not} used to compute the quotient
197of integers.
198Rather, it is used to create rational numbers as described in
199\downlink{`Fraction'}{FractionXmpPage}\ignore{Fraction}.
200}{
201\spadpaste{13 / 4}
202}
203\xtc{
204The infix operation \spadfunFrom{quo}{Integer} computes the integer
205quotient.
206}{
207\spadpaste{13 quo 4}
208}
209\xtc{
210The infix operation \spadfunFrom{rem}{Integer} computes the integer
211remainder.
212}{
213\spadpaste{13 rem 4}
214}
215\xtc{
216One integer is evenly divisible by another if the remainder is zero.
217The operation \spadfunFrom{exquo}{Integer} can also be used.
218See \downlink{``\ugTypesUnionsTitle''}{ugTypesUnionsPage} in Section \ugTypesUnionsNumber\ignore{ugTypesUnions} for an example.
219}{
220\spadpaste{zero?(167604736446952 rem 2003644)}
221}
222
223\xtc{
224The operation \spadfunFrom{divide}{Integer} returns a record of the
225quotient and remainder and thus is more efficient when both are needed.
226}{
227\spadpaste{d := divide(13,4) \bound{d}}
228}
229\xtc{
230}{
231\spadpaste{d.quotient \free{d}}
232}
233\xtc{
234Records are discussed in detail in \downlink{``\ugTypesRecordsTitle''}{ugTypesRecordsPage} in Section \ugTypesRecordsNumber\ignore{ugTypesRecords}.
235}{
236\spadpaste{d.remainder \free{d}}
237}
238
239\endscroll
240\autobuttons
241\end{page}
242%
243%
244\newcommand{\ugxIntegerPrimesTitle}{Primes and Factorization}
245\newcommand{\ugxIntegerPrimesNumber}{9.38.2.}
246%
247% =====================================================================
248\begin{page}{ugxIntegerPrimesPage}{9.38.2. Primes and Factorization}
249% =====================================================================
250\beginscroll
251
252\xtc{
253Use the operation \spadfunFrom{factor}{Integer} to factor integers.
254It returns an object of type \spadtype{Factored Integer}.
255%-% \HDindex{factorization}{ugxIntegerPrimesPage}{9.38.2.}{Primes and Factorization}
256See \downlink{`Factored'}{FactoredXmpPage}\ignore{Factored} for a discussion of the
257manipulation of factored objects.
258}{
259\spadpaste{factor 102400}
260}
261
262\xtc{
263The operation \spadfunFrom{prime?}{Integer} returns \spad{true} or \spad{false} depending
264on whether its argument is a prime.
265%-% \HDindex{prime}{ugxIntegerPrimesPage}{9.38.2.}{Primes and Factorization}
266}{
267\spadpaste{prime? 7}
268}
269\xtc{
270}{
271\spadpaste{prime? 8}
272}
273\xtc{
274The operation \spadfunFrom{nextPrime}{IntegerPrimesPackage} returns the
275least prime number greater than its argument.
276}{
277\spadpaste{nextPrime 100}
278}
279\xtc{
280The operation
281\spadfunFrom{prevPrime}{IntegerPrimesPackage} returns the greatest prime
282number less than its argument.
283}{
284\spadpaste{prevPrime 100}
285}
286\xtc{
287To compute all primes between two integers (inclusively), use the
288operation \spadfunFrom{primes}{IntegerPrimesPackage}.
289}{
290\spadpaste{primes(100,175)}
291}
292\xtc{
293You might sometimes want to see the factorization of an integer
294when it is considered a {\it Gaussian integer}.
295%-% \HDindex{Gaussian integer}{ugxIntegerPrimesPage}{9.38.2.}{Primes and Factorization}
296See \downlink{`Complex'}{ComplexXmpPage}\ignore{Complex} for more details.
297}{
298\spadpaste{factor(2 :: Complex Integer)}
299}
300
301\endscroll
302\autobuttons
303\end{page}
304%
305%
306\newcommand{\ugxIntegerNTTitle}{Some Number Theoretic Functions}
307\newcommand{\ugxIntegerNTNumber}{9.38.3.}
308%
309% =====================================================================
310\begin{page}{ugxIntegerNTPage}{9.38.3. Some Number Theoretic Functions}
311% =====================================================================
312\beginscroll
313
314\Language{} provides several number theoretic operations for integers.
315More examples are in \downlink{`IntegerNumberTheoryFunctions'}{IntegerNumberTheoryFunctionsXmpPage}\ignore{IntegerNumberTheoryFunctions}.
316
317\xtc{
318The operation \spadfunFrom{fibonacci}{IntegerNumberTheoryFunctions}
319computes the Fibonacci numbers.
320%-% \HDindex{Fibonacci numbers}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
321The algorithm has running time
322O(\spad{log(n)^3}) for argument \spad{n}.
323}{
324\spadpaste{[fibonacci(k) for k in 0..]}
325}
326\xtc{
327The operation \spadfunFrom{legendre}{IntegerNumberTheoryFunctions}
328computes the Legendre symbol for its two integer arguments where the
329second one is prime.
330If you know the second argument to be prime, use
331\spadfunFrom{jacobi}{IntegerNumberTheoryFunctions} instead where no check
332is made.
333}{
334\spadpaste{[legendre(i,11) for i in 0..10]}
335}
336\xtc{
337The operation \spadfunFrom{jacobi}{IntegerNumberTheoryFunctions} computes
338the Jacobi symbol for its two integer arguments.
339%-% \HDindex{Jacobi symbol}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
340By convention, \spad{0} is returned if the greatest common divisor of the
341numerator and denominator is not \spad{1}.
342}{
343\spadpaste{[jacobi(i,15) for i in 0..9]}
344}
345\xtc{
346The operation \spadfunFrom{eulerPhi}{IntegerNumberTheoryFunctions} computes
347%-% \HDindex{Euler!phi function@{$\varphi$ function}}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
348the values of Euler's phi-function where
349\spad{phi(n)}
350equals the number of positive integers
351less than or equal to \spad{n} that are relatively prime to
352the positive integer \spad{n}.
353%-% \HDindex{phi@{$\varphi$}}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
354}{
355\spadpaste{[eulerPhi i for i in 1..]}
356}
357\xtc{
358The operation \spadfunFrom{moebiusMu}{IntegerNumberTheoryFunctions}
359%-% \HDindex{mu@{$\mu$}}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
360computes the Moebius mu function.
361%-% \HDindex{Moebius@{M\"{o}bius}!mu function@{$\mu$ function}}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
362}{
363\spadpaste{[moebiusMu i for i in 1..]}
364}
365
366\xtc{
367Although they have somewhat limited utility, \Language{} provides
368%-% \HDindex{Roman numerals}{ugxIntegerNTPage}{9.38.3.}{Some Number Theoretic Functions}
369Roman numerals.
370}{
371\spadpaste{a := roman(78) \bound{a}}
372}
373\xtc{
374}{
375\spadpaste{b := roman(87) \bound{b}}
376}
377\xtc{
378}{
379\spadpaste{a + b \free{a}\free{b}}
380}
381\xtc{
382}{
383\spadpaste{a * b \free{a}\free{b}}
384}
385\xtc{
386}{
387\spadpaste{b rem a \free{a}\free{b}}
388}
389\endscroll
390\autobuttons
391\end{page}
392%
393