aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/pages/INT.ht
blob: ac092b41345701ad501a5a742d6933dcc16b4248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\IntegerXmpTitle}{Integer}
\newcommand{\IntegerXmpNumber}{9.34}
%
% =====================================================================
\begin{page}{IntegerXmpPage}{9.34 Integer}
% =====================================================================
\beginscroll

%% int.htex
%%
%% Integer examples
%%

\Language{} provides many operations for manipulating arbitrary
precision integers.
In this section we will show some of those that come from \spadtype{Integer}
itself plus some that are implemented in other packages.
More examples of using integers are in the following sections:
\downlink{``\ugIntroNumbersTitle''}{ugIntroNumbersPage} in section \ugIntroNumbersNumber
\downlink{`IntegerNumberTheoryFunctions'}{IntegerNumberTheoryFunctionsXmpPage}\ignore{IntegerNumberTheoryFunctions},
\downlink{`DecimalExpansion'}{DecimalExpansionXmpPage}\ignore{DecimalExpansion},
\downlink{`BinaryExpansion'}{BinaryExpansionXmpPage}\ignore{BinaryExpansion},
\downlink{`HexadecimalExpansion'}{HexadecimalExpansionXmpPage}\ignore{HexadecimalExpansion}, and
\downlink{`RadixExpansion'}{RadixExpansionXmpPage}\ignore{RadixExpansion}.

\beginmenu
    \menudownlink{{9.34.1. Basic Functions}}{ugxIntegerBasicPage}
    \menudownlink{{9.34.2. Primes and Factorization}}{ugxIntegerPrimesPage}
    \menudownlink{{9.34.3. Some Number Theoretic Functions}}{ugxIntegerNTPage}
\endmenu
\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugxIntegerBasicTitle}{Basic Functions}
\newcommand{\ugxIntegerBasicNumber}{9.34.1.}
%
% =====================================================================
\begin{page}{ugxIntegerBasicPage}{9.34.1. Basic Functions}
% =====================================================================
\beginscroll

\labelSpace{3pc}
\xtc{
The size of an integer in \Language{} is only limited by the amount of
computer storage you have available.
The usual arithmetic operations are available.
}{
\spadpaste{2**(5678 - 4856 + 2 * 17)}
}

\xtc{
There are a number of ways of working with the sign of an integer.
Let's use this \spad{x} as an example.
}{
\spadpaste{x := -101 \bound{x}}
}
\xtc{
First of all, there is the absolute value function.
}{
\spadpaste{abs(x) \free{x}}
}
\xtc{
The \spadfunFrom{sign}{Integer} operation returns \spad{-1} if its argument
is negative, \spad{0} if zero and \spad{1} if positive.
}{
\spadpaste{sign(x) \free{x}}
}
%
\xtc{
You can determine if an integer is negative in several other ways.
}{
\spadpaste{x < 0 \free{x}}
}
\xtc{
}{
\spadpaste{x <= -1 \free{x}}
}
\xtc{
}{
\spadpaste{negative?(x) \free{x}}
}
%
\xtc{
Similarly, you can find out if it is positive.
}{
\spadpaste{x > 0 \free{x}}
}
\xtc{
}{
\spadpaste{x >= 1 \free{x}}
}
\xtc{
}{
\spadpaste{positive?(x) \free{x}}
}
\xtc{
This is the recommended way of determining whether an integer is zero.
}{
\spadpaste{zero?(x) \free{x}}
}
%

\beginImportant
Use the \spadfunFrom{zero?}{Integer} operation whenever you are testing any
mathematical object for equality with zero.
This is usually more efficient that using \spadop{=} (think of matrices:
it is easier to tell if a matrix is zero by just checking term by term
than constructing another ``zero'' matrix and comparing the two
matrices term by term) and also avoids the problem that \spadop{=} is
usually used for creating equations.
\endImportant

\xtc{
This is the recommended way of determining
whether an integer is equal to one.
}{
\spadpaste{one?(x) \free{x}}
}

\xtc{
This syntax is used to test equality using \spadopFrom{=}{Integer}.
It says that you want a \spadtype{Boolean} (\spad{true} or
\spad{false}) answer rather than an equation.
}{
\spadpaste{(x = -101)@Boolean \free{x}}
}

\xtc{
The operations \spadfunFrom{odd?}{Integer} and
\spadfunFrom{even?}{Integer} determine whether an integer is odd or even,
respectively.
They each return a \spadtype{Boolean} object.
}{
\spadpaste{odd?(x) \free{x}}
}
\xtc{
}{
\spadpaste{even?(x) \free{x}}
}
\xtc{
The operation \spadfunFrom{gcd}{Integer} computes the greatest common divisor of
two integers.
}{
\spadpaste{gcd(56788,43688)}
}
\xtc{
The operation
\spadfunFrom{lcm}{Integer} computes their least common multiple.
}{
\spadpaste{lcm(56788,43688)}
}
\xtc{
To determine the maximum of two integers, use \spadfunFrom{max}{Integer}.
}{
\spadpaste{max(678,567)}
}
\xtc{
To determine the minimum, use \spadfunFrom{min}{Integer}.
}{
\spadpaste{min(678,567)}
}

\xtc{
The \spadfun{reduce} operation is used to extend
binary operations to more than two arguments.
For example, you can use \spadfun{reduce} to find the maximum integer in
a list or compute the least common multiple of all integers in the list.
}{
\spadpaste{reduce(max,[2,45,-89,78,100,-45])}
}
\xtc{
}{
\spadpaste{reduce(min,[2,45,-89,78,100,-45])}
}
\xtc{
}{
\spadpaste{reduce(gcd,[2,45,-89,78,100,-45])}
}
\xtc{
}{
\spadpaste{reduce(lcm,[2,45,-89,78,100,-45])}
}

\xtc{
The infix operator ``/'' is {\it not} used to compute the quotient
of integers.
Rather, it is used to create rational numbers as described in
\downlink{`Fraction'}{FractionXmpPage}\ignore{Fraction}.
}{
\spadpaste{13 / 4}
}
\xtc{
The infix operation \spadfunFrom{quo}{Integer} computes the integer
quotient.
}{
\spadpaste{13 quo 4}
}
\xtc{
The infix operation \spadfunFrom{rem}{Integer} computes the integer
remainder.
}{
\spadpaste{13 rem 4}
}
\xtc{
One integer is evenly divisible by another if the remainder is zero.
The operation \spadfunFrom{exquo}{Integer} can also be used.
See \downlink{``\ugTypesUnionsTitle''}{ugTypesUnionsPage} in Section \ugTypesUnionsNumber\ignore{ugTypesUnions} for an example.
}{
\spadpaste{zero?(167604736446952 rem 2003644)}
}

\xtc{
The operation \spadfunFrom{divide}{Integer} returns a record of the
quotient and remainder and thus is more efficient when both are needed.
}{
\spadpaste{d := divide(13,4) \bound{d}}
}
\xtc{
}{
\spadpaste{d.quotient \free{d}}
}
\xtc{
Records are discussed in detail in \downlink{``\ugTypesRecordsTitle''}{ugTypesRecordsPage} in Section \ugTypesRecordsNumber\ignore{ugTypesRecords}.
}{
\spadpaste{d.remainder \free{d}}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugxIntegerPrimesTitle}{Primes and Factorization}
\newcommand{\ugxIntegerPrimesNumber}{9.34.2.}
%
% =====================================================================
\begin{page}{ugxIntegerPrimesPage}{9.34.2. Primes and Factorization}
% =====================================================================
\beginscroll

\labelSpace{3pc}
\xtc{
Use the operation \spadfunFrom{factor}{Integer} to factor integers.
It returns an object of type \spadtype{Factored Integer}.
%-% \HDindex{factorization}{ugxIntegerPrimesPage}{9.34.2.}{Primes and Factorization}
See \downlink{`Factored'}{FactoredXmpPage}\ignore{Factored} for a discussion of the
manipulation of factored objects.
}{
\spadpaste{factor 102400}
}

\xtc{
The operation \spadfunFrom{prime?}{Integer} returns \spad{true} or \spad{false} depending
on whether its argument is a prime.
%-% \HDindex{prime}{ugxIntegerPrimesPage}{9.34.2.}{Primes and Factorization}
}{
\spadpaste{prime? 7}
}
\xtc{
}{
\spadpaste{prime? 8}
}
\xtc{
The operation \spadfunFrom{nextPrime}{IntegerPrimesPackage} returns the
least prime number greater than its argument.
}{
\spadpaste{nextPrime 100}
}
\xtc{
The operation
\spadfunFrom{prevPrime}{IntegerPrimesPackage} returns the greatest prime
number less than its argument.
}{
\spadpaste{prevPrime 100}
}
\xtc{
To compute all primes between two integers (inclusively), use the
operation \spadfunFrom{primes}{IntegerPrimesPackage}.
}{
\spadpaste{primes(100,175)}
}
\xtc{
You might sometimes want to see the factorization of an integer
when it is considered a {\it Gaussian integer}.
%-% \HDindex{Gaussian integer}{ugxIntegerPrimesPage}{9.34.2.}{Primes and Factorization}
See \downlink{`Complex'}{ComplexXmpPage}\ignore{Complex} for more details.
}{
\spadpaste{factor(2 :: Complex Integer)}
}

\endscroll
\autobuttons
\end{page}
%
%
\newcommand{\ugxIntegerNTTitle}{Some Number Theoretic Functions}
\newcommand{\ugxIntegerNTNumber}{9.34.3.}
%
% =====================================================================
\begin{page}{ugxIntegerNTPage}{9.34.3. Some Number Theoretic Functions}
% =====================================================================
\beginscroll

\Language{} provides several number theoretic operations for integers.
More examples are in \downlink{`IntegerNumberTheoryFunctions'}{IntegerNumberTheoryFunctionsXmpPage}\ignore{IntegerNumberTheoryFunctions}.

\labelSpace{1pc}
\xtc{
The operation \spadfunFrom{fibonacci}{IntegerNumberTheoryFunctions}
computes the Fibonacci numbers.
%-% \HDindex{Fibonacci numbers}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
The algorithm has running time
\texht{$O\,(\log^3(n))$}{O(\spad{log(n)**3})} for argument \spad{n}.
}{
\spadpaste{[fibonacci(k) for k in 0..]}
}
\xtc{
The operation \spadfunFrom{legendre}{IntegerNumberTheoryFunctions}
computes the Legendre symbol for its two integer arguments where the
second one is prime.
If you know the second argument to be prime, use
\spadfunFrom{jacobi}{IntegerNumberTheoryFunctions} instead where no check
is made.
}{
\spadpaste{[legendre(i,11) for i in 0..10]}
}
\xtc{
The operation \spadfunFrom{jacobi}{IntegerNumberTheoryFunctions} computes
the Jacobi symbol for its two integer arguments.
%-% \HDindex{Jacobi symbol}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
By convention, \spad{0} is returned if the greatest common divisor of the
numerator and denominator is not \spad{1}.
}{
\spadpaste{[jacobi(i,15) for i in 0..9]}
}
\xtc{
The operation \spadfunFrom{eulerPhi}{IntegerNumberTheoryFunctions} computes
%-% \HDindex{Euler!phi function@{$\varphi$ function}}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
the values of Euler's \texht{$\phi$}{phi}-function where
\texht{$\phi(n)$}{\spad{phi(n)}}
equals the number of positive integers
less than or equal to \spad{n} that are relatively prime to
the positive integer \spad{n}.
%-% \HDindex{phi@{$\varphi$}}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
}{
\spadpaste{[eulerPhi i for i in 1..]}
}
\xtc{
The operation \spadfunFrom{moebiusMu}{IntegerNumberTheoryFunctions}
%-% \HDindex{mu@{$\mu$}}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
computes the \texht{M\"{o}bius $\mu$}{Moebius mu} function.
%-% \HDindex{Moebius@{M\"{o}bius}!mu function@{$\mu$ function}}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
}{
\spadpaste{[moebiusMu i for i in 1..]}
}

\xtc{
Although they have somewhat limited utility, \Language{} provides
%-% \HDindex{Roman numerals}{ugxIntegerNTPage}{9.34.3.}{Some Number Theoretic Functions}
Roman numerals.
}{
\spadpaste{a := roman(78) \bound{a}}
}
\xtc{
}{
\spadpaste{b := roman(87) \bound{b}}
}
\xtc{
}{
\spadpaste{a + b \free{a}\free{b}}
}
\xtc{
}{
\spadpaste{a * b \free{a}\free{b}}
}
\xtc{
}{
\spadpaste{b rem a \free{a}\free{b}}
}
\endscroll
\autobuttons
\end{page}
%