Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / ref / chap14.txt
Views: 415153
1
2
14 Integers
3
4
One of the most fundamental datatypes in every programming language is the
5
integer type. GAP is no exception.
6
7
GAP integers are entered as a sequence of decimal digits optionally preceded
8
by a + sign for positive integers or a - sign for negative integers. The
9
size of integers in GAP is only limited by the amount of available memory,
10
so you can compute with integers having thousands of digits.
11
12
 Example 
13
gap> -1234;
14
-1234
15
gap> 123456789012345678901234567890123456789012345678901234567890;
16
123456789012345678901234567890123456789012345678901234567890
17

18
19
Many more functions that are mainly related to the prime residue group of
20
integers modulo an integer are described in chapter 15, and functions
21
dealing with combinatorics can be found in chapter 16.
22
23
24
14.1 Integers: Global Variables
25
26
14.1-1 Integers
27
28
Integers global variable
29
PositiveIntegers global variable
30
NonnegativeIntegers global variable
31
32
These global variables represent the ring of integers and the semirings of
33
positive and nonnegative integers, respectively.
34
35
 Example 
36
gap> Size( Integers ); 2 in Integers;
37
infinity
38
true
39

40
41
Integers is a subset of Rationals (17.1-1), which is a subset of Cyclotomics
42
(18.1-2). See Chapter 18 for arithmetic operations and comparison of
43
integers.
44
45
14.1-2 IsIntegers
46
47
IsIntegers( obj )  Category
48
IsPositiveIntegers( obj )  Category
49
IsNonnegativeIntegers( obj )  Category
50
51
are the defining categories for the domains Integers (14.1-1),
52
PositiveIntegers (14.1-1), and NonnegativeIntegers (14.1-1).
53
54
 Example 
55
gap> IsIntegers( Integers ); IsIntegers( Rationals ); IsIntegers( 7 );
56
true
57
false
58
false
59

60
61
62
14.2 Elementary Operations for Integers
63
64
14.2-1 IsInt
65
66
IsInt( obj )  Category
67
68
Every rational integer lies in the category IsInt, which is a subcategory of
69
IsRat (17.2-1).
70
71
14.2-2 IsPosInt
72
73
IsPosInt( obj )  Category
74
75
Every positive integer lies in the category IsPosInt.
76
77
14.2-3 Int
78
79
Int( elm )  attribute
80
81
Int returns an integer int whose meaning depends on the type of elm.
82
83
If elm is a rational number (see Chapter 17) then int is the integer part of
84
the quotient of numerator and denominator of elm (see QuoInt (14.3-1)).
85
86
If elm is an element of a finite prime field (see Chapter 59) then int is
87
the smallest nonnegative integer such that elm = int * One( elm ).
88
89
If elm is a string (see Chapter 27) consisting of digits '0', '1', ..., '9'
90
and '-' (at the first position) then int is the integer described by this
91
string. The operation String (27.7-6) can be used to compute a string for
92
rational integers, in fact for all cyclotomics.
93
94
 Example 
95
gap> Int( 4/3 ); Int( -2/3 );
96
1
97
0
98
gap> int:= Int( Z(5) ); int * One( Z(5) );
99
2
100
Z(5)
101
gap> Int( "12345" ); Int( "-27" ); Int( "-27/3" );
102
12345
103
-27
104
fail
105

106
107
14.2-4 IsEvenInt
108
109
IsEvenInt( n )  function
110
111
tests if the integer n is divisible by 2.
112
113
14.2-5 IsOddInt
114
115
IsOddInt( n )  function
116
117
tests if the integer n is not divisible by 2.
118
119
14.2-6 AbsInt
120
121
AbsInt( n )  function
122
123
AbsInt returns the absolute value of the integer n, i.e., n if n is
124
positive, -n if n is negative and 0 if n is 0.
125
126
AbsInt is a special case of the general operation EuclideanDegree (56.6-2).
127
128
See also AbsoluteValue (18.1-8).
129
130
 Example 
131
gap> AbsInt( 33 );
132
33
133
gap> AbsInt( -214378 );
134
214378
135
gap> AbsInt( 0 );
136
0
137

138
139
14.2-7 SignInt
140
141
SignInt( n )  function
142
143
SignInt returns the sign of the integer n, i.e., 1 if n is positive, -1 if n
144
is negative and 0 if n is 0.
145
146
 Example 
147
gap> SignInt( 33 );
148
1
149
gap> SignInt( -214378 );
150
-1
151
gap> SignInt( 0 );
152
0
153

154
155
14.2-8 LogInt
156
157
LogInt( n, base )  function
158
159
LogInt returns the integer part of the logarithm of the positive integer n
160
with respect to the positive integer base, i.e., the largest positive
161
integer e such that base^e ≤ n. The function LogInt will signal an error if
162
either n or base is not positive.
163
164
For base = 2 this is very efficient because the internal binary
165
representation of the integer is used.
166
167
 Example 
168
gap> LogInt( 1030, 2 );
169
10
170
gap> 2^10;
171
1024
172
gap> LogInt( 1, 10 );
173
0
174

