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 / chap15.txt
Views: 415154
1
2
15 Number Theory
3
4
GAP provides a couple of elementary number theoretic functions. Most of
5
these deal with the group of integers coprime to m, called the prime residue
6
group. The order of this group is ϕ(m) (see Phi (15.2-2)), and λ(m)
7
(see Lambda (15.2-3)) is its exponent. This group is cyclic if and only if m
8
is 2, 4, an odd prime power p^n, or twice an odd prime power 2 p^n. In this
9
case the generators of the group, i.e., elements of order ϕ(m), are called
10
primitive roots (see PrimitiveRootMod (15.3-3)).
11
12
Note that neither the arguments nor the return values of the functions
13
listed below are groups or group elements in the sense of GAP. The arguments
14
are simply integers.
15
16
17
15.1 InfoNumtheor (Info Class)
18
19
15.1-1 InfoNumtheor
20
21
InfoNumtheor info class
22
23
InfoNumtheor is the info class (see 7.4) for the functions in the number
24
theory chapter.
25
26
27
15.2 Prime Residues
28
29
15.2-1 PrimeResidues
30
31
PrimeResidues( m )  function
32
33
PrimeResidues returns the set of integers from the range [ 0 .. Abs( m )-1 ]
34
that are coprime to the integer m.
35
36
Abs(m) must be less than 2^28, otherwise the set would probably be too large
37
anyhow.
38
39
 Example 
40
gap> PrimeResidues( 0 ); PrimeResidues( 1 ); PrimeResidues( 20 );
41
[ ]
42
[ 0 ]
43
[ 1, 3, 7, 9, 11, 13, 17, 19 ]
44

45
46
15.2-2 Phi
47
48
Phi( m )  operation
49
50
Phi returns the number ϕ(m) of positive integers less than the positive
51
integer m that are coprime to m.
52
53
Suppose that m = p_1^{e_1} p_2^{e_2} ⋯ p_k^{e_k}. Then ϕ(m) is p_1^{e_1-1}
54
(p_1-1) p_2^{e_2-1} (p_2-1) ⋯ p_k^{e_k-1} (p_k-1).
55
56
 Example 
57
gap> Phi( 12 );
58
4
59
gap> Phi( 2^13-1 ); # this proves that 2^(13)-1 is a prime
60
8190
61
gap> Phi( 2^15-1 );
62
27000
63

64
65
15.2-3 Lambda
66
67
Lambda( m )  operation
68
69
Lambda returns the exponent λ(m) of the group of prime residues modulo the
70
integer m.
71
72
λ(m) is the smallest positive integer l such that for every a relatively
73
prime to m we have a^l ≡ 1 mod m. Fermat's theorem asserts a^{ϕ(m)} ≡ 1 mod
74
m; thus λ(m) divides ϕ(m) (see Phi (15.2-2)).
75
76
Carmichael's theorem states that λ can be computed as follows: λ(2) = 1,
77
λ(4) = 2 and λ(2^e) = 2^{e-2} if 3 ≤ e, λ(p^e) = (p-1) p^{e-1} (i.e. ϕ(m))
78
if p is an odd prime and λ(m*n) =Lcm( λ(m), λ(n) ) if m, n are coprime.
79
80
Composites for which λ(m) divides m - 1 are called Carmichaels. If 6k+1,
81
12k+1 and 18k+1 are primes their product is such a number. There are only
82
1547 Carmichaels below 10^10 but 455052511 primes.
83
84
 Example 
85
gap> Lambda( 10 );
86
4
87
gap> Lambda( 30 );
88
4
89
gap> Lambda( 561 ); # 561 is the smallest Carmichael number
90
80
91

92
93
15.2-4 GeneratorsPrimeResidues
94
95
GeneratorsPrimeResidues( n )  function
96
97
Let n be a positive integer. GeneratorsPrimeResidues returns a description
98
of generators of the group of prime residues modulo n. The return value is a
99
record with components
100
101
primes: 
102
a list of the prime factors of n,
103
104
exponents: 
105
a list of the exponents of these primes in the factorization of n, and
106
107
generators: 
108
a list describing generators of the group of prime residues; for the
109
prime factor 2, either a primitive root or a list of two generators is
110
stored, for each other prime factor of n, a primitive root is stored.
111
112
 Example 
113
gap> GeneratorsPrimeResidues( 1 );
114
rec( exponents := [ ], generators := [ ], primes := [ ] )
115
gap> GeneratorsPrimeResidues( 4*3 );
116
rec( exponents := [ 2, 1 ], generators := [ 7, 5 ], 
117
 primes := [ 2, 3 ] )
