Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 4183461[1X15 [33X[0;0YNumber Theory[133X[101X23[33X[0;0Y[5XGAP[105X provides a couple of elementary number theoretic functions. Most of4these deal with the group of integers coprime to [22Xm[122X, called the [13Xprime residue5group[113X. The order of this group is [22Xϕ(m)[122X (see [2XPhi[102X ([14X15.2-2[114X)), and [22Xλ(m)[122X6(see [2XLambda[102X ([14X15.2-3[114X)) is its exponent. This group is cyclic if and only if [22Xm[122X7is 2, 4, an odd prime power [22Xp^n[122X, or twice an odd prime power [22X2 p^n[122X. In this8case the generators of the group, i.e., elements of order [22Xϕ(m)[122X, are called9[13Xprimitive roots[113X (see [2XPrimitiveRootMod[102X ([14X15.3-3[114X)).[133X1011[33X[0;0YNote that neither the arguments nor the return values of the functions12listed below are groups or group elements in the sense of [5XGAP[105X. The arguments13are simply integers.[133X141516[1X15.1 [33X[0;0YInfoNumtheor (Info Class)[133X[101X1718[1X15.1-1 InfoNumtheor[101X1920[33X[1;0Y[29X[2XInfoNumtheor[102X[32X info class[133X2122[33X[0;0Y[2XInfoNumtheor[102X is the info class (see [14X7.4[114X) for the functions in the number23theory chapter.[133X242526[1X15.2 [33X[0;0YPrime Residues[133X[101X2728[1X15.2-1 PrimeResidues[101X2930[33X[1;0Y[29X[2XPrimeResidues[102X( [3Xm[103X ) [32X function[133X3132[33X[0;0Y[2XPrimeResidues[102X returns the set of integers from the range [10X[ 0 .. Abs( [3Xm[103X[10X )-1 ][110X33that are coprime to the integer [3Xm[103X.[133X3435[33X[0;0Y[10XAbs([3Xm[103X[10X)[110X must be less than [22X2^28[122X, otherwise the set would probably be too large36anyhow.[133X3738[4X[32X Example [32X[104X39[4X[25Xgap>[125X [27XPrimeResidues( 0 ); PrimeResidues( 1 ); PrimeResidues( 20 );[127X[104X40[4X[28X[ ][128X[104X41[4X[28X[ 0 ][128X[104X42[4X[28X[ 1, 3, 7, 9, 11, 13, 17, 19 ][128X[104X43[4X[32X[104X4445[1X15.2-2 Phi[101X4647[33X[1;0Y[29X[2XPhi[102X( [3Xm[103X ) [32X operation[133X4849[33X[0;0Y[2XPhi[102X returns the number [22Xϕ([3Xm[103X)[122X of positive integers less than the positive50integer [3Xm[103X that are coprime to [3Xm[103X.[133X5152[33X[0;0YSuppose that [22Xm = p_1^{e_1} p_2^{e_2} ⋯ p_k^{e_k}[122X. Then [22Xϕ(m)[122X is [22Xp_1^{e_1-1}53(p_1-1) p_2^{e_2-1} (p_2-1) ⋯ p_k^{e_k-1} (p_k-1)[122X.[133X5455[4X[32X Example [32X[104X56[4X[25Xgap>[125X [27XPhi( 12 );[127X[104X57[4X[28X4[128X[104X58[4X[25Xgap>[125X [27XPhi( 2^13-1 ); # this proves that 2^(13)-1 is a prime[127X[104X59[4X[28X8190[128X[104X60[4X[25Xgap>[125X [27XPhi( 2^15-1 );[127X[104X61[4X[28X27000[128X[104X62[4X[32X[104X6364[1X15.2-3 Lambda[101X6566[33X[1;0Y[29X[2XLambda[102X( [3Xm[103X ) [32X operation[133X6768[33X[0;0Y[2XLambda[102X returns the exponent [22Xλ([3Xm[103X)[122X of the group of prime residues modulo the69integer [3Xm[103X.[133X7071[33X[0;0Y[22Xλ([3Xm[103X)[122X is the smallest positive integer [22Xl[122X such that for every [22Xa[122X relatively72prime to [3Xm[103X we have [22Xa^l ≡ 1 mod [3Xm[103X[122X. Fermat's theorem asserts [22Xa^{ϕ([3Xm[103X)} ≡ 1 mod73[3Xm[103X[122X; thus [22Xλ([3Xm[103X)[122X divides [22Xϕ([3Xm[103X)[122X (see [2XPhi[102X ([14X15.2-2[114X)).[133X7475[33X[0;0YCarmichael's theorem states that [22Xλ[122X can be computed as follows: [22Xλ(2) = 1[122X,76[22Xλ(4) = 2[122X and [22Xλ(2^e) = 2^{e-2}[122X if [22X3 ≤ e[122X, [22Xλ(p^e) = (p-1) p^{e-1}[122X (i.e. [22Xϕ(m)[122X)77if [22Xp[122X is an odd prime and [22Xλ(m*n) =[122X[10XLcm[110X[22X( λ(m), λ(n) )[122X if [22Xm, n[122X are coprime.[133X7879[33X[0;0YComposites for which [22Xλ(m)[122X divides [22Xm - 1[122X are called Carmichaels. If [22X6k+1[122X,80[22X12k+1[122X and [22X18k+1[122X are primes their product is such a number. There are only811547 Carmichaels below [22X10^10[122X but 455052511 primes.[133X8283[4X[32X Example [32X[104X84[4X[25Xgap>[125X [27XLambda( 10 );[127X[104X85[4X[28X4[128X[104X86[4X[25Xgap>[125X [27XLambda( 30 );[127X[104X87[4X[28X4[128X[104X88[4X[25Xgap>[125X [27XLambda( 561 ); # 561 is the smallest Carmichael number[127X[104X89[4X[28X80[128X[104X90[4X[32X[104X9192[1X15.2-4 GeneratorsPrimeResidues[101X9394[33X[1;0Y[29X[2XGeneratorsPrimeResidues[102X( [3Xn[103X ) [32X function[133X9596[33X[0;0YLet [3Xn[103X be a positive integer. [2XGeneratorsPrimeResidues[102X returns a description97of generators of the group of prime residues modulo [3Xn[103X. The return value is a98record with components[133X99100[8X[10Xprimes[110X[8X: [108X101[33X[0;6Ya list of the prime factors of [3Xn[103X,[133X102103[8X[10Xexponents[110X[8X: [108X104[33X[0;6Ya list of the exponents of these primes in the factorization of [3Xn[103X, and[133X105106[8X[10Xgenerators[110X[8X: [108X107[33X[0;6Ya list describing generators of the group of prime residues; for the108prime factor [22X2[122X, either a primitive root or a list of two generators is109stored, for each other prime factor of [3Xn[103X, a primitive root is stored.[133X110111[4X[32X Example [32X[104X112[4X[25Xgap>[125X [27XGeneratorsPrimeResidues( 1 );[127X[104X113[4X[28Xrec( exponents := [ ], generators := [ ], primes := [ ] )[128X[104X114[4X[25Xgap>[125X [27XGeneratorsPrimeResidues( 4*3 );[127X[104X115[4X[28Xrec( exponents := [ 2, 1 ], generators := [ 7, 5 ], [128X[104X116[4X[28X primes := [ 2, 3 ] )[128X[104X117[4X[25Xgap>[125X [27XGeneratorsPrimeResidues( 8*9*5 );[127X[104X118[4X[28Xrec( exponents := [ 3, 2, 1 ], [128X[104X119[4X[28X generators := [ [ 271, 181 ], 281, 217 ], primes := [ 2, 3, 5 ] )[128X[104X120[4X[32X[104X121122123[1X15.3 [33X[0;0YPrimitive Roots and Discrete Logarithms[133X[101X124125[1X15.3-1 OrderMod[101X126127[33X[1;0Y[29X[2XOrderMod[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X128129[33X[0;0Y[2XOrderMod[102X returns the multiplicative order of the integer [3Xn[103X modulo the130positive integer [3Xm[103X. If [3Xn[103X and [3Xm[103X are not coprime the order of [3Xn[103X is not defined131and [2XOrderMod[102X will return [10X0[110X.[133X132133[33X[0;0YIf [3Xn[103X and [3Xm[103X are relatively prime the multiplicative order of [3Xn[103X modulo [3Xm[103X is134the smallest positive integer [22Xi[122X such that [22X[3Xn[103X^i ≡ 1 mod [3Xm[103X[122X. If the group of135prime residues modulo [3Xm[103X is cyclic then each element of maximal order is136called a primitive root modulo [3Xm[103X (see [2XIsPrimitiveRootMod[102X ([14X15.3-4[114X)).[133X137138[33X[0;0Y[2XOrderMod[102X usually spends most of its time factoring [3Xm[103X and [22Xϕ([3Xm[103X)[122X139(see [2XFactorsInt[102X ([14X14.4-7[114X)).[133X140141[4X[32X Example [32X[104X142[4X[25Xgap>[125X [27XOrderMod( 2, 7 );[127X[104X143[4X[28X3[128X[104X144[4X[25Xgap>[125X [27XOrderMod( 3, 7 ); # 3 is a primitive root modulo 7[127X[104X145[4X[28X6[128X[104X146[4X[32X[104X147148[1X15.3-2 LogMod[101X149150[33X[1;0Y[29X[2XLogMod[102X( [3Xn[103X, [3Xr[103X, [3Xm[103X ) [32X function[133X151[33X[1;0Y[29X[2XLogModShanks[102X( [3Xn[103X, [3Xr[103X, [3Xm[103X ) [32X function[133X152153[33X[0;0Ycomputes the discrete [3Xr[103X-logarithm of the integer [3Xn[103X modulo the integer [3Xm[103X. It154returns a number [3Xl[103X such that [22X[3Xr[103X^[3Xl[103X ≡ [3Xn[103X mod [3Xm[103X[122X if such a number exists.155Otherwise [9Xfail[109X is returned.[133X156157[33X[0;0Y[2XLogModShanks[102X uses the Baby Step - Giant Step Method of Shanks (see for158example [Coh93, section 5.4.1]) and in general requires more memory than a159call to [2XLogMod[102X.[133X160161[4X[32X Example [32X[104X162[4X[25Xgap>[125X [27Xl:= LogMod( 2, 5, 7 ); 5^l mod 7 = 2;[127X[104X163[4X[28X4[128X[104X164[4X[28Xtrue[128X[104X165[4X[25Xgap>[125X [27XLogMod( 1, 3, 3 ); LogMod( 2, 3, 3 );[127X[104X166[4X[28X0[128X[104X167[4X[28Xfail[128X[104X168[4X[32X[104X169170[1X15.3-3 PrimitiveRootMod[101X171172[33X[1;0Y[29X[2XPrimitiveRootMod[102X( [3Xm[103X[, [3Xstart[103X] ) [32X function[133X173174[33X[0;0Y[2XPrimitiveRootMod[102X returns the smallest primitive root modulo the positive175integer [3Xm[103X and [9Xfail[109X if no such primitive root exists. If the optional second176integer argument [3Xstart[103X is given [2XPrimitiveRootMod[102X returns the smallest177primitive root that is strictly larger than [3Xstart[103X.[133X178179[4X[32X Example [32X[104X180[4X[25Xgap>[125X [27X# largest primitive root for a prime less than 2000:[127X[104X181[4X[25Xgap>[125X [27XPrimitiveRootMod( 409 ); [127X[104X182[4X[28X21[128X[104X183[4X[25Xgap>[125X [27XPrimitiveRootMod( 541, 2 );[127X[104X184[4X[28X10[128X[104X185[4X[25Xgap>[125X [27X# 327 is the largest primitive root mod 337:[127X[104X186[4X[25Xgap>[125X [27XPrimitiveRootMod( 337, 327 );[127X[104X187[4X[28Xfail[128X[104X188[4X[25Xgap>[125X [27X# there exists no primitive root modulo 30:[127X[104X189[4X[25Xgap>[125X [27XPrimitiveRootMod( 30 );[127X[104X190[4X[28Xfail[128X[104X191[4X[32X[104X192193[1X15.3-4 IsPrimitiveRootMod[101X194195[33X[1;0Y[29X[2XIsPrimitiveRootMod[102X( [3Xr[103X, [3Xm[103X ) [32X function[133X196197[33X[0;0Y[2XIsPrimitiveRootMod[102X returns [9Xtrue[109X if the integer [3Xr[103X is a primitive root modulo198the positive integer [3Xm[103X, and [9Xfalse[109X otherwise. If [3Xr[103X is less than 0 or larger199than [3Xm[103X it is replaced by its remainder.[133X200201[4X[32X Example [32X[104X202[4X[25Xgap>[125X [27XIsPrimitiveRootMod( 2, 541 );[127X[104X203[4X[28Xtrue[128X[104X204[4X[25Xgap>[125X [27XIsPrimitiveRootMod( -539, 541 ); # same computation as above;[127X[104X205[4X[28Xtrue[128X[104X206[4X[25Xgap>[125X [27XIsPrimitiveRootMod( 4, 541 );[127X[104X207[4X[28Xfalse[128X[104X208[4X[25Xgap>[125X [27XForAny( [1..29], r -> IsPrimitiveRootMod( r, 30 ) );[127X[104X209[4X[28Xfalse[128X[104X210[4X[25Xgap>[125X [27X# there is no a primitive root modulo 30[127X[104X211[4X[32X[104X212213214[1X15.4 [33X[0;0YRoots Modulo Integers[133X[101X215216[1X15.4-1 Jacobi[101X217218[33X[1;0Y[29X[2XJacobi[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X219220[33X[0;0Y[2XJacobi[102X returns the value of the [13XKronecker-Jacobi symbol[113X [22XJ([3Xn[103X,[3Xm[103X)[122X of the221integer [3Xn[103X modulo the integer [3Xm[103X. It is defined as follows:[133X222223[33X[0;0YIf [22Xn[122X and [22Xm[122X are not coprime then [22XJ(n,m) = 0[122X. Furthermore, [22XJ(n,1) = 1[122X and224[22XJ(n,-1) = -1[122X if [22Xm < 0[122X and [22X+1[122X otherwise. And for odd [22Xn[122X it is [22XJ(n,2) = (-1)^k[122X225with [22Xk = (n^2-1)/8[122X. For odd primes [22Xm[122X which are coprime to [22Xn[122X the226Kronecker-Jacobi symbol has the same value as the Legendre symbol227(see [2XLegendre[102X ([14X15.4-2[114X)).[133X228229[33X[0;0YFor the general case suppose that [22Xm = p_1 ⋅ p_2 ⋯ p_k[122X is a product of [22X-1[122X and230of primes, not necessarily distinct, and that [22Xn[122X is coprime to [22Xm[122X. Then [22XJ(n,m)231= J(n,p_1) ⋅ J(n,p_2) ⋯ J(n,p_k)[122X.[133X232233[33X[0;0YNote that the Kronecker-Jacobi symbol coincides with the Jacobi symbol that234is defined for odd [22Xm[122X in many number theory books. For odd primes [22Xm[122X and [22Xn[122X235coprime to [22Xm[122X it coincides with the Legendre symbol.[133X236237[33X[0;0Y[2XJacobi[102X is very efficient, even for large values of [3Xn[103X and [3Xm[103X, it is about as238fast as the Euclidean algorithm (see [2XGcd[102X ([14X56.7-1[114X)).[133X239240[4X[32X Example [32X[104X241[4X[25Xgap>[125X [27XJacobi( 11, 35 ); # 9^2 = 11 mod 35[127X[104X242[4X[28X1[128X[104X243[4X[25Xgap>[125X [27X# this is -1, thus there is no r such that r^2 = 6 mod 35[127X[104X244[4X[25Xgap>[125X [27XJacobi( 6, 35 );[127X[104X245[4X[28X-1[128X[104X246[4X[25Xgap>[125X [27X# this is 1 even though there is no r with r^2 = 3 mod 35[127X[104X247[4X[25Xgap>[125X [27XJacobi( 3, 35 );[127X[104X248[4X[28X1[128X[104X249[4X[32X[104X250251[1X15.4-2 Legendre[101X252253[33X[1;0Y[29X[2XLegendre[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X254255[33X[0;0Y[2XLegendre[102X returns the value of the [13XLegendre symbol[113X of the integer [3Xn[103X modulo256the positive integer [3Xm[103X.[133X257258[33X[0;0YThe value of the Legendre symbol [22XL(n/m)[122X is 1 if [22Xn[122X is a [13Xquadratic residue[113X259modulo [22Xm[122X, i.e., if there exists an integer [22Xr[122X such that [22Xr^2 ≡ n mod m[122X and [22X-1[122X260otherwise.[133X261262[33X[0;0YIf a root of [3Xn[103X exists it can be found by [2XRootMod[102X ([14X15.4-3[114X).[133X263264[33X[0;0YWhile the value of the Legendre symbol usually is only defined for [3Xm[103X a265prime, we have extended the definition to include composite moduli too. The266Jacobi symbol (see [2XJacobi[102X ([14X15.4-1[114X)) is another generalization of the267Legendre symbol for composite moduli that is much cheaper to compute,268because it does not need the factorization of [3Xm[103X (see [2XFactorsInt[102X ([14X14.4-7[114X)).[133X269270[33X[0;0YA description of the Jacobi symbol, the Legendre symbol, and related topics271can be found in [Bak84].[133X272273[4X[32X Example [32X[104X274[4X[25Xgap>[125X [27XLegendre( 5, 11 ); # 4^2 = 5 mod 11[127X[104X275[4X[28X1[128X[104X276[4X[25Xgap>[125X [27X# this is -1, thus there is no r such that r^2 = 6 mod 11[127X[104X277[4X[25Xgap>[125X [27XLegendre( 6, 11 );[127X[104X278[4X[28X-1[128X[104X279[4X[25Xgap>[125X [27X# this is -1, thus there is no r such that r^2 = 3 mod 35[127X[104X280[4X[25Xgap>[125X [27XLegendre( 3, 35 );[127X[104X281[4X[28X-1[128X[104X282[4X[32X[104X283284[1X15.4-3 RootMod[101X285286[33X[1;0Y[29X[2XRootMod[102X( [3Xn[103X[, [3Xk[103X], [3Xm[103X ) [32X function[133X287288[33X[0;0Y[2XRootMod[102X computes a [3Xk[103Xth root of the integer [3Xn[103X modulo the positive integer [3Xm[103X,289i.e., a [22Xr[122X such that [22Xr^[3Xk[103X ≡ [3Xn[103X mod [3Xm[103X[122X. If no such root exists [2XRootMod[102X returns290[9Xfail[109X. If only the arguments [3Xn[103X and [3Xm[103X are given, the default value for [3Xk[103X is [22X2[122X.[133X291292[33X[0;0YA square root of [3Xn[103X exists only if [10XLegendre([3Xn[103X[10X,[3Xm[103X[10X) = 1[110X (see [2XLegendre[102X ([14X15.4-2[114X)).293If [3Xm[103X has [22Xr[122X different prime factors then there are [22X2^r[122X different roots of [3Xn[103X294mod [3Xm[103X. It is unspecified which one [2XRootMod[102X returns. You can, however, use295[2XRootsMod[102X ([14X15.4-4[114X) to compute the full set of roots.[133X296297[33X[0;0Y[2XRootMod[102X is efficient even for large values of [3Xm[103X, in fact the most time is298usually spent factoring [3Xm[103X (see [2XFactorsInt[102X ([14X14.4-7[114X)).[133X299300[4X[32X Example [32X[104X301[4X[25Xgap>[125X [27X# note 'RootMod' does not return 8 in this case but -8:[127X[104X302[4X[25Xgap>[125X [27XRootMod( 64, 1009 );[127X[104X303[4X[28X1001[128X[104X304[4X[25Xgap>[125X [27XRootMod( 64, 3, 1009 );[127X[104X305[4X[28X518[128X[104X306[4X[25Xgap>[125X [27XRootMod( 64, 5, 1009 );[127X[104X307[4X[28X656[128X[104X308[4X[25Xgap>[125X [27XList( RootMod( 64, 1009 ) * RootsUnityMod( 1009 ),[127X[104X309[4X[25X>[125X [27X x -> x mod 1009 ); # set of all square roots of 64 mod 1009[127X[104X310[4X[28X[ 1001, 8 ][128X[104X311[4X[32X[104X312313[1X15.4-4 RootsMod[101X314315[33X[1;0Y[29X[2XRootsMod[102X( [3Xn[103X[, [3Xk[103X], [3Xm[103X ) [32X function[133X316317[33X[0;0Y[2XRootsMod[102X computes the set of [3Xk[103Xth roots of the integer [3Xn[103X modulo the positive318integer [3Xm[103X, i.e., the list of all [22Xr[122X such that [22Xr^[3Xk[103X ≡ [3Xn[103X mod [3Xm[103X[122X. If only the319arguments [3Xn[103X and [3Xm[103X are given, the default value for [3Xk[103X is [22X2[122X.[133X320321[4X[32X Example [32X[104X322[4X[25Xgap>[125X [27XRootsMod( 1, 7*31 ); # the same as `RootsUnityMod( 7*31 )'[127X[104X323[4X[28X[ 1, 92, 125, 216 ][128X[104X324[4X[25Xgap>[125X [27XRootsMod( 7, 7*31 );[127X[104X325[4X[28X[ 21, 196 ][128X[104X326[4X[25Xgap>[125X [27XRootsMod( 5, 7*31 );[127X[104X327[4X[28X[ ][128X[104X328[4X[25Xgap>[125X [27XRootsMod( 1, 5, 7*31 );[127X[104X329[4X[28X[ 1, 8, 64, 78, 190 ][128X[104X330[4X[32X[104X331332[1X15.4-5 RootsUnityMod[101X333334[33X[1;0Y[29X[2XRootsUnityMod[102X( [[3Xk[103X, ][3Xm[103X ) [32X function[133X335336[33X[0;0Y[2XRootsUnityMod[102X returns the set of [3Xk[103X-th roots of unity modulo the positive337integer [3Xm[103X, i.e., the list of all solutions [22Xr[122X of [22Xr^[3Xk[103X ≡ [3Xn[103X mod [3Xm[103X[122X. If only the338argument [3Xm[103X is given, the default value for [3Xk[103X is [22X2[122X.[133X339340[33X[0;0YIn general there are [22X[3Xk[103X^n[122X such roots if the modulus [3Xm[103X has [22Xn[122X different prime341factors [22Xp[122X such that [22Xp ≡ 1 mod [3Xk[103X[122X. If [22X[3Xk[103X^2[122X divides [3Xm[103X then there are [22X[3Xk[103X^{n+1}[122X342such roots; and especially if [22X[3Xk[103X = 2[122X and 8 divides [3Xm[103X there are [22X2^{n+2}[122X such343roots.[133X344345[33X[0;0YIn the current implementation [3Xk[103X must be a prime.[133X346347[4X[32X Example [32X[104X348[4X[25Xgap>[125X [27XRootsUnityMod( 7*31 ); RootsUnityMod( 3, 7*31 );[127X[104X349[4X[28X[ 1, 92, 125, 216 ][128X[104X350[4X[28X[ 1, 25, 32, 36, 67, 149, 156, 191, 211 ][128X[104X351[4X[25Xgap>[125X [27XRootsUnityMod( 5, 7*31 );[127X[104X352[4X[28X[ 1, 8, 64, 78, 190 ][128X[104X353[4X[25Xgap>[125X [27XList( RootMod( 64, 1009 ) * RootsUnityMod( 1009 ),[127X[104X354[4X[25X>[125X [27X x -> x mod 1009 ); # set of all square roots of 64 mod 1009[127X[104X355[4X[28X[ 1001, 8 ][128X[104X356[4X[32X[104X357358359[1X15.5 [33X[0;0YMultiplicative Arithmetic Functions[133X[101X360361[1X15.5-1 Sigma[101X362363[33X[1;0Y[29X[2XSigma[102X( [3Xn[103X ) [32X operation[133X364365[33X[0;0Y[2XSigma[102X returns the sum of the positive divisors of the nonzero integer [3Xn[103X.[133X366367[33X[0;0Y[2XSigma[102X is a multiplicative arithmetic function, i.e., if [22Xn[122X and [22Xm[122X are368relatively prime we have that [22Xσ(n ⋅ m) = σ(n) σ(m)[122X.[133X369370[33X[0;0YTogether with the formula [22Xσ(p^k) = (p^{k+1}-1) / (p-1)[122X this allows us to371compute [22Xσ([3Xn[103X)[122X.[133X372373[33X[0;0YIntegers [3Xn[103X for which [22Xσ([3Xn[103X) = 2 [3Xn[103X[122X are called perfect. Even perfect integers374are exactly of the form [22X2^{[3Xn[103X-1}(2^[3Xn[103X-1)[122X where [22X2^[3Xn[103X-1[122X is prime. Primes of the375form [22X2^[3Xn[103X-1[122X are called [13XMersenne primes[113X, and 42 among the known Mersenne376primes are obtained for [3Xn[103X [22X=[122X 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127,377521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937,37821701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787,3791398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583 and38025964951. Please find more up to date information about Mersenne primes at381[7Xhttp://www.mersenne.org[107X. It is not known whether odd perfect integers exist,382however [BC89] show that any such integer must have at least 300 decimal383digits.[133X384385[33X[0;0Y[2XSigma[102X usually spends most of its time factoring [3Xn[103X (see [2XFactorsInt[102X ([14X14.4-7[114X)).[133X386387[4X[32X Example [32X[104X388[4X[25Xgap>[125X [27XSigma( 1 );[127X[104X389[4X[28X1[128X[104X390[4X[25Xgap>[125X [27XSigma( 1009 ); # 1009 is a prime[127X[104X391[4X[28X1010[128X[104X392[4X[25Xgap>[125X [27XSigma( 8128 ) = 2*8128; # 8128 is a perfect number[127X[104X393[4X[28Xtrue[128X[104X394[4X[32X[104X395396[1X15.5-2 Tau[101X397398[33X[1;0Y[29X[2XTau[102X( [3Xn[103X ) [32X operation[133X399400[33X[0;0Y[2XTau[102X returns the number of the positive divisors of the nonzero integer [3Xn[103X.[133X401402[33X[0;0Y[2XTau[102X is a multiplicative arithmetic function, i.e., if [22Xn[122X and [22Xm[122X are relative403prime we have [22Xτ(n ⋅ m) = τ(n) τ(m)[122X. Together with the formula [22Xτ(p^k) = k+1[122X404this allows us to compute [22Xτ([3Xn[103X)[122X.[133X405406[33X[0;0Y[2XTau[102X usually spends most of its time factoring [3Xn[103X (see [2XFactorsInt[102X ([14X14.4-7[114X)).[133X407408[4X[32X Example [32X[104X409[4X[25Xgap>[125X [27XTau( 1 );[127X[104X410[4X[28X1[128X[104X411[4X[25Xgap>[125X [27XTau( 1013 ); # thus 1013 is a prime[127X[104X412[4X[28X2[128X[104X413[4X[25Xgap>[125X [27XTau( 8128 );[127X[104X414[4X[28X14[128X[104X415[4X[25Xgap>[125X [27X# result is odd if and only if argument is a perfect square:[127X[104X416[4X[25Xgap>[125X [27XTau( 36 );[127X[104X417[4X[28X9[128X[104X418[4X[32X[104X419420[1X15.5-3 MoebiusMu[101X421422[33X[1;0Y[29X[2XMoebiusMu[102X( [3Xn[103X ) [32X function[133X423424[33X[0;0Y[2XMoebiusMu[102X computes the value of Moebius inversion function for the nonzero425integer [3Xn[103X. This is 0 for integers which are not squarefree, i.e., which are426divided by a square [22Xr^2[122X. Otherwise it is 1 if [3Xn[103X has a even number and [22X-1[122X if427[3Xn[103X has an odd number of prime factors.[133X428429[33X[0;0YThe importance of [22Xμ[122X stems from the so called inversion formula. Suppose [22Xf[122X is430a multiplicative arithmetic function defined on the positive integers and431let [22Xg(n) = ∑_{d ∣ n} f(d)[122X. Then [22Xf(n) = ∑_{d ∣ n} μ(d) g(n/d)[122X. As a special432case we have [22Xϕ(n) = ∑_{d ∣ n} μ(d) n/d[122X since [22Xn = ∑_{d ∣ n} ϕ(d)[122X (see [2XPhi[102X433([14X15.2-2[114X)).[133X434435[33X[0;0Y[2XMoebiusMu[102X usually spends all of its time factoring [3Xn[103X (see [2XFactorsInt[102X436([14X14.4-7[114X)).[133X437438[4X[32X Example [32X[104X439[4X[25Xgap>[125X [27XMoebiusMu( 60 ); MoebiusMu( 61 ); MoebiusMu( 62 );[127X[104X440[4X[28X0[128X[104X441[4X[28X-1[128X[104X442[4X[28X1[128X[104X443[4X[32X[104X444445446[1X15.6 [33X[0;0YContinued Fractions[133X[101X447448[1X15.6-1 ContinuedFractionExpansionOfRoot[101X449450[33X[1;0Y[29X[2XContinuedFractionExpansionOfRoot[102X( [3Xf[103X, [3Xn[103X ) [32X function[133X451452[33X[0;0YThe first [3Xn[103X terms of the continued fraction expansion of the only positive453real root of the polynomial [3Xf[103X with integer coefficients. The leading454coefficient of [3Xf[103X must be positive and the value of [3Xf[103X at 0 must be negative.455If the degree of [3Xf[103X is 2 and [3Xn[103X = 0, the function computes one period of the456continued fraction expansion of the root in question. Anything may happen if457[3Xf[103X has three or more positive real roots.[133X458459[4X[32X Example [32X[104X460[4X[25Xgap>[125X [27Xx := Indeterminate(Integers);;[127X[104X461[4X[25Xgap>[125X [27XContinuedFractionExpansionOfRoot(x^2-7,20);[127X[104X462[4X[28X[ 2, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1 ][128X[104X463[4X[25Xgap>[125X [27XContinuedFractionExpansionOfRoot(x^2-7,0);[127X[104X464[4X[28X[ 2, 1, 1, 1, 4 ][128X[104X465[4X[25Xgap>[125X [27XContinuedFractionExpansionOfRoot(x^3-2,20);[127X[104X466[4X[28X[ 1, 3, 1, 5, 1, 1, 4, 1, 1, 8, 1, 14, 1, 10, 2, 1, 4, 12, 2, 3 ][128X[104X467[4X[25Xgap>[125X [27XContinuedFractionExpansionOfRoot(x^5-x-1,50);[127X[104X468[4X[28X[ 1, 5, 1, 42, 1, 3, 24, 2, 2, 1, 16, 1, 11, 1, 1, 2, 31, 1, 12, 5, [128X[104X469[4X[28X 1, 7, 11, 1, 4, 1, 4, 2, 2, 3, 4, 2, 1, 1, 11, 1, 41, 12, 1, 8, 1, [128X[104X470[4X[28X 1, 1, 1, 1, 9, 2, 1, 5, 4 ][128X[104X471[4X[32X[104X472473[1X15.6-2 ContinuedFractionApproximationOfRoot[101X474475[33X[1;0Y[29X[2XContinuedFractionApproximationOfRoot[102X( [3Xf[103X, [3Xn[103X ) [32X function[133X476477[33X[0;0YThe [3Xn[103Xth continued fraction approximation of the only positive real root of478the polynomial [3Xf[103X with integer coefficients. The leading coefficient of [3Xf[103X479must be positive and the value of [3Xf[103X at 0 must be negative. Anything may480happen if [3Xf[103X has three or more positive real roots.[133X481482[4X[32X Example [32X[104X483[4X[25Xgap>[125X [27XContinuedFractionApproximationOfRoot(x^2-2,10);[127X[104X484[4X[28X3363/2378[128X[104X485[4X[25Xgap>[125X [27X3363^2-2*2378^2;[127X[104X486[4X[28X1[128X[104X487[4X[25Xgap>[125X [27Xz := ContinuedFractionApproximationOfRoot(x^5-x-1,20);[127X[104X488[4X[28X499898783527/428250732317[128X[104X489[4X[25Xgap>[125X [27Xz^5-z-1;[127X[104X490[4X[28X486192462527432755459620441970617283/[128X[104X491[4X[28X14404247382319842421697357558805709031116987826242631261357[128X[104X492[4X[32X[104X493494495[1X15.7 [33X[0;0YMiscellaneous[133X[101X496497[1X15.7-1 TwoSquares[101X498499[33X[1;0Y[29X[2XTwoSquares[102X( [3Xn[103X ) [32X function[133X500501[33X[0;0Y[2XTwoSquares[102X returns a list of two integers [22Xx ≤ y[122X such that the sum of the502squares of [22Xx[122X and [22Xy[122X is equal to the nonnegative integer [3Xn[103X, i.e., [22Xn = x^2 +503y^2[122X. If no such representation exists [2XTwoSquares[102X will return [9Xfail[109X.504[2XTwoSquares[102X will return a representation for which the gcd of [22Xx[122X and [22Xy[122X is as505small as possible. It is not specified which representation [2XTwoSquares[102X506returns if there is more than one.[133X507508[33X[0;0YLet [22Xa[122X be the product of all maximal powers of primes of the form [22X4k+3[122X509dividing [3Xn[103X. A representation of [3Xn[103X as a sum of two squares exists if and only510if [22Xa[122X is a perfect square. Let [22Xb[122X be the maximal power of [22X2[122X dividing [3Xn[103X or its511half, whichever is a perfect square. Then the minimal possible gcd of [22Xx[122X and512[22Xy[122X is the square root [22Xc[122X of [22Xa ⋅ b[122X. The number of different minimal513representation with [22Xx ≤ y[122X is [22X2^{l-1}[122X, where [22Xl[122X is the number of different514prime factors of the form [22X4k+1[122X of [3Xn[103X.[133X515516[33X[0;0YThe algorithm first finds a square root [22Xr[122X of [22X-1[122X modulo [22X[3Xn[103X / (a ⋅ b)[122X, which517must exist, and applies the Euclidean algorithm to [22Xr[122X and [3Xn[103X. The first518residues in the sequence that are smaller than [22Xsqrt{[3Xn[103X/(a ⋅ b)}[122X times [22Xc[122X are a519possible pair [22Xx[122X and [22Xy[122X.[133X520521[33X[0;0YBetter descriptions of the algorithm and related topics can be found in522[Wag90] and [Zag90].[133X523524[4X[32X Example [32X[104X525[4X[25Xgap>[125X [27XTwoSquares( 5 );[127X[104X526[4X[28X[ 1, 2 ][128X[104X527[4X[25Xgap>[125X [27XTwoSquares( 11 ); # there is no representation[127X[104X528[4X[28Xfail[128X[104X529[4X[25Xgap>[125X [27XTwoSquares( 16 );[127X[104X530[4X[28X[ 0, 4 ][128X[104X531[4X[25Xgap>[125X [27X# 3 is the minimal possible gcd because 9 divides 45:[127X[104X532[4X[25Xgap>[125X [27XTwoSquares( 45 );[127X[104X533[4X[28X[ 3, 6 ][128X[104X534[4X[25Xgap>[125X [27X# it is not [5,10] because their gcd is not minimal:[127X[104X535[4X[25Xgap>[125X [27XTwoSquares( 125 );[127X[104X536[4X[28X[ 2, 11 ][128X[104X537[4X[25Xgap>[125X [27X# [10,11] would be the other possible representation:[127X[104X538[4X[25Xgap>[125X [27XTwoSquares( 13*17 );[127X[104X539[4X[28X[ 5, 14 ][128X[104X540[4X[25Xgap>[125X [27XTwoSquares( 848654483879497562821 ); # argument is prime[127X[104X541[4X[28X[ 6305894639, 28440994650 ][128X[104X542[4X[32X[104X543544545546