175
176
14.2-9 RootInt
177
178
RootInt( n[, k] )  function
179
180
RootInt returns the integer part of the kth root of the integer n. If the
181
optional integer argument k is not given it defaults to 2, i.e., RootInt
182
returns the integer part of the square root in this case.
183
184
If n is positive, RootInt returns the largest positive integer r such that
185
r^k ≤ n. If n is negative and k is odd RootInt returns -RootInt( -n, k ). If
186
n is negative and k is even RootInt will cause an error. RootInt will also
187
cause an error if k is 0 or negative.
188
189
 Example 
190
gap> RootInt( 361 );
191
19
192
gap> RootInt( 2 * 10^12 );
193
1414213
194
gap> RootInt( 17000, 5 );
195
7
196
gap> 7^5;
197
16807
198

199
200
14.2-10 SmallestRootInt
201
202
SmallestRootInt( n )  function
203
204
SmallestRootInt returns the smallest root of the integer n.
205
206
The smallest root of an integer n is the integer r of smallest absolute
207
value for which a positive integer k exists such that n = r^k.
208
209
 Example 
210
gap> SmallestRootInt( 2^30 );
211
2
212
gap> SmallestRootInt( -(2^30) );
213
-4
214

215
216
Note that (-2)^30 = +(2^30).
217
218
 Example 
219
gap> SmallestRootInt( 279936 );
220
6
221
gap> LogInt( 279936, 6 );
222
7
223
gap> SmallestRootInt( 1001 );
224
1001
225

226
227
14.2-11 ListOfDigits
228
229
ListOfDigits( n )  function
230
231
For a positive integer n this function returns a list l, consisting of the
232
digits of n in decimal representation.
233
234
 Example 
235
gap> ListOfDigits(3142); 
236
[ 3, 1, 4, 2 ]
237

238
239
14.2-12 Random
240
241
Random( Integers )  method
242
243
Random for integers returns pseudo random integers between -10 and 10
244
distributed according to a binomial distribution. To generate uniformly
245
distributed integers from a range, use the construction Random( [ low ..
246
high ] )  (see Random (30.7-1)).
247
248
249
14.3 Quotients and Remainders
250
251
14.3-1 QuoInt
252
253
QuoInt( n, m )  function
254
255
QuoInt returns the integer part of the quotient of its integer operands.
256
257
If n and m are positive, QuoInt returns the largest positive integer q such
258
that q * m ≤ n. If n or m or both are negative the absolute value of the
259
integer part of the quotient is the quotient of the absolute values of n and
260
m, and the sign of it is the product of the signs of n and m.
261
262
QuoInt is used in a method for the general operation EuclideanQuotient
263
(56.6-3).
264
265
 Example 
266
gap> QuoInt(5,3); QuoInt(-5,3); QuoInt(5,-3); QuoInt(-5,-3);
267
1
268
-1
269
-1
270
1
271

272
273
14.3-2 BestQuoInt
274
275
BestQuoInt( n, m )  function
276
277
BestQuoInt returns the best quotient q of the integers n and m. This is the
278
quotient such that n-q*m has minimal absolute value. If there are two
279
quotients whose remainders have the same absolute value, then the quotient
280
with the smaller absolute value is chosen.
281
282
 Example 
283
gap> BestQuoInt( 5, 3 ); BestQuoInt( -5, 3 );
284
2
285
-2
286

287
288
14.3-3 RemInt
289
290
RemInt( n, m )  function
291
292
RemInt returns the remainder of its two integer operands.
293
294
If m is not equal to zero, RemInt returns n - m * QuoInt( n, m ). Note that
295
the rules given for QuoInt (14.3-1) imply that the return value of RemInt
296
has the same sign as n and its absolute value is strictly less than the
297
absolute value of m. Note also that the return value equals n mod m when
298
both n and m are nonnegative. Dividing by 0 signals an error.
299
300
RemInt is used in a method for the general operation EuclideanRemainder
301
(56.6-4).
302
303
 Example 
304
gap> RemInt(5,3); RemInt(-5,3); RemInt(5,-3); RemInt(-5,-3);
305
2
306
-2
307
2
308
-2
309

310
311
14.3-4 GcdInt
312
313
GcdInt( m, n )  function
314
315
GcdInt returns the greatest common divisor of its two integer operands m and
316
n, i.e., the greatest integer that divides both m and n. The greatest common
317
divisor is never negative, even if the arguments are. We define GcdInt( m, 0
318
) = GcdInt( 0, m ) = AbsInt( m ) and GcdInt( 0, 0 ) = 0.
319
320
GcdInt is a method used by the general function Gcd (56.7-1).
321
322
 Example 
323
gap> GcdInt( 123, 66 );
324
3
325