118
gap> GeneratorsPrimeResidues( 8*9*5 );
119
rec( exponents := [ 3, 2, 1 ], 
120
 generators := [ [ 271, 181 ], 281, 217 ], primes := [ 2, 3, 5 ] )
121

122
123
124
15.3 Primitive Roots and Discrete Logarithms
125
126
15.3-1 OrderMod
127
128
OrderMod( n, m )  function
129
130
OrderMod returns the multiplicative order of the integer n modulo the
131
positive integer m. If n and m are not coprime the order of n is not defined
132
and OrderMod will return 0.
133
134
If n and m are relatively prime the multiplicative order of n modulo m is
135
the smallest positive integer i such that n^i ≡ 1 mod m. If the group of
136
prime residues modulo m is cyclic then each element of maximal order is
137
called a primitive root modulo m (see IsPrimitiveRootMod (15.3-4)).
138
139
OrderMod usually spends most of its time factoring m and ϕ(m)
140
(see FactorsInt (14.4-7)).
141
142
 Example 
143
gap> OrderMod( 2, 7 );
144
3
145
gap> OrderMod( 3, 7 ); # 3 is a primitive root modulo 7
146
6
147

148
149
15.3-2 LogMod
150
151
LogMod( n, r, m )  function
152
LogModShanks( n, r, m )  function
153
154
computes the discrete r-logarithm of the integer n modulo the integer m. It
155
returns a number l such that r^l ≡ n mod m if such a number exists.
156
Otherwise fail is returned.
157
158
LogModShanks uses the Baby Step - Giant Step Method of Shanks (see for
159
example [Coh93, section 5.4.1]) and in general requires more memory than a
160
call to LogMod.
161
162
 Example 
163
gap> l:= LogMod( 2, 5, 7 ); 5^l mod 7 = 2;
164
4
165
true
166
gap> LogMod( 1, 3, 3 ); LogMod( 2, 3, 3 );
167
0
168
fail
169

170
171
15.3-3 PrimitiveRootMod
172
173
PrimitiveRootMod( m[, start] )  function
174
175
PrimitiveRootMod returns the smallest primitive root modulo the positive
176
integer m and fail if no such primitive root exists. If the optional second
177
integer argument start is given PrimitiveRootMod returns the smallest
178
primitive root that is strictly larger than start.
179
180
 Example 
181
gap> # largest primitive root for a prime less than 2000:
182
gap> PrimitiveRootMod( 409 ); 
183
21
184
gap> PrimitiveRootMod( 541, 2 );
185
10
186
gap> # 327 is the largest primitive root mod 337:
187
gap> PrimitiveRootMod( 337, 327 );
188
fail
189
gap> # there exists no primitive root modulo 30:
190
gap> PrimitiveRootMod( 30 );
191
fail
192

193
194
15.3-4 IsPrimitiveRootMod
195
196
IsPrimitiveRootMod( r, m )  function
197
198
IsPrimitiveRootMod returns true if the integer r is a primitive root modulo
199
the positive integer m, and false otherwise. If r is less than 0 or larger
200
than m it is replaced by its remainder.
201
202
 Example 
203
gap> IsPrimitiveRootMod( 2, 541 );
204
true
205
gap> IsPrimitiveRootMod( -539, 541 ); # same computation as above;
206
true
207
gap> IsPrimitiveRootMod( 4, 541 );
208
false
209
gap> ForAny( [1..29], r -> IsPrimitiveRootMod( r, 30 ) );
210
false
211
gap> # there is no a primitive root modulo 30
212

213
214
215
15.4 Roots Modulo Integers
216
217
15.4-1 Jacobi
218
219
Jacobi( n, m )  function
220
221
Jacobi returns the value of the Kronecker-Jacobi symbol J(n,m) of the
222
integer n modulo the integer m. It is defined as follows:
223
224
If n and m are not coprime then J(n,m) = 0. Furthermore, J(n,1) = 1 and
225
J(n,-1) = -1 if m < 0 and +1 otherwise. And for odd n it is J(n,2) = (-1)^k
226
with k = (n^2-1)/8. For odd primes m which are coprime to n the
227
Kronecker-Jacobi symbol has the same value as the Legendre symbol
228
(see Legendre (15.4-2)).
229
230
For the general case suppose that m = p_1 ⋅ p_2 ⋯ p_k is a product of -1 and
231
of primes, not necessarily distinct, and that n is coprime to m. Then J(n,m)
232
= J(n,p_1) ⋅ J(n,p_2) ⋯ J(n,p_k).
233
234
Note that the Kronecker-Jacobi symbol coincides with the Jacobi symbol that
235
is defined for odd m in many number theory books. For odd primes m and n
236
coprime to m it coincides with the Legendre symbol.
237
238
Jacobi is very efficient, even for large values of n and m, it is about as
239
fast as the Euclidean algorithm (see Gcd (56.7-1)).
240
241
 Example 
