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[1X14 [33X[0;0YIntegers[133X[101X23[33X[0;0YOne of the most fundamental datatypes in every programming language is the4integer type. [5XGAP[105X is no exception.[133X56[33X[0;0Y[5XGAP[105X integers are entered as a sequence of decimal digits optionally preceded7by a [21X[10X+[110X[121X sign for positive integers or a [21X[10X-[110X[121X sign for negative integers. The8size of integers in [5XGAP[105X is only limited by the amount of available memory,9so you can compute with integers having thousands of digits.[133X1011[4X[32X Example [32X[104X12[4X[25Xgap>[125X [27X-1234;[127X[104X13[4X[28X-1234[128X[104X14[4X[25Xgap>[125X [27X123456789012345678901234567890123456789012345678901234567890;[127X[104X15[4X[28X123456789012345678901234567890123456789012345678901234567890[128X[104X16[4X[32X[104X1718[33X[0;0YMany more functions that are mainly related to the prime residue group of19integers modulo an integer are described in chapter [14X15[114X, and functions20dealing with combinatorics can be found in chapter [14X16[114X.[133X212223[1X14.1 [33X[0;0YIntegers: Global Variables[133X[101X2425[1X14.1-1 Integers[101X2627[33X[1;0Y[29X[2XIntegers[102X[32X global variable[133X28[33X[1;0Y[29X[2XPositiveIntegers[102X[32X global variable[133X29[33X[1;0Y[29X[2XNonnegativeIntegers[102X[32X global variable[133X3031[33X[0;0YThese global variables represent the ring of integers and the semirings of32positive and nonnegative integers, respectively.[133X3334[4X[32X Example [32X[104X35[4X[25Xgap>[125X [27XSize( Integers ); 2 in Integers;[127X[104X36[4X[28Xinfinity[128X[104X37[4X[28Xtrue[128X[104X38[4X[32X[104X3940[33X[0;0Y[2XIntegers[102X is a subset of [2XRationals[102X ([14X17.1-1[114X), which is a subset of [2XCyclotomics[102X41([14X18.1-2[114X). See Chapter [14X18[114X for arithmetic operations and comparison of42integers.[133X4344[1X14.1-2 IsIntegers[101X4546[33X[1;0Y[29X[2XIsIntegers[102X( [3Xobj[103X ) [32X Category[133X47[33X[1;0Y[29X[2XIsPositiveIntegers[102X( [3Xobj[103X ) [32X Category[133X48[33X[1;0Y[29X[2XIsNonnegativeIntegers[102X( [3Xobj[103X ) [32X Category[133X4950[33X[0;0Yare the defining categories for the domains [2XIntegers[102X ([14X14.1-1[114X),51[2XPositiveIntegers[102X ([14X14.1-1[114X), and [2XNonnegativeIntegers[102X ([14X14.1-1[114X).[133X5253[4X[32X Example [32X[104X54[4X[25Xgap>[125X [27XIsIntegers( Integers ); IsIntegers( Rationals ); IsIntegers( 7 );[127X[104X55[4X[28Xtrue[128X[104X56[4X[28Xfalse[128X[104X57[4X[28Xfalse[128X[104X58[4X[32X[104X596061[1X14.2 [33X[0;0YElementary Operations for Integers[133X[101X6263[1X14.2-1 IsInt[101X6465[33X[1;0Y[29X[2XIsInt[102X( [3Xobj[103X ) [32X Category[133X6667[33X[0;0YEvery rational integer lies in the category [2XIsInt[102X, which is a subcategory of68[2XIsRat[102X ([14X17.2-1[114X).[133X6970[1X14.2-2 IsPosInt[101X7172[33X[1;0Y[29X[2XIsPosInt[102X( [3Xobj[103X ) [32X Category[133X7374[33X[0;0YEvery positive integer lies in the category [2XIsPosInt[102X.[133X7576[1X14.2-3 Int[101X7778[33X[1;0Y[29X[2XInt[102X( [3Xelm[103X ) [32X attribute[133X7980[33X[0;0Y[2XInt[102X returns an integer [10Xint[110X whose meaning depends on the type of [3Xelm[103X.[133X8182[33X[0;0YIf [3Xelm[103X is a rational number (see Chapter [14X17[114X) then [10Xint[110X is the integer part of83the quotient of numerator and denominator of [3Xelm[103X (see [2XQuoInt[102X ([14X14.3-1[114X)).[133X8485[33X[0;0YIf [3Xelm[103X is an element of a finite prime field (see Chapter [14X59[114X) then [10Xint[110X is86the smallest nonnegative integer such that [10X[3Xelm[103X[10X = int * One( [3Xelm[103X[10X )[110X.[133X8788[33X[0;0YIf [3Xelm[103X is a string (see Chapter [14X27[114X) consisting of digits [10X'0'[110X, [10X'1'[110X, [22X...[122X, [10X'9'[110X89and [10X'-'[110X (at the first position) then [10Xint[110X is the integer described by this90string. The operation [2XString[102X ([14X27.7-6[114X) can be used to compute a string for91rational integers, in fact for all cyclotomics.[133X9293[4X[32X Example [32X[104X94[4X[25Xgap>[125X [27XInt( 4/3 ); Int( -2/3 );[127X[104X95[4X[28X1[128X[104X96[4X[28X0[128X[104X97[4X[25Xgap>[125X [27Xint:= Int( Z(5) ); int * One( Z(5) );[127X[104X98[4X[28X2[128X[104X99[4X[28XZ(5)[128X[104X100[4X[25Xgap>[125X [27XInt( "12345" ); Int( "-27" ); Int( "-27/3" );[127X[104X101[4X[28X12345[128X[104X102[4X[28X-27[128X[104X103[4X[28Xfail[128X[104X104[4X[32X[104X105106[1X14.2-4 IsEvenInt[101X107108[33X[1;0Y[29X[2XIsEvenInt[102X( [3Xn[103X ) [32X function[133X109110[33X[0;0Ytests if the integer [3Xn[103X is divisible by 2.[133X111112[1X14.2-5 IsOddInt[101X113114[33X[1;0Y[29X[2XIsOddInt[102X( [3Xn[103X ) [32X function[133X115116[33X[0;0Ytests if the integer [3Xn[103X is not divisible by 2.[133X117118[1X14.2-6 AbsInt[101X119120[33X[1;0Y[29X[2XAbsInt[102X( [3Xn[103X ) [32X function[133X121122[33X[0;0Y[2XAbsInt[102X returns the absolute value of the integer [3Xn[103X, i.e., [3Xn[103X if [3Xn[103X is123positive, -[3Xn[103X if [3Xn[103X is negative and 0 if [3Xn[103X is 0.[133X124125[33X[0;0Y[2XAbsInt[102X is a special case of the general operation [2XEuclideanDegree[102X ([14X56.6-2[114X).[133X126127[33X[0;0YSee also [2XAbsoluteValue[102X ([14X18.1-8[114X).[133X128129[4X[32X Example [32X[104X130[4X[25Xgap>[125X [27XAbsInt( 33 );[127X[104X131[4X[28X33[128X[104X132[4X[25Xgap>[125X [27XAbsInt( -214378 );[127X[104X133[4X[28X214378[128X[104X134[4X[25Xgap>[125X [27XAbsInt( 0 );[127X[104X135[4X[28X0[128X[104X136[4X[32X[104X137138[1X14.2-7 SignInt[101X139140[33X[1;0Y[29X[2XSignInt[102X( [3Xn[103X ) [32X function[133X141142[33X[0;0Y[2XSignInt[102X returns the sign of the integer [3Xn[103X, i.e., 1 if [3Xn[103X is positive, -1 if [3Xn[103X143is negative and 0 if [3Xn[103X is 0.[133X144145[4X[32X Example [32X[104X146[4X[25Xgap>[125X [27XSignInt( 33 );[127X[104X147[4X[28X1[128X[104X148[4X[25Xgap>[125X [27XSignInt( -214378 );[127X[104X149[4X[28X-1[128X[104X150[4X[25Xgap>[125X [27XSignInt( 0 );[127X[104X151[4X[28X0[128X[104X152[4X[32X[104X153154[1X14.2-8 LogInt[101X155156[33X[1;0Y[29X[2XLogInt[102X( [3Xn[103X, [3Xbase[103X ) [32X function[133X157158[33X[0;0Y[2XLogInt[102X returns the integer part of the logarithm of the positive integer [3Xn[103X159with respect to the positive integer [3Xbase[103X, i.e., the largest positive160integer [22Xe[122X such that [22X[3Xbase[103X^e ≤ [3Xn[103X[122X. The function [2XLogInt[102X will signal an error if161either [3Xn[103X or [3Xbase[103X is not positive.[133X162163[33X[0;0YFor [3Xbase[103X [22X= 2[122X this is very efficient because the internal binary164representation of the integer is used.[133X165166[4X[32X Example [32X[104X167[4X[25Xgap>[125X [27XLogInt( 1030, 2 );[127X[104X168[4X[28X10[128X[104X169[4X[25Xgap>[125X [27X2^10;[127X[104X170[4X[28X1024[128X[104X171[4X[25Xgap>[125X [27XLogInt( 1, 10 );[127X[104X172[4X[28X0[128X[104X173[4X[32X[104X174175[1X14.2-9 RootInt[101X176177[33X[1;0Y[29X[2XRootInt[102X( [3Xn[103X[, [3Xk[103X] ) [32X function[133X178179[33X[0;0Y[2XRootInt[102X returns the integer part of the [3Xk[103Xth root of the integer [3Xn[103X. If the180optional integer argument [3Xk[103X is not given it defaults to 2, i.e., [2XRootInt[102X181returns the integer part of the square root in this case.[133X182183[33X[0;0YIf [3Xn[103X is positive, [2XRootInt[102X returns the largest positive integer [22Xr[122X such that184[22Xr^[3Xk[103X ≤ [3Xn[103X[122X. If [3Xn[103X is negative and [3Xk[103X is odd [2XRootInt[102X returns [10X-RootInt( -[3Xn[103X[10X, [3Xk[103X[10X )[110X. If185[3Xn[103X is negative and [3Xk[103X is even [2XRootInt[102X will cause an error. [2XRootInt[102X will also186cause an error if [3Xk[103X is 0 or negative.[133X187188[4X[32X Example [32X[104X189[4X[25Xgap>[125X [27XRootInt( 361 );[127X[104X190[4X[28X19[128X[104X191[4X[25Xgap>[125X [27XRootInt( 2 * 10^12 );[127X[104X192[4X[28X1414213[128X[104X193[4X[25Xgap>[125X [27XRootInt( 17000, 5 );[127X[104X194[4X[28X7[128X[104X195[4X[25Xgap>[125X [27X7^5;[127X[104X196[4X[28X16807[128X[104X197[4X[32X[104X198199[1X14.2-10 SmallestRootInt[101X200201[33X[1;0Y[29X[2XSmallestRootInt[102X( [3Xn[103X ) [32X function[133X202203[33X[0;0Y[2XSmallestRootInt[102X returns the smallest root of the integer [3Xn[103X.[133X204205[33X[0;0YThe smallest root of an integer [3Xn[103X is the integer [22Xr[122X of smallest absolute206value for which a positive integer [22Xk[122X exists such that [22X[3Xn[103X = r^k[122X.[133X207208[4X[32X Example [32X[104X209[4X[25Xgap>[125X [27XSmallestRootInt( 2^30 );[127X[104X210[4X[28X2[128X[104X211[4X[25Xgap>[125X [27XSmallestRootInt( -(2^30) );[127X[104X212[4X[28X-4[128X[104X213[4X[32X[104X214215[33X[0;0YNote that [22X(-2)^30 = +(2^30)[122X.[133X216217[4X[32X Example [32X[104X218[4X[25Xgap>[125X [27XSmallestRootInt( 279936 );[127X[104X219[4X[28X6[128X[104X220[4X[25Xgap>[125X [27XLogInt( 279936, 6 );[127X[104X221[4X[28X7[128X[104X222[4X[25Xgap>[125X [27XSmallestRootInt( 1001 );[127X[104X223[4X[28X1001[128X[104X224[4X[32X[104X225226[1X14.2-11 ListOfDigits[101X227228[33X[1;0Y[29X[2XListOfDigits[102X( [3Xn[103X ) [32X function[133X229230[33X[0;0YFor a positive integer [3Xn[103X this function returns a list [3Xl[103X, consisting of the231digits of [3Xn[103X in decimal representation.[133X232233[4X[32X Example [32X[104X234[4X[25Xgap>[125X [27XListOfDigits(3142); [127X[104X235[4X[28X[ 3, 1, 4, 2 ][128X[104X236[4X[32X[104X237238[1X14.2-12 Random[101X239240[33X[1;0Y[29X[2XRandom[102X( [3XIntegers[103X ) [32X method[133X241242[33X[0;0Y[2XRandom[102X for integers returns pseudo random integers between [22X-10[122X and [22X10[122X243distributed according to a binomial distribution. To generate uniformly244distributed integers from a range, use the construction [10XRandom( [ [3Xlow[103X[10X ..245[3Xhigh[103X[10X ] )[110X (see [2XRandom[102X ([14X30.7-1[114X)).[133X246247248[1X14.3 [33X[0;0YQuotients and Remainders[133X[101X249250[1X14.3-1 QuoInt[101X251252[33X[1;0Y[29X[2XQuoInt[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X253254[33X[0;0Y[2XQuoInt[102X returns the integer part of the quotient of its integer operands.[133X255256[33X[0;0YIf [3Xn[103X and [3Xm[103X are positive, [2XQuoInt[102X returns the largest positive integer [22Xq[122X such257that [22Xq * [3Xm[103X ≤ [3Xn[103X[122X. If [3Xn[103X or [3Xm[103X or both are negative the absolute value of the258integer part of the quotient is the quotient of the absolute values of [3Xn[103X and259[3Xm[103X, and the sign of it is the product of the signs of [3Xn[103X and [3Xm[103X.[133X260261[33X[0;0Y[2XQuoInt[102X is used in a method for the general operation [2XEuclideanQuotient[102X262([14X56.6-3[114X).[133X263264[4X[32X Example [32X[104X265[4X[25Xgap>[125X [27XQuoInt(5,3); QuoInt(-5,3); QuoInt(5,-3); QuoInt(-5,-3);[127X[104X266[4X[28X1[128X[104X267[4X[28X-1[128X[104X268[4X[28X-1[128X[104X269[4X[28X1[128X[104X270[4X[32X[104X271272[1X14.3-2 BestQuoInt[101X273274[33X[1;0Y[29X[2XBestQuoInt[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X275276[33X[0;0Y[2XBestQuoInt[102X returns the best quotient [22Xq[122X of the integers [3Xn[103X and [3Xm[103X. This is the277quotient such that [22X[3Xn[103X-q*[3Xm[103X[122X has minimal absolute value. If there are two278quotients whose remainders have the same absolute value, then the quotient279with the smaller absolute value is chosen.[133X280281[4X[32X Example [32X[104X282[4X[25Xgap>[125X [27XBestQuoInt( 5, 3 ); BestQuoInt( -5, 3 );[127X[104X283[4X[28X2[128X[104X284[4X[28X-2[128X[104X285[4X[32X[104X286287[1X14.3-3 RemInt[101X288289[33X[1;0Y[29X[2XRemInt[102X( [3Xn[103X, [3Xm[103X ) [32X function[133X290291[33X[0;0Y[2XRemInt[102X returns the remainder of its two integer operands.[133X292293[33X[0;0YIf [3Xm[103X is not equal to zero, [2XRemInt[102X returns [10X[3Xn[103X[10X - [3Xm[103X[10X * QuoInt( [3Xn[103X[10X, [3Xm[103X[10X )[110X. Note that294the rules given for [2XQuoInt[102X ([14X14.3-1[114X) imply that the return value of [2XRemInt[102X295has the same sign as [3Xn[103X and its absolute value is strictly less than the296absolute value of [3Xm[103X. Note also that the return value equals [10X[3Xn[103X[10X mod [3Xm[103X[10X[110X when297both [3Xn[103X and [3Xm[103X are nonnegative. Dividing by [10X0[110X signals an error.[133X298299[33X[0;0Y[2XRemInt[102X is used in a method for the general operation [2XEuclideanRemainder[102X300([14X56.6-4[114X).[133X301302[4X[32X Example [32X[104X303[4X[25Xgap>[125X [27XRemInt(5,3); RemInt(-5,3); RemInt(5,-3); RemInt(-5,-3);[127X[104X304[4X[28X2[128X[104X305[4X[28X-2[128X[104X306[4X[28X2[128X[104X307[4X[28X-2[128X[104X308[4X[32X[104X309310[1X14.3-4 GcdInt[101X311312[33X[1;0Y[29X[2XGcdInt[102X( [3Xm[103X, [3Xn[103X ) [32X function[133X313314[33X[0;0Y[2XGcdInt[102X returns the greatest common divisor of its two integer operands [3Xm[103X and315[3Xn[103X, i.e., the greatest integer that divides both [3Xm[103X and [3Xn[103X. The greatest common316divisor is never negative, even if the arguments are. We define [10XGcdInt( [3Xm[103X[10X, 0317) = GcdInt( 0, [3Xm[103X[10X ) = AbsInt( [3Xm[103X[10X )[110X and [10XGcdInt( 0, 0 ) = 0[110X.[133X318319[33X[0;0Y[2XGcdInt[102X is a method used by the general function [2XGcd[102X ([14X56.7-1[114X).[133X320321[4X[32X Example [32X[104X322[4X[25Xgap>[125X [27XGcdInt( 123, 66 );[127X[104X323[4X[28X3[128X[104X324[4X[32X[104X325326[1X14.3-5 Gcdex[101X327328[33X[1;0Y[29X[2XGcdex[102X( [3Xm[103X, [3Xn[103X ) [32X function[133X329330[33X[0;0Yreturns a record [10Xg[110X describing the extended greatest common divisor of [3Xm[103X and331[3Xn[103X. The component [10Xgcd[110X is this gcd, the components [10Xcoeff1[110X and [10Xcoeff2[110X are332integer cofactors such that [10Xg.gcd = g.coeff1 * [3Xm[103X[10X + g.coeff2 * [3Xn[103X[10X[110X, and the333components [10Xcoeff3[110X and [10Xcoeff4[110X are integer cofactors such that [10X0 = g.coeff3 *334[3Xm[103X[10X + g.coeff4 * [3Xn[103X[10X[110X.[133X335336[33X[0;0YIf [3Xm[103X and [3Xn[103X both are nonzero, [10XAbsInt( g.coeff1 )[110X is less than or equal to337[10XAbsInt([3Xn[103X[10X) / (2 * g.gcd)[110X, and [10XAbsInt( g.coeff2 )[110X is less than or equal to338[10XAbsInt([3Xm[103X[10X) / (2 * g.gcd)[110X.[133X339340[33X[0;0YIf [3Xm[103X or [3Xn[103X or both are zero [10Xcoeff3[110X is [10X-[3Xn[103X[10X / g.gcd[110X and [10Xcoeff4[110X is [10X[3Xm[103X[10X / g.gcd[110X.[133X341342[33X[0;0YThe coefficients always form a unimodular matrix, i.e., the determinant343[10Xg.coeff1 * g.coeff4 - g.coeff3 * g.coeff2[110X is [22X1[122X or [22X-1[122X.[133X344345[4X[32X Example [32X[104X346[4X[25Xgap>[125X [27XGcdex( 123, 66 );[127X[104X347[4X[28Xrec( coeff1 := 7, coeff2 := -13, coeff3 := -22, coeff4 := 41, [128X[104X348[4X[28X gcd := 3 )[128X[104X349[4X[32X[104X350351[33X[0;0YThis means [22X3 = 7 * 123 - 13 * 66[122X, [22X0 = -22 * 123 + 41 * 66[122X.[133X352353[4X[32X Example [32X[104X354[4X[25Xgap>[125X [27XGcdex( 0, -3 );[127X[104X355[4X[28Xrec( coeff1 := 0, coeff2 := -1, coeff3 := 1, coeff4 := 0, gcd := 3 )[128X[104X356[4X[25Xgap>[125X [27XGcdex( 0, 0 );[127X[104X357[4X[28Xrec( coeff1 := 1, coeff2 := 0, coeff3 := 0, coeff4 := 1, gcd := 0 )[128X[104X358[4X[32X[104X359360[33X[0;0Y[2XGcdRepresentation[102X ([14X56.7-3[114X) provides similar functionality over arbitrary361Euclidean rings.[133X362363[1X14.3-6 LcmInt[101X364365[33X[1;0Y[29X[2XLcmInt[102X( [3Xm[103X, [3Xn[103X ) [32X function[133X366367[33X[0;0Yreturns the least common multiple of the integers [3Xm[103X and [3Xn[103X.[133X368369[33X[0;0Y[2XLcmInt[102X is a method used by the general operation [2XLcm[102X ([14X56.7-6[114X).[133X370371[4X[32X Example [32X[104X372[4X[25Xgap>[125X [27XLcmInt( 123, 66 );[127X[104X373[4X[28X2706[128X[104X374[4X[32X[104X375376[1X14.3-7 CoefficientsQadic[101X377378[33X[1;0Y[29X[2XCoefficientsQadic[102X( [3Xi[103X, [3Xq[103X ) [32X operation[133X379380[33X[0;0Yreturns the [3Xq[103X-adic representation of the integer [3Xi[103X as a list [22Xl[122X of381coefficients satisfying the equality [22X[3Xi[103X = ∑_{j = 0} [3Xq[103X^j ⋅ l[j+1][122X for an382integer [22X[3Xq[103X > 1[122X.[133X383384[4X[32X Example [32X[104X385[4X[25Xgap>[125X [27Xl:=CoefficientsQadic(462,3);[127X[104X386[4X[28X[ 0, 1, 0, 2, 2, 1 ][128X[104X387[4X[32X[104X388389[1X14.3-8 CoefficientsMultiadic[101X390391[33X[1;0Y[29X[2XCoefficientsMultiadic[102X( [3Xints[103X, [3Xint[103X ) [32X function[133X392393[33X[0;0Yreturns the multiadic expansion of the integer [3Xint[103X modulo the integers given394in [3Xints[103X (in ascending order). It returns a list of coefficients in the395[13Xreverse[113X order to that in [3Xints[103X.[133X396397[1X14.3-9 ChineseRem[101X398399[33X[1;0Y[29X[2XChineseRem[102X( [3Xmoduli[103X, [3Xresidues[103X ) [32X function[133X400401[33X[0;0Y[2XChineseRem[102X returns the combination of the [3Xresidues[103X modulo the [3Xmoduli[103X, i.e.,402the unique integer [10Xc[110X from [10X[0..Lcm([3Xmoduli[103X[10X)-1][110X such that [10Xc = [3Xresidues[103X[10X[i][110X403modulo [10X[3Xmoduli[103X[10X[i][110X for all [10Xi[110X, if it exists. If no such combination exists404[2XChineseRem[102X signals an error.[133X405406[33X[0;0YSuch a combination does exist if and only if [10X[3Xresidues[103X[10X[i] = [3Xresidues[103X[10X[k] mod407Gcd( [3Xmoduli[103X[10X[i], [3Xmoduli[103X[10X[k] )[110X for every pair [10Xi[110X, [10Xk[110X. Note that this implies that408such a combination exists if the moduli are pairwise relatively prime. This409is called the Chinese remainder theorem.[133X410411[4X[32X Example [32X[104X412[4X[25Xgap>[125X [27XChineseRem( [ 2, 3, 5, 7 ], [ 1, 2, 3, 4 ] );[127X[104X413[4X[28X53[128X[104X414[4X[25Xgap>[125X [27XChineseRem( [ 6, 10, 14 ], [ 1, 3, 5 ] );[127X[104X415[4X[28X103[128X[104X416[4X[32X[104X417418[4X[32X Example [32X[104X419[4X[25Xgap>[125X [27XChineseRem( [ 6, 10, 14 ], [ 1, 2, 3 ] );[127X[104X420[4X[28XError, the residues must be equal modulo 2 called from[128X[104X421[4X[28X<function>( <arguments> ) called from read-eval-loop[128X[104X422[4X[28XEntering break read-eval-print loop ...[128X[104X423[4X[28Xyou can 'quit;' to quit to outer loop, or[128X[104X424[4X[28Xyou can 'return;' to continue[128X[104X425[4X[26Xbrk>[126X [27Xgap> [127X[104X426[4X[32X[104X427428[1X14.3-10 PowerModInt[101X429430[33X[1;0Y[29X[2XPowerModInt[102X( [3Xr[103X, [3Xe[103X, [3Xm[103X ) [32X function[133X431432[33X[0;0Yreturns [22X[3Xr[103X^[3Xe[103X mod [3Xm[103X[122X for integers [3Xr[103X, [3Xe[103X and [3Xm[103X ([22X[3Xe[103X ≥ 0[122X).[133X433434[33X[0;0YNote that [2XPowerModInt[102X can reduce intermediate results and thus will435generally be faster than using [3Xr[103X[10X^[110X[3Xe[103X[10X mod [110X[3Xm[103X, which would compute [22X[3Xr[103X^[3Xe[103X[122X first and436reduces the result afterwards.[133X437438[33X[0;0Y[2XPowerModInt[102X is a method for the general operation [2XPowerMod[102X ([14X56.7-9[114X).[133X439440441[1X14.4 [33X[0;0YPrime Integers and Factorization[133X[101X442443[1X14.4-1 Primes[101X444445[33X[1;0Y[29X[2XPrimes[102X[32X global variable[133X446447[33X[0;0Y[2XPrimes[102X is a strictly sorted list of the 168 primes less than 1000.[133X448449[33X[0;0YThis is used in [2XIsPrimeInt[102X ([14X14.4-2[114X) and [2XFactorsInt[102X ([14X14.4-7[114X) to cast out450small primes quickly.[133X451452[4X[32X Example [32X[104X453[4X[25Xgap>[125X [27XPrimes[1];[127X[104X454[4X[28X2[128X[104X455[4X[25Xgap>[125X [27XPrimes[100];[127X[104X456[4X[28X541[128X[104X457[4X[32X[104X458459[1X14.4-2 IsPrimeInt[101X460461[33X[1;0Y[29X[2XIsPrimeInt[102X( [3Xn[103X ) [32X function[133X462[33X[1;0Y[29X[2XIsProbablyPrimeInt[102X( [3Xn[103X ) [32X function[133X463464[33X[0;0Y[2XIsPrimeInt[102X returns [9Xfalse[109X if it can prove that the integer [3Xn[103X is composite and465[9Xtrue[109X otherwise. By convention [10XIsPrimeInt(0) = IsPrimeInt(1) = false[110X and we466define [10XIsPrimeInt(-[110X[3Xn[103X[10X) = IsPrimeInt([110X[3Xn[103X[10X)[110X.[133X467468[33X[0;0Y[2XIsPrimeInt[102X will return [9Xtrue[109X for every prime [3Xn[103X. [2XIsPrimeInt[102X will return [9Xfalse[109X469for all composite [3Xn[103X [22X< 10^18[122X and for all composite [3Xn[103X that have a factor [22Xp <4701000[122X. So for integers [3Xn[103X [22X< 10^18[122X, [2XIsPrimeInt[102X is a proper primality test. It471is conceivable that [2XIsPrimeInt[102X may return [9Xtrue[109X for some composite [3Xn[103X [22X> 10^18[122X,472but no such [3Xn[103X is currently known. So for integers [3Xn[103X [22X> 10^18[122X, [2XIsPrimeInt[102X is a473probable-primality test. [2XIsPrimeInt[102X will issue a warning when its argument474is probably prime but not a proven prime. (The function [2XIsProbablyPrimeInt[102X475will do a similar calculation but not issue a warning.) The warning can be476switched off by [10XSetInfoLevel( InfoPrimeInt, 0 );[110X, the default level is [22X1[122X477(also see [2XSetInfoLevel[102X ([14X7.4-3[114X) ).[133X478479[33X[0;0YIf composites that fool [2XIsPrimeInt[102X do exist, they would be extremely rare,480and finding one by pure chance might be less likely than finding a bug in481[5XGAP[105X. We would appreciate being informed about any example of a composite482number [3Xn[103X for which [2XIsPrimeInt[102X returns [9Xtrue[109X.[133X483484[33X[0;0Y[2XIsPrimeInt[102X is a deterministic algorithm, i.e., the computations involve no485random numbers, and repeated calls will always return the same result.486[2XIsPrimeInt[102X first does trial divisions by the primes less than 1000. Then it487tests that [3Xn[103X is a strong pseudoprime w.r.t. the base 2. Finally it tests488whether [3Xn[103X is a Lucas pseudoprime w.r.t. the smallest quadratic nonresidue of489[3Xn[103X. A better description can be found in the comment in the library file490[11Xprimality.gi[111X.[133X491492[33X[0;0YThe time taken by [2XIsPrimeInt[102X is approximately proportional to the third493power of the number of digits of [3Xn[103X. Testing numbers with several hundreds494digits is quite feasible.[133X495496[33X[0;0Y[2XIsPrimeInt[102X is a method for the general operation [2XIsPrime[102X ([14X56.5-8[114X).[133X497498[33X[0;0YRemark: In future versions of [5XGAP[105X we hope to change the definition of499[2XIsPrimeInt[102X to return [9Xtrue[109X only for proven primes (currently, we lack a500sufficiently good primality proving function). In applications, use501explicitly [2XIsPrimeInt[102X or [2XIsProbablyPrimeInt[102X with this change in mind.[133X502503[4X[32X Example [32X[104X504[4X[25Xgap>[125X [27XIsPrimeInt( 2^31 - 1 );[127X[104X505[4X[28Xtrue[128X[104X506[4X[25Xgap>[125X [27XIsPrimeInt( 10^42 + 1 );[127X[104X507[4X[28Xfalse[128X[104X508[4X[32X[104X509510[1X14.4-3 PrimalityProof[101X511512[33X[1;0Y[29X[2XPrimalityProof[102X( [3Xn[103X ) [32X function[133X513514[33X[0;0YConstruct a machine verifiable proof of the primality of (the probable515prime) [3Xn[103X, following the ideas of [BLS75]. The proof consists of various516Fermat and Lucas pseudoprimality tests, which taken as a whole prove the517primality. The proof is represented as a list of witnesses of two kinds. The518first kind, [10X[ "F", divisor, base ][110X, indicates a successful Fermat519pseudoprimality test, where [3Xn[103X is a strong pseudoprime at [9Xbase[109X with order not520divisible by [22X([3Xn[103X-1)/divisor[122X. The second kind, [10X[ "L", divisor, discriminant, P521][110X indicates a successful Lucas pseudoprimality test, for a quadratic form of522given [9Xdiscriminant[109X and middle term [9XP[109X with an extra check at [22X([3Xn[103X+1)/divisor[122X.[133X523524[1X14.4-4 IsPrimePowerInt[101X525526[33X[1;0Y[29X[2XIsPrimePowerInt[102X( [3Xn[103X ) [32X function[133X527528[33X[0;0Y[2XIsPrimePowerInt[102X returns [9Xtrue[109X if the integer [3Xn[103X is a prime power and [9Xfalse[109X529otherwise.[133X530531[33X[0;0YAn integer [22Xn[122X is a [13Xprime power[113X if there exists a prime [22Xp[122X and a positive532integer [22Xi[122X such that [22Xp^i = n[122X. If [22Xn[122X is negative the condition is that there533must exist a negative prime [22Xp[122X and an odd positive integer [22Xi[122X such that [22Xp^i =534n[122X. The integers 1 and -1 are not prime powers.[133X535536[33X[0;0YNote that [2XIsPrimePowerInt[102X uses [2XSmallestRootInt[102X ([14X14.2-10[114X) and a537probable-primality test (see [2XIsPrimeInt[102X ([14X14.4-2[114X)).[133X538539[4X[32X Example [32X[104X540[4X[25Xgap>[125X [27XIsPrimePowerInt( 31^5 );[127X[104X541[4X[28Xtrue[128X[104X542[4X[25Xgap>[125X [27XIsPrimePowerInt( 2^31-1 ); # 2^31-1 is actually a prime[127X[104X543[4X[28Xtrue[128X[104X544[4X[25Xgap>[125X [27XIsPrimePowerInt( 2^63-1 );[127X[104X545[4X[28Xfalse[128X[104X546[4X[25Xgap>[125X [27XFiltered( [-10..10], IsPrimePowerInt );[127X[104X547[4X[28X[ -8, -7, -5, -3, -2, 2, 3, 4, 5, 7, 8, 9 ][128X[104X548[4X[32X[104X549550[1X14.4-5 NextPrimeInt[101X551552[33X[1;0Y[29X[2XNextPrimeInt[102X( [3Xn[103X ) [32X function[133X553554[33X[0;0Y[2XNextPrimeInt[102X returns the smallest prime which is strictly larger than the555integer [3Xn[103X.[133X556557[33X[0;0YNote that [2XNextPrimeInt[102X uses a probable-primality test (see [2XIsPrimeInt[102X558([14X14.4-2[114X)).[133X559560[4X[32X Example [32X[104X561[4X[25Xgap>[125X [27XNextPrimeInt( 541 ); NextPrimeInt( -1 );[127X[104X562[4X[28X547[128X[104X563[4X[28X2[128X[104X564[4X[32X[104X565566[1X14.4-6 PrevPrimeInt[101X567568[33X[1;0Y[29X[2XPrevPrimeInt[102X( [3Xn[103X ) [32X function[133X569570[33X[0;0Y[2XPrevPrimeInt[102X returns the largest prime which is strictly smaller than the571integer [3Xn[103X.[133X572573[33X[0;0YNote that [2XPrevPrimeInt[102X uses a probable-primality test (see [2XIsPrimeInt[102X574([14X14.4-2[114X)).[133X575576[4X[32X Example [32X[104X577[4X[25Xgap>[125X [27XPrevPrimeInt( 541 ); PrevPrimeInt( 1 );[127X[104X578[4X[28X523[128X[104X579[4X[28X-2[128X[104X580[4X[32X[104X581582[1X14.4-7 FactorsInt[101X583584[33X[1;0Y[29X[2XFactorsInt[102X( [3Xn[103X ) [32X function[133X585[33X[1;0Y[29X[2XFactorsInt[102X( [3Xn:[103X [3XRhoTrials[103X [3X:=[103X [3Xtrials[103X ) [32X function[133X586587[33X[0;0Y[2XFactorsInt[102X returns a list of factors of a given integer [3Xn[103X such that [10XProduct(588FactorsInt( [3Xn[103X[10X ) ) = [3Xn[103X[10X[110X. If [22X|n| ≤ 1[122X the list [10X[[3Xn[103X[10X][110X is returned. Otherwise the589result contains probable primes, sorted by absolute value. The entries will590all be positive except for the first one in case of a negative [3Xn[103X.[133X591592[33X[0;0YSee [2XPrimeDivisors[102X ([14X14.4-8[114X) for a function that returns a set of (probable)593primes dividing [3Xn[103X.[133X594595[33X[0;0YNote that [2XFactorsInt[102X uses a probable-primality test (see [2XIsPrimeInt[102X596([14X14.4-2[114X)). Thus [2XFactorsInt[102X might return a list which contains composite597integers. In such a case you will get a warning about the use of a probable598prime. You can switch off these warnings by [10XSetInfoLevel( InfoPrimeInt, 0 );[110X599(also see [2XSetInfoLevel[102X ([14X7.4-3[114X)).[133X600601[33X[0;0YThe time taken by [2XFactorsInt[102X is approximately proportional to the square602root of the second largest prime factor of [3Xn[103X, which is the last one that603[2XFactorsInt[102X has to find, since the largest factor is simply what remains when604all others have been removed. Thus the time is roughly bounded by the fourth605root of [3Xn[103X. [2XFactorsInt[102X is guaranteed to find all factors less than [22X10^6[122X and606will find most factors less than [22X10^10[122X. If [3Xn[103X contains multiple factors607larger than that [2XFactorsInt[102X may not be able to factor [3Xn[103X and will then signal608an error.[133X609610[33X[0;0Y[2XFactorsInt[102X is used in a method for the general operation [2XFactors[102X ([14X56.5-9[114X).[133X611612[33X[0;0YIn the second form, [2XFactorsInt[102X calls [10XFactorsRho[110X with a limit of [3Xtrials[103X on613the number of trials it performs. The default is 8192. Note that Pollard's614Rho is the fastest method for finding prime factors with roughly 5-10615decimal digits, but becomes more and more inferior to other factorization616techniques like e.g. the Elliptic Curves Method (ECM) the bigger the prime617factors are. Therefore instead of performing a huge number of Rho [3Xtrials[103X, it618is usually more advisable to install the [5XFactInt[105X package and then simply to619use the operation [2XFactors[102X ([14X56.5-9[114X). The factorization of the 8-th Fermat620number by Pollard's Rho below takes already a while.[133X621622[4X[32X Example [32X[104X623[4X[25Xgap>[125X [27XFactorsInt( -Factorial(6) );[127X[104X624[4X[28X[ -2, 2, 2, 2, 3, 3, 5 ][128X[104X625[4X[25Xgap>[125X [27XSet( FactorsInt( Factorial(13)/11 ) );[127X[104X626[4X[28X[ 2, 3, 5, 7, 13 ][128X[104X627[4X[25Xgap>[125X [27XFactorsInt( 2^63 - 1 );[127X[104X628[4X[28X[ 7, 7, 73, 127, 337, 92737, 649657 ][128X[104X629[4X[25Xgap>[125X [27XFactorsInt( 10^42 + 1 );[127X[104X630[4X[28X[ 29, 101, 281, 9901, 226549, 121499449, 4458192223320340849 ][128X[104X631[4X[25Xgap>[125X [27XFactorsInt(2^256+1:RhoTrials:=100000000);[127X[104X632[4X[28X[ 1238926361552897, [128X[104X633[4X[28X 93461639715357977769163558199606896584051237541638188580280321 ][128X[104X634[4X[32X[104X635636[1X14.4-8 PrimeDivisors[101X637638[33X[1;0Y[29X[2XPrimeDivisors[102X( [3Xn[103X ) [32X attribute[133X639640[33X[0;0Y[2XPrimeDivisors[102X returns for a non-zero integer [3Xn[103X a set of its positive641(probable) primes divisors. In rare cases the result could contain a642composite number which passed certain primality tests, see643[2XIsProbablyPrimeInt[102X ([14X14.4-2[114X) and [2XFactorsInt[102X ([14X14.4-7[114X) for more details.[133X644645[4X[32X Example [32X[104X646[4X[25Xgap>[125X [27XPrimeDivisors(-12);[127X[104X647[4X[28X[ 2, 3 ][128X[104X648[4X[25Xgap>[125X [27XPrimeDivisors(1);[127X[104X649[4X[28X[ ][128X[104X650[4X[32X[104X651652[1X14.4-9 PartialFactorization[101X653654[33X[1;0Y[29X[2XPartialFactorization[102X( [3Xn[103X[, [3Xeffort[103X] ) [32X operation[133X655656[33X[0;0Y[2XPartialFactorization[102X returns a partial factorization of the integer [3Xn[103X. No657assertions are made about the primality of the factors, except of those658mentioned below.[133X659660[33X[0;0YThe argument [3Xeffort[103X, if given, specifies how intensively the function should661try to determine factors of [3Xn[103X. The default is [3Xeffort[103X = 5.[133X662663[30X [33X[0;6YIf [3Xeffort[103X = 0, trial division by the primes below 100 is done.664Returned factors below [22X10^4[122X are guaranteed to be prime.[133X665666[30X [33X[0;6YIf [3Xeffort[103X = 1, trial division by the primes below 1000 is done.667Returned factors below [22X10^6[122X are guaranteed to be prime.[133X668669[30X [33X[0;6YIf [3Xeffort[103X = 2, additionally trial division by the numbers in the lists670[10XPrimes2[110X and [10XProbablePrimes2[110X is done, and perfect powers are detected.671Returned factors below [22X10^6[122X are guaranteed to be prime.[133X672673[30X [33X[0;6YIf [3Xeffort[103X = 3, additionally [10XFactorsRho[110X (Pollard's Rho) with [10XRhoTrials[110X674= 256 is used.[133X675676[30X [33X[0;6YIf [3Xeffort[103X = 4, as above, but [10XRhoTrials[110X = 2048.[133X677678[30X [33X[0;6YIf [3Xeffort[103X = 5, as above, but [10XRhoTrials[110X = 8192. Returned factors below679[22X10^12[122X are guaranteed to be prime, and all prime factors below [22X10^6[122X are680guaranteed to be found.[133X681682[30X [33X[0;6YIf [3Xeffort[103X = 6 and the package [5XFactInt[105X is loaded, in addition to the683above quite a number of special cases are handled.[133X684685[30X [33X[0;6YIf [3Xeffort[103X = 7 and the package [5XFactInt[105X is loaded, the only thing which686is not attempted to obtain a full factorization into687Baillie-Pomerance-Selfridge-Wagstaff pseudoprimes is the application688of the MPQS to a remaining composite with more than 50 decimal digits.[133X689690[33X[0;0YIncreasing the value of the argument [3Xeffort[103X by one usually results in an691increase of the runtime requirements by a factor of (very roughly!) 3 to 10.692(Also see [2XCheapFactorsInt[102X ([14XEDIM: CheapFactorsInt[114X)).[133X693694[4X[32X Example [32X[104X695[4X[25Xgap>[125X [27XList([0..5],i->PartialFactorization(97^35-1,i)); [127X[104X696[4X[28X[ [ 2, 2, 2, 2, 2, 3, 11, 31, 43, [128X[104X697[4X[28X 2446338959059521520901826365168917110105972824229555319002965029 ], [128X[104X698[4X[28X [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, [128X[104X699[4X[28X 2529823122088440042297648774735177983563570655873376751812787 ],[128X[104X700[4X[28X [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, [128X[104X701[4X[28X 2529823122088440042297648774735177983563570655873376751812787 ],[128X[104X702[4X[28X [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, [128X[104X703[4X[28X 242549173950325921859769421435653153445616962914227 ], [128X[104X704[4X[28X [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 687121, [128X[104X705[4X[28X 352993394104278463123335513593170858474150787 ], [128X[104X706[4X[28X [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 687121, [128X[104X707[4X[28X 20241187, 504769301, 34549173843451574629911361501 ] ][128X[104X708[4X[32X[104X709710[1X14.4-10 PrintFactorsInt[101X711712[33X[1;0Y[29X[2XPrintFactorsInt[102X( [3Xn[103X ) [32X function[133X713714[33X[0;0Yprints the prime factorization of the integer [3Xn[103X in human-readable form.[133X715716[4X[32X Example [32X[104X717[4X[25Xgap>[125X [27XPrintFactorsInt( Factorial( 7 ) ); Print( "\n" );[127X[104X718[4X[28X2^4*3^2*5*7[128X[104X719[4X[32X[104X720721[1X14.4-11 PrimePowersInt[101X722723[33X[1;0Y[29X[2XPrimePowersInt[102X( [3Xn[103X ) [32X function[133X724725[33X[0;0Yreturns the prime factorization of the integer [3Xn[103X as a list [22X[ p_1, e_1, ...,726p_k, e_k ][122X with [3Xn[103X = [22Xp_1^{e_1} ⋅ p_2^{e_2} ⋅ ... ⋅ p_k^{e_k}[122X.[133X727728[4X[32X Example [32X[104X729[4X[25Xgap>[125X [27XPrimePowersInt( Factorial( 7 ) );[127X[104X730[4X[28X[ 2, 4, 3, 2, 5, 1, 7, 1 ][128X[104X731[4X[32X[104X732733[1X14.4-12 DivisorsInt[101X734735[33X[1;0Y[29X[2XDivisorsInt[102X( [3Xn[103X ) [32X function[133X736737[33X[0;0Y[2XDivisorsInt[102X returns a list of all divisors of the integer [3Xn[103X. The list is738sorted, so that it starts with 1 and ends with [3Xn[103X. We define that739[10XDivisorsInt( -[3Xn[103X[10X ) = DivisorsInt( [3Xn[103X[10X )[110X.[133X740741[33X[0;0YSince the set of divisors of 0 is infinite calling [10XDivisorsInt( 0 )[110X causes742an error.[133X743744[33X[0;0Y[2XDivisorsInt[102X may call [2XFactorsInt[102X ([14X14.4-7[114X) to obtain the prime factors. [2XSigma[102X745([14X15.5-1[114X) and [2XTau[102X ([14X15.5-2[114X) compute the sum and the number of positive746divisors, respectively.[133X747748[4X[32X Example [32X[104X749[4X[25Xgap>[125X [27XDivisorsInt( 1 ); DivisorsInt( 20 ); DivisorsInt( 541 );[127X[104X750[4X[28X[ 1 ][128X[104X751[4X[28X[ 1, 2, 4, 5, 10, 20 ][128X[104X752[4X[28X[ 1, 541 ][128X[104X753[4X[32X[104X754755756[1X14.5 [33X[0;0YResidue Class Rings[133X[101X757758[33X[0;0Y[2XZmodnZ[102X ([14X14.5-2[114X) returns a residue class ring of [2XIntegers[102X ([14X14[114X) modulo an759ideal. These residue class rings are rings, thus all operations for rings760(see Chapter [14X56[114X) apply. See also Chapters [14X59[114X and [14X15[114X.[133X761762[1X14.5-1 \mod[101X763764[33X[1;0Y[29X[2X\mod[102X( [3Xr/s[103X, [3Xn[103X ) [32X operation[133X765766[33X[0;0YIf [3Xr[103X, [3Xs[103X and [3Xn[103X are integers, [10X[3Xr[103X[10X / [3Xs[103X[10X[110X as a reduced fraction is [10Xp/q[110X, where [10Xq[110X and767[3Xn[103X are coprime, then [10X[3Xr[103X[10X / [3Xs[103X[10X mod [3Xn[103X[10X[110X is defined to be the product of [10Xp[110X and the768inverse of [10Xq[110X modulo [3Xn[103X. See Section [14X4.13[114X for more details and definitions.[133X769770[33X[0;0YWith the above definition, [10X4 / 6 mod 32[110X equals [10X2 / 3 mod 32[110X and hence exists771(and is equal to 22), despite the fact that 6 has no inverse modulo 32.[133X772773[1X14.5-2 ZmodnZ[101X774775[33X[1;0Y[29X[2XZmodnZ[102X( [3Xn[103X ) [32X function[133X776[33X[1;0Y[29X[2XZmodpZ[102X( [3Xp[103X ) [32X function[133X777[33X[1;0Y[29X[2XZmodpZNC[102X( [3Xp[103X ) [32X function[133X778779[33X[0;0Y[2XZmodnZ[102X returns a ring [22XR[122X isomorphic to the residue class ring of the integers780modulo the ideal generated by [3Xn[103X. The element corresponding to the residue781class of the integer [22Xi[122X in this ring can be obtained by [10Xi * One( R )[110X, and a782representative of the residue class corresponding to the element [22Xx ∈ R[122X can783be computed by [10XInt[110X[22X( x )[122X.[133X784785[33X[0;0Y[10XZmodnZ( [3Xn[103X[10X )[110X is equal to [10XIntegers mod [3Xn[103X[10X[110X.[133X786787[33X[0;0Y[2XZmodpZ[102X does the same if the argument [3Xp[103X is a prime integer, additionally the788result is a field. [2XZmodpZNC[102X omits the check whether [3Xp[103X is a prime.[133X789790[33X[0;0YEach ring returned by these functions contains the whole family of its791elements if [3Xn[103X is not a prime, and is embedded into the family of finite792field elements of characteristic [3Xn[103X if [3Xn[103X is a prime.[133X793794[1X14.5-3 ZmodnZObj[101X795796[33X[1;0Y[29X[2XZmodnZObj[102X( [3XFam[103X, [3Xr[103X ) [32X operation[133X797[33X[1;0Y[29X[2XZmodnZObj[102X( [3Xr[103X, [3Xn[103X ) [32X operation[133X798799[33X[0;0YIf the first argument is a residue class family [3XFam[103X then [2XZmodnZObj[102X returns800the element in [3XFam[103X whose coset is represented by the integer [3Xr[103X.[133X801802[33X[0;0YIf the two arguments are an integer [3Xr[103X and a positive integer [3Xn[103X then803[2XZmodnZObj[102X returns the element in [10XZmodnZ( [3Xn[103X[10X )[110X (see [2XZmodnZ[102X ([14X14.5-2[114X)) whose804coset is represented by the integer [3Xr[103X.[133X805806[4X[32X Example [32X[104X807[4X[25Xgap>[125X [27Xr:= ZmodnZ(15);[127X[104X808[4X[28X(Integers mod 15)[128X[104X809[4X[25Xgap>[125X [27Xfam:=ElementsFamily(FamilyObj(r));;[127X[104X810[4X[25Xgap>[125X [27Xa:= ZmodnZObj(fam,9);[127X[104X811[4X[28XZmodnZObj( 9, 15 )[128X[104X812[4X[25Xgap>[125X [27Xa+a;[127X[104X813[4X[28XZmodnZObj( 3, 15 )[128X[104X814[4X[25Xgap>[125X [27XInt(a+a);[127X[104X815[4X[28X3[128X[104X816[4X[32X[104X817818[1X14.5-4 IsZmodnZObj[101X819820[33X[1;0Y[29X[2XIsZmodnZObj[102X( [3Xobj[103X ) [32X Category[133X821[33X[1;0Y[29X[2XIsZmodnZObjNonprime[102X( [3Xobj[103X ) [32X Category[133X822[33X[1;0Y[29X[2XIsZmodpZObj[102X( [3Xobj[103X ) [32X Category[133X823[33X[1;0Y[29X[2XIsZmodpZObjSmall[102X( [3Xobj[103X ) [32X Category[133X824[33X[1;0Y[29X[2XIsZmodpZObjLarge[102X( [3Xobj[103X ) [32X Category[133X825826[33X[0;0YThe elements in the rings [22XZ / n Z[122X are in the category [2XIsZmodnZObj[102X. If [22Xn[122X is a827prime then the elements are of course also in the category [2XIsFFE[102X ([14X59.1-1[114X),828otherwise they are in [2XIsZmodnZObjNonprime[102X. [2XIsZmodpZObj[102X is an abbreviation of829[10XIsZmodnZObj and IsFFE[110X. This category is the disjoint union of830[2XIsZmodpZObjSmall[102X and [2XIsZmodpZObjLarge[102X, the former containing all elements831with [22Xn[122X at most [10XMAXSIZE_GF_INTERNAL[110X.[133X832833[33X[0;0YThe reasons to distinguish the prime case from the nonprime case are[133X834835[30X [33X[0;6Ythat objects in [2XIsZmodnZObjNonprime[102X have an external representation836(namely the residue in the range [22X[ 0, 1, ..., n-1 ][122X),[133X837838[30X [33X[0;6Ythat the comparison of elements can be defined as comparison of the839residues, and[133X840841[30X [33X[0;6Ythat the elements lie in a family of type [10XIsZmodnZObjNonprimeFamily[110X842(note that for prime [22Xn[122X, the family must be an [10XIsFFEFamily[110X).[133X843844[33X[0;0YThe reasons to distinguish the small and the large case are that for small [22Xn[122X845the elements must be compatible with the internal representation of finite846field elements, whereas we are free to define comparison as comparison of847residues for large [22Xn[122X.[133X848849[33X[0;0YNote that we [13Xcannot[113X claim that every finite field element of degree 1 is in850[2XIsZmodnZObj[102X, since finite field elements in internal representation may not851know that they lie in the prime field.[133X852853854[1X14.6 [33X[0;0YCheck Digits[133X[101X855856[1X14.6-1 CheckDigitISBN[101X857858[33X[1;0Y[29X[2XCheckDigitISBN[102X( [3Xn[103X ) [32X function[133X859[33X[1;0Y[29X[2XCheckDigitISBN13[102X( [3Xn[103X ) [32X function[133X860[33X[1;0Y[29X[2XCheckDigitPostalMoneyOrder[102X( [3Xn[103X ) [32X function[133X861[33X[1;0Y[29X[2XCheckDigitUPC[102X( [3Xn[103X ) [32X function[133X862863[33X[0;0YThese functions can be used to compute, or check, check digits for some864everyday items. In each case what is submitted as input is either the number865with check digit (in which case the function returns [10Xtrue[110X or [10Xfalse[110X), or the866number without check digit (in which case the function returns the missing867check digit). The number can be specified as integer, as string (for example868in case of leading zeros) or as a sequence of arguments, each representing a869single digit. The check digits tested are the 10-digit ISBN (International870Standard Book Number) using [2XCheckDigitISBN[102X (since arithmetic is module 11, a871digit 11 is represented by an X); the newer 13-digit ISBN-13 using872[2XCheckDigitISBN13[102X; the numbers of 11-digit US postal money orders using873[2XCheckDigitPostalMoneyOrder[102X; and the 12-digit UPC bar code found on groceries874using [2XCheckDigitUPC[102X.[133X875876[4X[32X Example [32X[104X877[4X[25Xgap>[125X [27XCheckDigitISBN("052166103");[127X[104X878[4X[28XCheck Digit is 'X'[128X[104X879[4X[28X'X'[128X[104X880[4X[25Xgap>[125X [27XCheckDigitISBN("052166103X");[127X[104X881[4X[28XChecksum test satisfied[128X[104X882[4X[28Xtrue[128X[104X883[4X[25Xgap>[125X [27XCheckDigitISBN(0,5,2,1,6,6,1,0,3,1);[127X[104X884[4X[28XChecksum test failed[128X[104X885[4X[28Xfalse[128X[104X886[4X[25Xgap>[125X [27XCheckDigitISBN(0,5,2,1,6,6,1,0,3,'X'); # note single quotes![127X[104X887[4X[28XChecksum test satisfied[128X[104X888[4X[28Xtrue[128X[104X889[4X[25Xgap>[125X [27XCheckDigitISBN13("9781420094527");[127X[104X890[4X[28XChecksum test satisfied[128X[104X891[4X[28Xtrue[128X[104X892[4X[25Xgap>[125X [27XCheckDigitUPC("07164183001");[127X[104X893[4X[28XCheck Digit is 1[128X[104X894[4X[28X1[128X[104X895[4X[25Xgap>[125X [27XCheckDigitPostalMoneyOrder(16786457155);[127X[104X896[4X[28XChecksum test satisfied[128X[104X897[4X[28Xtrue[128X[104X898[4X[32X[104X899900[1X14.6-2 CheckDigitTestFunction[101X901902[33X[1;0Y[29X[2XCheckDigitTestFunction[102X( [3Xl[103X, [3Xm[103X, [3Xf[103X ) [32X function[133X903904[33X[0;0YThis function creates check digit test functions such as [2XCheckDigitISBN[102X905([14X14.6-1[114X) for check digit schemes that use the inner products with a fixed906vector modulo a number. The scheme creates will use strings of [3Xl[103X digits907(including the check digits), the check consists of taking the standard908product of the vector of digits with the fixed vector [3Xf[103X modulo [3Xm[103X; the result909needs to be 0. The function returns a function that then can be used for910testing or determining check digits.[133X911912[4X[32X Example [32X[104X913[4X[25Xgap>[125X [27Xisbntest:=CheckDigitTestFunction(10,11,[1,2,3,4,5,6,7,8,9,-1]); [127X[104X914[4X[28Xfunction( arg... ) ... end[128X[104X915[4X[25Xgap>[125X [27Xisbntest("038794680");[127X[104X916[4X[28XCheck Digit is 2[128X[104X917[4X[28X2[128X[104X918[4X[32X[104X919920921[1X14.7 [33X[0;0YRandom Sources[133X[101X922923[33X[0;0Y[5XGAP[105X provides [2XRandom[102X ([14X30.7-1[114X) methods for many collections of objects. On a924lower level these methods use [13Xrandom sources[113X which provide random integers925and random choices from lists.[133X926927[1X14.7-1 IsRandomSource[101X928929[33X[1;0Y[29X[2XIsRandomSource[102X( [3Xobj[103X ) [32X Category[133X930931[33X[0;0YThis is the category of random source objects which are defined to have, for932an object [3Xrs[103X in this category, methods available for the following933operations which are explained in more detail below: [10XRandom( [3Xrs[103X[10X, [3Xlist[103X[10X )[110X934giving a random element of a list, [10XRandom( [3Xrs[103X[10X, [3Xlow[103X[10X, [3Xhigh[103X[10X )[110X giving a random935integer between [3Xlow[103X and [3Xhigh[103X (inclusive), [2XInit[102X ([14X14.7-3[114X), [2XState[102X ([14X14.7-3[114X) and936[2XReset[102X ([14X14.7-3[114X).[133X937938[33X[0;0YUse [2XRandomSource[102X ([14X14.7-5[114X) to construct new random sources.[133X939940[33X[0;0YOne idea behind providing several independent (pseudo) random sources is to941make algorithms which use some sort of random choices deterministic. They942can use their own new random source created with a fixed seed and so do943exactly the same in different calls.[133X944945[33X[0;0YRandom source objects lie in the family [10XRandomSourcesFamily[110X.[133X946947[1X14.7-2 Random[101X948949[33X[1;0Y[29X[2XRandom[102X( [3Xrs[103X, [3Xlist[103X ) [32X operation[133X950[33X[1;0Y[29X[2XRandom[102X( [3Xrs[103X, [3Xlow[103X, [3Xhigh[103X ) [32X operation[133X951952[33X[0;0YThis operation returns a random element from list [3Xlist[103X, or an integer in the953range from the given (possibly large) integers [3Xlow[103X to [3Xhigh[103X, respectively.[133X954955[33X[0;0YThe choice should only depend on the random source [3Xrs[103X and have no effect on956other random sources.[133X957958[4X[32X Example [32X[104X959[4X[25Xgap>[125X [27Xmysource := RandomSource(IsMersenneTwister, 42);;[127X[104X960[4X[25Xgap>[125X [27XRandom(mysource, 1, 10^60);[127X[104X961[4X[28X999331861769949319194941485000557997842686717712198687315183[128X[104X962[4X[32X[104X963964[1X14.7-3 State[101X965966[33X[1;0Y[29X[2XState[102X( [3Xrs[103X ) [32X operation[133X967[33X[1;0Y[29X[2XReset[102X( [3Xrs[103X[, [3Xseed[103X] ) [32X operation[133X968[33X[1;0Y[29X[2XInit[102X( [3Xprers[103X[, [3Xseed[103X] ) [32X operation[133X969970[33X[0;0YThese are the basic operations for which random sources (see [2XIsRandomSource[102X971([14X14.7-1[114X)) must have methods.[133X972973[33X[0;0Y[2XState[102X should return a data structure which allows to recover the state of974the random source such that a sequence of random calls using this random975source can be reproduced. If a random source cannot be reset (say, it uses976truly random physical data) then [2XState[102X should return [9Xfail[109X.[133X977978[33X[0;0Y[10XReset( [3Xrs[103X[10X, [3Xseed[103X[10X )[110X resets the random source [3Xrs[103X to a state described by [3Xseed[103X,979if the random source can be reset (otherwise it should do nothing). Here980[3Xseed[103X can be an output of [2XState[102X and then should reset to that state. Also,981the methods should always allow integers as [3Xseed[103X. Without the [3Xseed[103X argument982the default [22X[3Xseed[103X = 1[122X is used.[133X983984[33X[0;0Y[2XInit[102X is the constructor of a random source, it gets an empty component985object [3Xprers[103X which has already the correct type and should fill in the986actual data which are needed. Optionally, it should allow one to specify a987[3Xseed[103X for the initial state, as explained for [2XReset[102X.[133X988989[33X[0;0YMost methods for [2XRandom[102X ([14X30.7-1[114X) in the [5XGAP[105X library use the990[2XGlobalMersenneTwister[102X ([14X14.7-4[114X) as random source. It can be reset into a991known state as in the following example.[133X992993[4X[32X Example [32X[104X994[4X[25Xgap>[125X [27Xseed := State(GlobalMersenneTwister);;[127X[104X995[4X[25Xgap>[125X [27XList([1..10],i->Random(Integers));[127X[104X996[4X[28X[ 2, -1, -2, -1, -1, 1, -4, 1, 0, -1 ][128X[104X997[4X[25Xgap>[125X [27XList([1..10],i->Random(Integers));[127X[104X998[4X[28X[ -1, -1, 1, -1, 1, -2, -1, -2, 0, -1 ][128X[104X999[4X[25Xgap>[125X [27XReset(GlobalMersenneTwister, seed);;[127X[104X1000[4X[25Xgap>[125X [27XList([1..10],i->Random(Integers));[127X[104X1001[4X[28X[ 2, -1, -2, -1, -1, 1, -4, 1, 0, -1 ][128X[104X1002[4X[32X[104X10031004[1X14.7-4 IsMersenneTwister[101X10051006[33X[1;0Y[29X[2XIsMersenneTwister[102X( [3Xrs[103X ) [32X Category[133X1007[33X[1;0Y[29X[2XIsGAPRandomSource[102X( [3Xrs[103X ) [32X Category[133X1008[33X[1;0Y[29X[2XIsGlobalRandomSource[102X( [3Xrs[103X ) [32X Category[133X1009[33X[1;0Y[29X[2XGlobalMersenneTwister[102X[32X global variable[133X1010[33X[1;0Y[29X[2XGlobalRandomSource[102X[32X global variable[133X10111012[33X[0;0YCurrently, the [5XGAP[105X library provides three types of random sources,1013distinguished by the three listed categories.[133X10141015[33X[0;0Y[2XIsMersenneTwister[102X are random sources which use a fast random generator of 321016bit numbers, called the Mersenne twister. The pseudo random sequence has a1017period of [22X2^19937-1[122X and the numbers have a [22X623[122X-dimensional equidistribution.1018For more details and the origin of the code used in the [5XGAP[105X kernel, see:1019[7Xhttp://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html[107X.[133X10201021[33X[0;0YUse the Mersenne twister if possible, in particular for generating many1022large random integers.[133X10231024[33X[0;0YThere is also a predefined global random source [2XGlobalMersenneTwister[102X which1025is used by most of the library methods for [2XRandom[102X ([14X30.7-1[114X).[133X10261027[33X[0;0Y[2XIsGAPRandomSource[102X uses the same number generator as [2XIsGlobalRandomSource[102X,1028but you can create several of these random sources which generate their1029random numbers independently of all other random sources.[133X10301031[33X[0;0Y[2XIsGlobalRandomSource[102X gives access to the [13Xclassical[113X global random generator1032which was used by [5XGAP[105X in former releases. You do not need to construct new1033random sources of this kind which would all use the same global data1034structure. Just use the existing random source [2XGlobalRandomSource[102X. This uses1035the additive random number generator described in [Knu98] (Algorithm A1036in 3.2.2 with lag [22X30[122X).[133X10371038[1X14.7-5 RandomSource[101X10391040[33X[1;0Y[29X[2XRandomSource[102X( [3Xcat[103X[, [3Xseed[103X] ) [32X operation[133X10411042[33X[0;0YThis operation is used to create new random sources. The first argument [3Xcat[103X1043is the category describing the type of the random generator, an optional1044[3Xseed[103X which can be an integer or a type specific data structure can be given1045to specify the initial state.[133X10461047[4X[32X Example [32X[104X1048[4X[25Xgap>[125X [27Xrs1 := RandomSource(IsMersenneTwister);[127X[104X1049[4X[28X<RandomSource in IsMersenneTwister>[128X[104X1050[4X[25Xgap>[125X [27Xstate1 := State(rs1);;[127X[104X1051[4X[25Xgap>[125X [27Xl1 := List([1..10000], i-> Random(rs1, [1..6]));; [127X[104X1052[4X[25Xgap>[125X [27Xrs2 := RandomSource(IsMersenneTwister);;[127X[104X1053[4X[25Xgap>[125X [27Xl2 := List([1..10000], i-> Random(rs2, [1..6]));;[127X[104X1054[4X[25Xgap>[125X [27Xl1 = l2;[127X[104X1055[4X[28Xtrue[128X[104X1056[4X[25Xgap>[125X [27Xl1 = List([1..10000], i-> Random(rs1, [1..6])); [127X[104X1057[4X[28Xfalse[128X[104X1058[4X[25Xgap>[125X [27Xn := Random(rs1, 1, 2^220);[127X[104X1059[4X[28X1077726777923092117987668044202944212469136000816111066409337432400[128X[104X1060[4X[32X[104X1061106210631064