326
327
14.3-5 Gcdex
328
329
Gcdex( m, n )  function
330
331
returns a record g describing the extended greatest common divisor of m and
332
n. The component gcd is this gcd, the components coeff1 and coeff2 are
333
integer cofactors such that g.gcd = g.coeff1 * m + g.coeff2 * n, and the
334
components coeff3 and coeff4 are integer cofactors such that 0 = g.coeff3 *
335
m + g.coeff4 * n.
336
337
If m and n both are nonzero, AbsInt( g.coeff1 ) is less than or equal to
338
AbsInt(n) / (2 * g.gcd), and AbsInt( g.coeff2 ) is less than or equal to
339
AbsInt(m) / (2 * g.gcd).
340
341
If m or n or both are zero coeff3 is -n / g.gcd and coeff4 is m / g.gcd.
342
343
The coefficients always form a unimodular matrix, i.e., the determinant
344
g.coeff1 * g.coeff4 - g.coeff3 * g.coeff2 is 1 or -1.
345
346
 Example 
347
gap> Gcdex( 123, 66 );
348
rec( coeff1 := 7, coeff2 := -13, coeff3 := -22, coeff4 := 41, 
349
 gcd := 3 )
350

351
352
This means 3 = 7 * 123 - 13 * 66, 0 = -22 * 123 + 41 * 66.
353
354
 Example 
355
gap> Gcdex( 0, -3 );
356
rec( coeff1 := 0, coeff2 := -1, coeff3 := 1, coeff4 := 0, gcd := 3 )
357
gap> Gcdex( 0, 0 );
358
rec( coeff1 := 1, coeff2 := 0, coeff3 := 0, coeff4 := 1, gcd := 0 )
359

360
361
GcdRepresentation (56.7-3) provides similar functionality over arbitrary
362
Euclidean rings.
363
364
14.3-6 LcmInt
365
366
LcmInt( m, n )  function
367
368
returns the least common multiple of the integers m and n.
369
370
LcmInt is a method used by the general operation Lcm (56.7-6).
371
372
 Example 
373
gap> LcmInt( 123, 66 );
374
2706
375

376
377
14.3-7 CoefficientsQadic
378
379
CoefficientsQadic( i, q )  operation
380
381
returns the q-adic representation of the integer i as a list l of
382
coefficients satisfying the equality i = ∑_{j = 0} q^j ⋅ l[j+1] for an
383
integer q > 1.
384
385
 Example 
386
gap> l:=CoefficientsQadic(462,3);
387
[ 0, 1, 0, 2, 2, 1 ]
388

389
390
14.3-8 CoefficientsMultiadic
391
392
CoefficientsMultiadic( ints, int )  function
393
394
returns the multiadic expansion of the integer int modulo the integers given
395
in ints (in ascending order). It returns a list of coefficients in the
396
reverse order to that in ints.
397
398
14.3-9 ChineseRem
399
400
ChineseRem( moduli, residues )  function
401
402
ChineseRem returns the combination of the residues modulo the moduli, i.e.,
403
the unique integer c from [0..Lcm(moduli)-1] such that c = residues[i]
404
modulo moduli[i] for all i, if it exists. If no such combination exists
405
ChineseRem signals an error.
406
407
Such a combination does exist if and only if residues[i] = residues[k] mod
408
Gcd( moduli[i], moduli[k] ) for every pair i, k. Note that this implies that
409
such a combination exists if the moduli are pairwise relatively prime. This
410
is called the Chinese remainder theorem.
411
412
 Example 
413
gap> ChineseRem( [ 2, 3, 5, 7 ], [ 1, 2, 3, 4 ] );
414
53
415
gap> ChineseRem( [ 6, 10, 14 ], [ 1, 3, 5 ] );
416
103
417

418
419
 Example 
420
gap> ChineseRem( [ 6, 10, 14 ], [ 1, 2, 3 ] );
421
Error, the residues must be equal modulo 2 called from
422
<function>( <arguments> ) called from read-eval-loop
423
Entering break read-eval-print loop ...
424
you can 'quit;' to quit to outer loop, or
425
you can 'return;' to continue
426
brk> gap> 
427

428
429
14.3-10 PowerModInt
430
431
PowerModInt( r, e, m )  function
432
433
returns r^e mod m for integers r, e and m (e ≥ 0).
434
435
Note that PowerModInt can reduce intermediate results and thus will
436
generally be faster than using r^e mod m, which would compute r^e first and
437
reduces the result afterwards.
438
439
PowerModInt is a method for the general operation PowerMod (56.7-9).
440
441
442
14.4 Prime Integers and Factorization
443
444
14.4-1 Primes
445
446
Primes global variable
447
448
Primes is a strictly sorted list of the 168 primes less than 1000.
449
450
This is used in IsPrimeInt (14.4-2) and FactorsInt (14.4-7) to cast out
451
small primes quickly.
452
453
 Example 
454
gap> Primes[1];
455
2
456
gap> Primes[100];
457
541
458