242
gap> Jacobi( 11, 35 ); # 9^2 = 11 mod 35
243
1
244
gap> # this is -1, thus there is no r such that r^2 = 6 mod 35
245
gap> Jacobi( 6, 35 );
246
-1
247
gap> # this is 1 even though there is no r with r^2 = 3 mod 35
248
gap> Jacobi( 3, 35 );
249
1
250

251
252
15.4-2 Legendre
253
254
Legendre( n, m )  function
255
256
Legendre returns the value of the Legendre symbol of the integer n modulo
257
the positive integer m.
258
259
The value of the Legendre symbol L(n/m) is 1 if n is a quadratic residue
260
modulo m, i.e., if there exists an integer r such that r^2 ≡ n mod m and -1
261
otherwise.
262
263
If a root of n exists it can be found by RootMod (15.4-3).
264
265
While the value of the Legendre symbol usually is only defined for m a
266
prime, we have extended the definition to include composite moduli too. The
267
Jacobi symbol (see Jacobi (15.4-1)) is another generalization of the
268
Legendre symbol for composite moduli that is much cheaper to compute,
269
because it does not need the factorization of m (see FactorsInt (14.4-7)).
270
271
A description of the Jacobi symbol, the Legendre symbol, and related topics
272
can be found in [Bak84].
273
274
 Example 
275
gap> Legendre( 5, 11 ); # 4^2 = 5 mod 11
276
1
277
gap> # this is -1, thus there is no r such that r^2 = 6 mod 11
278
gap> Legendre( 6, 11 );
279
-1
280
gap> # this is -1, thus there is no r such that r^2 = 3 mod 35
281
gap> Legendre( 3, 35 );
282
-1
283

284
285
15.4-3 RootMod
286
287
RootMod( n[, k], m )  function
288
289
RootMod computes a kth root of the integer n modulo the positive integer m,
290
i.e., a r such that r^k ≡ n mod m. If no such root exists RootMod returns
291
fail. If only the arguments n and m are given, the default value for k is 2.
292
293
A square root of n exists only if Legendre(n,m) = 1 (see Legendre (15.4-2)).
294
If m has r different prime factors then there are 2^r different roots of n
295
mod m. It is unspecified which one RootMod returns. You can, however, use
296
RootsMod (15.4-4) to compute the full set of roots.
297
298
RootMod is efficient even for large values of m, in fact the most time is
299
usually spent factoring m (see FactorsInt (14.4-7)).
300
301
 Example 
302
gap> # note 'RootMod' does not return 8 in this case but -8:
303
gap> RootMod( 64, 1009 );
304
1001
305
gap> RootMod( 64, 3, 1009 );
306
518
307
gap> RootMod( 64, 5, 1009 );
308
656
309
gap> List( RootMod( 64, 1009 ) * RootsUnityMod( 1009 ),
310
>  x -> x mod 1009 ); # set of all square roots of 64 mod 1009
311
[ 1001, 8 ]
312

313
314
15.4-4 RootsMod
315
316
RootsMod( n[, k], m )  function
317
318
RootsMod computes the set of kth roots of the integer n modulo the positive
319
integer m, i.e., the list of all r such that r^k ≡ n mod m. If only the
320
arguments n and m are given, the default value for k is 2.
321
322
 Example 
323
gap> RootsMod( 1, 7*31 ); # the same as `RootsUnityMod( 7*31 )'
324
[ 1, 92, 125, 216 ]
325
gap> RootsMod( 7, 7*31 );
326
[ 21, 196 ]
327
gap> RootsMod( 5, 7*31 );
328
[ ]
329
gap> RootsMod( 1, 5, 7*31 );
330
[ 1, 8, 64, 78, 190 ]
331

332
333
15.4-5 RootsUnityMod
334
335
RootsUnityMod( [k, ]m )  function
336
337
RootsUnityMod returns the set of k-th roots of unity modulo the positive
338
integer m, i.e., the list of all solutions r of r^k ≡ n mod m. If only the
339
argument m is given, the default value for k is 2.
340
341
In general there are k^n such roots if the modulus m has n different prime
342
factors p such that p ≡ 1 mod k. If k^2 divides m then there are k^{n+1}
343
such roots; and especially if k = 2 and 8 divides m there are 2^{n+2} such
344
roots.
345
346
In the current implementation k must be a prime.
347
348
 Example 
349
gap> RootsUnityMod( 7*31 ); RootsUnityMod( 3, 7*31 );
350
[ 1, 92, 125, 216 ]
351
[ 1, 25, 32, 36, 67, 149, 156, 191, 211 ]
352
gap> RootsUnityMod( 5, 7*31 );
353
[ 1, 8, 64, 78, 190 ]
354
gap> List( RootMod( 64, 1009 ) * RootsUnityMod( 1009 ),
355
>  x -> x mod 1009 ); # set of all square roots of 64 mod 1009
356
[ 1001, 8 ]
357

358
359
360
15.5 Multiplicative Arithmetic Functions
361
362
15.5-1 Sigma
363
364
Sigma( n )  operation
365
366
Sigma returns the sum of the positive divisors of the nonzero integer n.
367
368
Sigma is a multiplicative arithmetic function, i.e., if n and m are
369
relatively prime we have that σ(n ⋅ m) = σ(n) σ(m).
370
371
Together with the formula σ(p^k) = (p^{k+1}-1) / (p-1) this allows us to
372
compute σ(n).
373
374
Integers n for which σ(n) = 2 n are called perfect. Even perfect integers
375
are exactly of the form 2^{n-1}(2^n-1) where 2^n-1 is prime. Primes of the
376
form 2^n-1 are called Mersenne primes, and 42 among the known Mersenne
377
primes are obtained for n = 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127,
378
521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937,
379
21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787,
380
1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583 and
381
25964951. Please find more up to date information about Mersenne primes at
382
http://www.mersenne.org. It is not known whether odd perfect integers exist,
383
however [BC89] show that any such integer must have at least 300 decimal
384
digits.
385
386
Sigma usually spends most of its time factoring n (see FactorsInt (14.4-7)).
387
388
 Example 
389
gap> Sigma( 1 );
390
1
391
gap> Sigma( 1009 ); # 1009 is a prime
392
1010
393
gap> Sigma( 8128 ) = 2*8128; # 8128 is a perfect number
394
true
395

396
397
15.5-2 Tau
398
399
Tau( n )  operation
400
401
Tau returns the number of the positive divisors of the nonzero integer n.
402
403
Tau is a multiplicative arithmetic function, i.e., if n and m are relative
404
prime we have τ(n ⋅ m) = τ(n) τ(m). Together with the formula τ(p^k) = k+1
405
this allows us to compute τ(n).
406
407
Tau usually spends most of its time factoring n (see FactorsInt (14.4-7)).
408
409
 Example 
410
gap> Tau( 1 );
411
1
412
gap> Tau( 1013 ); # thus 1013 is a prime
413
2
414
gap> Tau( 8128 );
415
14
416
gap> # result is odd if and only if argument is a perfect square:
417
gap> Tau( 36 );
418
9
419

420
421
15.5-3 MoebiusMu
422
423
MoebiusMu( n )  function
424
425
MoebiusMu computes the value of Moebius inversion function for the nonzero
426
integer n. This is 0 for integers which are not squarefree, i.e., which are
427
divided by a square r^2. Otherwise it is 1 if n has a even number and -1 if
428
n has an odd number of prime factors.
429
430
The importance of μ stems from the so called inversion formula. Suppose f is
431
a multiplicative arithmetic function defined on the positive integers and
432
let g(n) = ∑_{d ∣ n} f(d). Then f(n) = ∑_{d ∣ n} μ(d) g(n/d). As a special
433
case we have ϕ(n) = ∑_{d ∣ n} μ(d) n/d since n = ∑_{d ∣ n} ϕ(d) (see Phi
434
(15.2-2)).
435
436
MoebiusMu usually spends all of its time factoring n (see FactorsInt
437
(14.4-7)).
438
439
 Example 
440
gap> MoebiusMu( 60 ); MoebiusMu( 61 ); MoebiusMu( 62 );
441
0
442
-1
443
1
444