459
460
14.4-2 IsPrimeInt
461
462
IsPrimeInt( n )  function
463
IsProbablyPrimeInt( n )  function
464
465
IsPrimeInt returns false if it can prove that the integer n is composite and
466
true otherwise. By convention IsPrimeInt(0) = IsPrimeInt(1) = false and we
467
define IsPrimeInt(-n) = IsPrimeInt(n).
468
469
IsPrimeInt will return true for every prime n. IsPrimeInt will return false
470
for all composite n < 10^18 and for all composite n that have a factor p <
471
1000. So for integers n < 10^18, IsPrimeInt is a proper primality test. It
472
is conceivable that IsPrimeInt may return true for some composite n > 10^18,
473
but no such n is currently known. So for integers n > 10^18, IsPrimeInt is a
474
probable-primality test. IsPrimeInt will issue a warning when its argument
475
is probably prime but not a proven prime. (The function IsProbablyPrimeInt
476
will do a similar calculation but not issue a warning.) The warning can be
477
switched off by SetInfoLevel( InfoPrimeInt, 0 );, the default level is 1
478
(also see SetInfoLevel (7.4-3) ).
479
480
If composites that fool IsPrimeInt do exist, they would be extremely rare,
481
and finding one by pure chance might be less likely than finding a bug in
482
GAP. We would appreciate being informed about any example of a composite
483
number n for which IsPrimeInt returns true.
484
485
IsPrimeInt is a deterministic algorithm, i.e., the computations involve no
486
random numbers, and repeated calls will always return the same result.
487
IsPrimeInt first does trial divisions by the primes less than 1000. Then it
488
tests that n is a strong pseudoprime w.r.t. the base 2. Finally it tests
489
whether n is a Lucas pseudoprime w.r.t. the smallest quadratic nonresidue of
490
n. A better description can be found in the comment in the library file
491
primality.gi.
492
493
The time taken by IsPrimeInt is approximately proportional to the third
494
power of the number of digits of n. Testing numbers with several hundreds
495
digits is quite feasible.
496
497
IsPrimeInt is a method for the general operation IsPrime (56.5-8).
498
499
Remark: In future versions of GAP we hope to change the definition of
500
IsPrimeInt to return true only for proven primes (currently, we lack a
501
sufficiently good primality proving function). In applications, use
502
explicitly IsPrimeInt or IsProbablyPrimeInt with this change in mind.
503
504
 Example 
505
gap> IsPrimeInt( 2^31 - 1 );
506
true
507
gap> IsPrimeInt( 10^42 + 1 );
508
false
509

510
511
14.4-3 PrimalityProof
512
513
PrimalityProof( n )  function
514
515
Construct a machine verifiable proof of the primality of (the probable
516
prime) n, following the ideas of [BLS75]. The proof consists of various
517
Fermat and Lucas pseudoprimality tests, which taken as a whole prove the
518
primality. The proof is represented as a list of witnesses of two kinds. The
519
first kind, [ "F", divisor, base ], indicates a successful Fermat
520
pseudoprimality test, where n is a strong pseudoprime at base with order not
521
divisible by (n-1)/divisor. The second kind, [ "L", divisor, discriminant, P
522
] indicates a successful Lucas pseudoprimality test, for a quadratic form of
523
given discriminant and middle term P with an extra check at (n+1)/divisor.
524
525
14.4-4 IsPrimePowerInt
526
527
IsPrimePowerInt( n )  function
528
529
IsPrimePowerInt returns true if the integer n is a prime power and false
530
otherwise.
531
532
An integer n is a prime power if there exists a prime p and a positive
533
integer i such that p^i = n. If n is negative the condition is that there
534
must exist a negative prime p and an odd positive integer i such that p^i =
535
n. The integers 1 and -1 are not prime powers.
536
537
Note that IsPrimePowerInt uses SmallestRootInt (14.2-10) and a
538
probable-primality test (see IsPrimeInt (14.4-2)).
539
540
 Example 
541
gap> IsPrimePowerInt( 31^5 );
542
true
543
gap> IsPrimePowerInt( 2^31-1 ); # 2^31-1 is actually a prime
544
true
545
gap> IsPrimePowerInt( 2^63-1 );
546
false
547
gap> Filtered( [-10..10], IsPrimePowerInt );
548
[ -8, -7, -5, -3, -2, 2, 3, 4, 5, 7, 8, 9 ]
549

550
551
14.4-5 NextPrimeInt
552
553
NextPrimeInt( n )  function
554
555
NextPrimeInt returns the smallest prime which is strictly larger than the
556
integer n.
557
558
Note that NextPrimeInt uses a probable-primality test (see IsPrimeInt
559
(14.4-2)).
560
561
 Example 
562
gap> NextPrimeInt( 541 ); NextPrimeInt( -1 );
563
547
564
2
565

566
567
14.4-6 PrevPrimeInt
568
569
PrevPrimeInt( n )  function
570
571
PrevPrimeInt returns the largest prime which is strictly smaller than the
572
integer n.
573
574
Note that PrevPrimeInt uses a probable-primality test (see IsPrimeInt
575
(14.4-2)).
576
577
 Example 
578
gap> PrevPrimeInt( 541 ); PrevPrimeInt( 1 );
579
523
580
-2
581

582
583
14.4-7 FactorsInt
584
585
FactorsInt( n )  function
586
FactorsInt( n: RhoTrials := trials )  function
587
588
FactorsInt returns a list of factors of a given integer n such that Product(
589
FactorsInt( n ) ) = n. If |n| ≤ 1 the list [n] is returned. Otherwise the
590
result contains probable primes, sorted by absolute value. The entries will
591
all be positive except for the first one in case of a negative n.
592
593
See PrimeDivisors (14.4-8) for a function that returns a set of (probable)
594
primes dividing n.
595
596
Note that FactorsInt uses a probable-primality test (see IsPrimeInt
597
(14.4-2)). Thus FactorsInt might return a list which contains composite
598
integers. In such a case you will get a warning about the use of a probable
599
prime. You can switch off these warnings by SetInfoLevel( InfoPrimeInt, 0 );
600
(also see SetInfoLevel (7.4-3)).
601
602
The time taken by FactorsInt is approximately proportional to the square
603
root of the second largest prime factor of n, which is the last one that
604
FactorsInt has to find, since the largest factor is simply what remains when
605
all others have been removed. Thus the time is roughly bounded by the fourth
606
root of n. FactorsInt is guaranteed to find all factors less than 10^6 and
607
will find most factors less than 10^10. If n contains multiple factors
608
larger than that FactorsInt may not be able to factor n and will then signal
609
an error.
610
611
FactorsInt is used in a method for the general operation Factors (56.5-9).
612
613
In the second form, FactorsInt calls FactorsRho with a limit of trials on
614
the number of trials it performs. The default is 8192. Note that Pollard's
615
Rho is the fastest method for finding prime factors with roughly 5-10
616
decimal digits, but becomes more and more inferior to other factorization
617
techniques like e.g. the Elliptic Curves Method (ECM) the bigger the prime
618
factors are. Therefore instead of performing a huge number of Rho trials, it
619
is usually more advisable to install the FactInt package and then simply to
620
use the operation Factors (56.5-9). The factorization of the 8-th Fermat
621
number by Pollard's Rho below takes already a while.
622
623
 Example 
624
gap> FactorsInt( -Factorial(6) );
625
[ -2, 2, 2, 2, 3, 3, 5 ]
626
gap> Set( FactorsInt( Factorial(13)/11 ) );
627
[ 2, 3, 5, 7, 13 ]
628
gap> FactorsInt( 2^63 - 1 );
629
[ 7, 7, 73, 127, 337, 92737, 649657 ]
630
gap> FactorsInt( 10^42 + 1 );
631
[ 29, 101, 281, 9901, 226549, 121499449, 4458192223320340849 ]
632
gap> FactorsInt(2^256+1:RhoTrials:=100000000);
633
[ 1238926361552897, 
634
 93461639715357977769163558199606896584051237541638188580280321 ]
635

636
637
14.4-8 PrimeDivisors
638
639
PrimeDivisors( n )  attribute
640
641
PrimeDivisors returns for a non-zero integer n a set of its positive
642
(probable) primes divisors. In rare cases the result could contain a
643
composite number which passed certain primality tests, see
644
IsProbablyPrimeInt (14.4-2) and FactorsInt (14.4-7) for more details.
645
646
 Example 
647
gap> PrimeDivisors(-12);
648
[ 2, 3 ]
649
gap> PrimeDivisors(1);
650
[ ]
651

652
653
14.4-9 PartialFactorization
654
655
PartialFactorization( n[, effort] )  operation
656
657
PartialFactorization returns a partial factorization of the integer n. No
658
assertions are made about the primality of the factors, except of those
659
mentioned below.
660
661
The argument effort, if given, specifies how intensively the function should
662
try to determine factors of n. The default is effort = 5.
663
664
 If effort = 0, trial division by the primes below 100 is done.
665
Returned factors below 10^4 are guaranteed to be prime.
666
667
 If effort = 1, trial division by the primes below 1000 is done.
668
Returned factors below 10^6 are guaranteed to be prime.
669
670
 If effort = 2, additionally trial division by the numbers in the lists
671
Primes2 and ProbablePrimes2 is done, and perfect powers are detected.
672
Returned factors below 10^6 are guaranteed to be prime.
673
674
 If effort = 3, additionally FactorsRho (Pollard's Rho) with RhoTrials
675
= 256 is used.
676
677
 If effort = 4, as above, but RhoTrials = 2048.
678
679
 If effort = 5, as above, but RhoTrials = 8192. Returned factors below
680
10^12 are guaranteed to be prime, and all prime factors below 10^6 are
681
guaranteed to be found.
682
683
 If effort = 6 and the package FactInt is loaded, in addition to the
684
above quite a number of special cases are handled.
685
686
 If effort = 7 and the package FactInt is loaded, the only thing which
687
is not attempted to obtain a full factorization into
688
Baillie-Pomerance-Selfridge-Wagstaff pseudoprimes is the application
689
of the MPQS to a remaining composite with more than 50 decimal digits.
690
691
Increasing the value of the argument effort by one usually results in an
692
increase of the runtime requirements by a factor of (very roughly!) 3 to 10.
693
(Also see CheapFactorsInt (EDIM: CheapFactorsInt)).
694
695
 Example 
696
gap> List([0..5],i->PartialFactorization(97^35-1,i)); 
697
[ [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 
698
 2446338959059521520901826365168917110105972824229555319002965029 ], 
699
 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 
700
 2529823122088440042297648774735177983563570655873376751812787 ],
701
 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 
702
 2529823122088440042297648774735177983563570655873376751812787 ],