445
446
447
15.6 Continued Fractions
448
449
15.6-1 ContinuedFractionExpansionOfRoot
450
451
ContinuedFractionExpansionOfRoot( f, n )  function
452
453
The first n terms of the continued fraction expansion of the only positive
454
real root of the polynomial f with integer coefficients. The leading
455
coefficient of f must be positive and the value of f at 0 must be negative.
456
If the degree of f is 2 and n = 0, the function computes one period of the
457
continued fraction expansion of the root in question. Anything may happen if
458
f has three or more positive real roots.
459
460
 Example 
461
gap> x := Indeterminate(Integers);;
462
gap> ContinuedFractionExpansionOfRoot(x^2-7,20);
463
[ 2, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1 ]
464
gap> ContinuedFractionExpansionOfRoot(x^2-7,0);
465
[ 2, 1, 1, 1, 4 ]
466
gap> ContinuedFractionExpansionOfRoot(x^3-2,20);
467
[ 1, 3, 1, 5, 1, 1, 4, 1, 1, 8, 1, 14, 1, 10, 2, 1, 4, 12, 2, 3 ]
468
gap> ContinuedFractionExpansionOfRoot(x^5-x-1,50);
469
[ 1, 5, 1, 42, 1, 3, 24, 2, 2, 1, 16, 1, 11, 1, 1, 2, 31, 1, 12, 5, 
470
 1, 7, 11, 1, 4, 1, 4, 2, 2, 3, 4, 2, 1, 1, 11, 1, 41, 12, 1, 8, 1, 
471
 1, 1, 1, 1, 9, 2, 1, 5, 4 ]
472

473
474
15.6-2 ContinuedFractionApproximationOfRoot
475
476
ContinuedFractionApproximationOfRoot( f, n )  function
477
478
The nth continued fraction approximation of the only positive real root of
479
the polynomial f with integer coefficients. The leading coefficient of f
480
must be positive and the value of f at 0 must be negative. Anything may
481
happen if f has three or more positive real roots.
482
483
 Example 
484
gap> ContinuedFractionApproximationOfRoot(x^2-2,10);
485
3363/2378
486
gap> 3363^2-2*2378^2;
487
1
488
gap> z := ContinuedFractionApproximationOfRoot(x^5-x-1,20);
489
499898783527/428250732317
490
gap> z^5-z-1;
491
486192462527432755459620441970617283/
492
14404247382319842421697357558805709031116987826242631261357
493

494
495
496
15.7 Miscellaneous
497
498
15.7-1 TwoSquares
499
500
TwoSquares( n )  function
501
502
TwoSquares returns a list of two integers x ≤ y such that the sum of the
503
squares of x and y is equal to the nonnegative integer n, i.e., n = x^2 +
504
y^2. If no such representation exists TwoSquares will return fail.
505
TwoSquares will return a representation for which the gcd of x and y is as
506
small as possible. It is not specified which representation TwoSquares
507
returns if there is more than one.
508
509
Let a be the product of all maximal powers of primes of the form 4k+3
510
dividing n. A representation of n as a sum of two squares exists if and only
511
if a is a perfect square. Let b be the maximal power of 2 dividing n or its
512
half, whichever is a perfect square. Then the minimal possible gcd of x and
513
y is the square root c of a ⋅ b. The number of different minimal
514
representation with x ≤ y is 2^{l-1}, where l is the number of different
515
prime factors of the form 4k+1 of n.
516
517
The algorithm first finds a square root r of -1 modulo n / (a ⋅ b), which
518
must exist, and applies the Euclidean algorithm to r and n. The first
519
residues in the sequence that are smaller than sqrt{n/(a ⋅ b)} times c are a
520
possible pair x and y.
521
522
Better descriptions of the algorithm and related topics can be found in
523
[Wag90] and [Zag90].
524
525
 Example 
526
gap> TwoSquares( 5 );
527
[ 1, 2 ]
528
gap> TwoSquares( 11 ); # there is no representation
529
fail
530
gap> TwoSquares( 16 );
531
[ 0, 4 ]
532
gap> # 3 is the minimal possible gcd because 9 divides 45:
533
gap> TwoSquares( 45 );
534
[ 3, 6 ]
535
gap> # it is not [5,10] because their gcd is not minimal:
536
gap> TwoSquares( 125 );
537
[ 2, 11 ]
538
gap> # [10,11] would be the other possible representation:
539
gap> TwoSquares( 13*17 );
540
[ 5, 14 ]
541
gap> TwoSquares( 848654483879497562821 ); # argument is prime
542
[ 6305894639, 28440994650 ]
543

544
545
546