703
 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 
704
 242549173950325921859769421435653153445616962914227 ], 
705
 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 687121, 
706
 352993394104278463123335513593170858474150787 ], 
707
 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 687121, 
708
 20241187, 504769301, 34549173843451574629911361501 ] ]
709

710
711
14.4-10 PrintFactorsInt
712
713
PrintFactorsInt( n )  function
714
715
prints the prime factorization of the integer n in human-readable form.
716
717
 Example 
718
gap> PrintFactorsInt( Factorial( 7 ) ); Print( "\n" );
719
2^4*3^2*5*7
720

721
722
14.4-11 PrimePowersInt
723
724
PrimePowersInt( n )  function
725
726
returns the prime factorization of the integer n as a list [ p_1, e_1, ...,
727
p_k, e_k ] with n = p_1^{e_1} ⋅ p_2^{e_2} ⋅ ... ⋅ p_k^{e_k}.
728
729
 Example 
730
gap> PrimePowersInt( Factorial( 7 ) );
731
[ 2, 4, 3, 2, 5, 1, 7, 1 ]
732

733
734
14.4-12 DivisorsInt
735
736
DivisorsInt( n )  function
737
738
DivisorsInt returns a list of all divisors of the integer n. The list is
739
sorted, so that it starts with 1 and ends with n. We define that
740
DivisorsInt( -n ) = DivisorsInt( n ).
741
742
Since the set of divisors of 0 is infinite calling DivisorsInt( 0 ) causes
743
an error.
744
745
DivisorsInt may call FactorsInt (14.4-7) to obtain the prime factors. Sigma
746
(15.5-1) and Tau (15.5-2) compute the sum and the number of positive
747
divisors, respectively.
748
749
 Example 
750
gap> DivisorsInt( 1 ); DivisorsInt( 20 ); DivisorsInt( 541 );
751
[ 1 ]
752
[ 1, 2, 4, 5, 10, 20 ]
753
[ 1, 541 ]
754

755
756
757
14.5 Residue Class Rings
758
759
ZmodnZ (14.5-2) returns a residue class ring of Integers (14) modulo an
760
ideal. These residue class rings are rings, thus all operations for rings
761
(see Chapter 56) apply. See also Chapters 59 and 15.
762
763
14.5-1 \mod
764
765
\mod( r/s, n )  operation
766
767
If r, s and n are integers, r / s as a reduced fraction is p/q, where q and
768
n are coprime, then r / s mod n is defined to be the product of p and the
769
inverse of q modulo n. See Section 4.13 for more details and definitions.
770
771
With the above definition, 4 / 6 mod 32 equals 2 / 3 mod 32 and hence exists
772
(and is equal to 22), despite the fact that 6 has no inverse modulo 32.
773
774
14.5-2 ZmodnZ
775
776
ZmodnZ( n )  function
777
ZmodpZ( p )  function
778
ZmodpZNC( p )  function
779
780
ZmodnZ returns a ring R isomorphic to the residue class ring of the integers
781
modulo the ideal generated by n. The element corresponding to the residue
782
class of the integer i in this ring can be obtained by i * One( R ), and a
783
representative of the residue class corresponding to the element x ∈ R can
784
be computed by Int( x ).
785
786
ZmodnZ( n ) is equal to Integers mod n.
787
788
ZmodpZ does the same if the argument p is a prime integer, additionally the
789
result is a field. ZmodpZNC omits the check whether p is a prime.
790
791
Each ring returned by these functions contains the whole family of its
792
elements if n is not a prime, and is embedded into the family of finite
793
field elements of characteristic n if n is a prime.
794
795
14.5-3 ZmodnZObj
796
797
ZmodnZObj( Fam, r )  operation
798
ZmodnZObj( r, n )  operation
799
800
If the first argument is a residue class family Fam then ZmodnZObj returns
801
the element in Fam whose coset is represented by the integer r.
802
803
If the two arguments are an integer r and a positive integer n then
804
ZmodnZObj returns the element in ZmodnZ( n ) (see ZmodnZ (14.5-2)) whose
805
coset is represented by the integer r.
806
807
 Example 
808
gap> r:= ZmodnZ(15);
809
(Integers mod 15)
810
gap> fam:=ElementsFamily(FamilyObj(r));;
811
gap> a:= ZmodnZObj(fam,9);
812
ZmodnZObj( 9, 15 )
813
gap> a+a;
814
ZmodnZObj( 3, 15 )
815
gap> Int(a+a);
816
3
817

818
819
14.5-4 IsZmodnZObj
820
821
IsZmodnZObj( obj )  Category
822
IsZmodnZObjNonprime( obj )  Category
823
IsZmodpZObj( obj )  Category
824
IsZmodpZObjSmall( obj )  Category
825
IsZmodpZObjLarge( obj )  Category
826
827
The elements in the rings Z / n Z are in the category IsZmodnZObj. If n is a
828
prime then the elements are of course also in the category IsFFE (59.1-1),
829
otherwise they are in IsZmodnZObjNonprime. IsZmodpZObj is an abbreviation of
830
IsZmodnZObj and IsFFE. This category is the disjoint union of
831
IsZmodpZObjSmall and IsZmodpZObjLarge, the former containing all elements
832
with n at most MAXSIZE_GF_INTERNAL.
833
834
The reasons to distinguish the prime case from the nonprime case are
835
836
 that objects in IsZmodnZObjNonprime have an external representation
837
(namely the residue in the range [ 0, 1, ..., n-1 ]),
838
839
 that the comparison of elements can be defined as comparison of the
840
residues, and
841
842
 that the elements lie in a family of type IsZmodnZObjNonprimeFamily
843
(note that for prime n, the family must be an IsFFEFamily).
844
845
The reasons to distinguish the small and the large case are that for small n
846
the elements must be compatible with the internal representation of finite
847
field elements, whereas we are free to define comparison as comparison of
848
residues for large n.
849
850
Note that we cannot claim that every finite field element of degree 1 is in
851
IsZmodnZObj, since finite field elements in internal representation may not
852
know that they lie in the prime field.
853
854
855
14.6 Check Digits
856
857
14.6-1 CheckDigitISBN
858
859
CheckDigitISBN( n )  function
860
CheckDigitISBN13( n )  function
861
CheckDigitPostalMoneyOrder( n )  function
862
CheckDigitUPC( n )  function
863
864
These functions can be used to compute, or check, check digits for some
865
everyday items. In each case what is submitted as input is either the number
866
with check digit (in which case the function returns true or false), or the
867
number without check digit (in which case the function returns the missing
868
check digit). The number can be specified as integer, as string (for example
869
in case of leading zeros) or as a sequence of arguments, each representing a
870
single digit. The check digits tested are the 10-digit ISBN (International
871
Standard Book Number) using CheckDigitISBN (since arithmetic is module 11, a
872
digit 11 is represented by an X); the newer 13-digit ISBN-13 using
873
CheckDigitISBN13; the numbers of 11-digit US postal money orders using
874
CheckDigitPostalMoneyOrder; and the 12-digit UPC bar code found on groceries
875
using CheckDigitUPC.
876
877
 Example 
878
gap> CheckDigitISBN("052166103");
879
Check Digit is 'X'
880
'X'
881
gap> CheckDigitISBN("052166103X");
882
Checksum test satisfied
883
true
884
gap> CheckDigitISBN(0,5,2,1,6,6,1,0,3,1);
885
Checksum test failed
886
false
887
gap> CheckDigitISBN(0,5,2,1,6,6,1,0,3,'X'); # note single quotes!
888
Checksum test satisfied
889
true
890
gap> CheckDigitISBN13("9781420094527");
891
Checksum test satisfied
892
true
893
gap> CheckDigitUPC("07164183001");
894
Check Digit is 1
895
1
896
gap> CheckDigitPostalMoneyOrder(16786457155);
897
Checksum test satisfied
898
true
899

900
901
14.6-2 CheckDigitTestFunction
902
903
CheckDigitTestFunction( l, m, f )  function
904
905
This function creates check digit test functions such as CheckDigitISBN
906
(14.6-1) for check digit schemes that use the inner products with a fixed
907
vector modulo a number. The scheme creates will use strings of l digits
908
(including the check digits), the check consists of taking the standard
909
product of the vector of digits with the fixed vector f modulo m; the result
910
needs to be 0. The function returns a function that then can be used for
911
testing or determining check digits.
912
913
 Example 
914
gap> isbntest:=CheckDigitTestFunction(10,11,[1,2,3,4,5,6,7,8,9,-1]); 
915
function( arg... ) ... end
916
gap> isbntest("038794680");
917
Check Digit is 2
918
2
919

920
921
922
14.7 Random Sources
923
924
GAP provides Random (30.7-1) methods for many collections of objects. On a
925
lower level these methods use random sources which provide random integers
926
and random choices from lists.
927
928
14.7-1 IsRandomSource
929
930
IsRandomSource( obj )  Category
931
932
This is the category of random source objects which are defined to have, for
933
an object rs in this category, methods available for the following
934
operations which are explained in more detail below: Random( rs, list )
935
giving a random element of a list, Random( rs, low, high ) giving a random
936
integer between low and high (inclusive), Init (14.7-3), State (14.7-3) and
937
Reset (14.7-3).
938
939
Use RandomSource (14.7-5) to construct new random sources.
940
941
One idea behind providing several independent (pseudo) random sources is to
942
make algorithms which use some sort of random choices deterministic. They
943
can use their own new random source created with a fixed seed and so do
944
exactly the same in different calls.
945
946
Random source objects lie in the family RandomSourcesFamily.
947
948
14.7-2 Random
949
950
Random( rs, list )  operation
951
Random( rs, low, high )  operation
952
953
This operation returns a random element from list list, or an integer in the
954
range from the given (possibly large) integers low to high, respectively.
955
956
The choice should only depend on the random source rs and have no effect on
957
other random sources.
958
959
 Example 
960
gap> mysource := RandomSource(IsMersenneTwister, 42);;
961
gap> Random(mysource, 1, 10^60);
962
999331861769949319194941485000557997842686717712198687315183
963

964
965
14.7-3 State
966
967
State( rs )  operation
968
Reset( rs[, seed] )  operation
969
Init( prers[, seed] )  operation
970
971
These are the basic operations for which random sources (see IsRandomSource
972
(14.7-1)) must have methods.
973
974
State should return a data structure which allows to recover the state of
975
the random source such that a sequence of random calls using this random
976
source can be reproduced. If a random source cannot be reset (say, it uses
977
truly random physical data) then State should return fail.
978
979
Reset( rs, seed ) resets the random source rs to a state described by seed,
980
if the random source can be reset (otherwise it should do nothing). Here
981
seed can be an output of State and then should reset to that state. Also,
982
the methods should always allow integers as seed. Without the seed argument
983
the default seed = 1 is used.
984
985
Init is the constructor of a random source, it gets an empty component
986
object prers which has already the correct type and should fill in the
987
actual data which are needed. Optionally, it should allow one to specify a
988
seed for the initial state, as explained for Reset.
989
990
Most methods for Random (30.7-1) in the GAP library use the
991
GlobalMersenneTwister (14.7-4) as random source. It can be reset into a
992
known state as in the following example.
993
994
 Example 
995
gap> seed := State(GlobalMersenneTwister);;
996
gap> List([1..10],i->Random(Integers));
997
[ 2, -1, -2, -1, -1, 1, -4, 1, 0, -1 ]
998
gap> List([1..10],i->Random(Integers));
999
[ -1, -1, 1, -1, 1, -2, -1, -2, 0, -1 ]
1000
gap> Reset(GlobalMersenneTwister, seed);;
1001
gap> List([1..10],i->Random(Integers));
1002
[ 2, -1, -2, -1, -1, 1, -4, 1, 0, -1 ]
1003

1004
1005
14.7-4 IsMersenneTwister
1006
1007
IsMersenneTwister( rs )  Category
1008
IsGAPRandomSource( rs )  Category
1009
IsGlobalRandomSource( rs )  Category
1010
GlobalMersenneTwister global variable
1011
GlobalRandomSource global variable
1012
1013
Currently, the GAP library provides three types of random sources,
1014
distinguished by the three listed categories.
1015
1016
IsMersenneTwister are random sources which use a fast random generator of 32
1017
bit numbers, called the Mersenne twister. The pseudo random sequence has a
1018
period of 2^19937-1 and the numbers have a 623-dimensional equidistribution.
1019
For more details and the origin of the code used in the GAP kernel, see:
1020
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html.
1021
1022
Use the Mersenne twister if possible, in particular for generating many
1023
large random integers.
1024
1025
There is also a predefined global random source GlobalMersenneTwister which
1026
is used by most of the library methods for Random (30.7-1).
1027
1028
IsGAPRandomSource uses the same number generator as IsGlobalRandomSource,
1029
but you can create several of these random sources which generate their
1030
random numbers independently of all other random sources.
1031
1032
IsGlobalRandomSource gives access to the classical global random generator
1033
which was used by GAP in former releases. You do not need to construct new
1034
random sources of this kind which would all use the same global data
1035
structure. Just use the existing random source GlobalRandomSource. This uses
1036
the additive random number generator described in [Knu98] (Algorithm A
1037
in 3.2.2 with lag 30).
1038
1039
14.7-5 RandomSource
1040
1041
RandomSource( cat[, seed] )  operation
1042
1043
This operation is used to create new random sources. The first argument cat
1044
is the category describing the type of the random generator, an optional
1045
seed which can be an integer or a type specific data structure can be given
1046
to specify the initial state.
1047
1048
 Example 
1049
gap> rs1 := RandomSource(IsMersenneTwister);
1050
<RandomSource in IsMersenneTwister>
1051
gap> state1 := State(rs1);;
1052
gap> l1 := List([1..10000], i-> Random(rs1, [1..6]));; 
1053
gap> rs2 := RandomSource(IsMersenneTwister);;
1054
gap> l2 := List([1..10000], i-> Random(rs2, [1..6]));;
1055
gap> l1 = l2;
1056
true
1057
gap> l1 = List([1..10000], i-> Random(rs1, [1..6])); 
1058
false
1059
gap> n := Random(rs1, 1, 2^220);
1060
1077726777923092117987668044202944212469136000816111066409337432400
1061

1062
1063
1064