home *** CD-ROM | disk | FTP | other *** search
/ Math Solutions 1995 October / Math_Solutions_CD-ROM_Walnut_Creek_October_1995.iso / pc / mac / discrete / doc / integer.tex < prev    next >
Encoding:
Text File  |  1993-05-05  |  25.9 KB  |  792 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%
  3. %A  integer.tex                 GAP documentation            Martin Schoenert
  4. %%
  5. %A  @(#)$Id: integer.tex,v 3.12 1993/02/19 10:48:42 gap Exp $
  6. %%
  7. %Y  Copyright 1990-1992,  Lehrstuhl D fuer Mathematik,  RWTH Aachen,  Germany
  8. %%
  9. %%  This file contains descriptions of the  integer datatype,  the operations
  10. %%  and the functions.
  11. %%
  12. %H  $Log: integer.tex,v $
  13. %H  Revision 3.12  1993/02/19  10:48:42  gap
  14. %H  adjustments in line length and spelling
  15. %H
  16. %H  Revision 3.11  1993/02/18  15:06:02  felsch
  17. %H  anothe example fixed
  18. %H
  19. %H  Revision 3.10  1993/02/12  16:08:27  felsch
  20. %H  more examples fixed
  21. %H
  22. %H  Revision 3.9  1993/02/01  13:38:07  felsch
  23. %H  examples fixed
  24. %H
  25. %H  Revision 3.8  1992/04/06  16:26:54  martin
  26. %H  fixed some more typos
  27. %H
  28. %H  Revision 3.7  1992/04/02  21:06:23  martin
  29. %H  changed *domain functions* to *set theoretic functions*
  30. %H
  31. %H  Revision 3.6  1992/03/27  18:55:07  martin
  32. %H  added another Mersenne prime exponent
  33. %H
  34. %H  Revision 3.5  1991/12/30  09:29:21  martin
  35. %H  changed incorrect reference to "SmallestRoot"
  36. %H
  37. %H  Revision 3.4  1991/12/27  16:07:04  martin
  38. %H  revised everything for GAP 3.1 manual
  39. %H
  40. %H  Revision 3.3  1991/07/26  12:34:01  martin
  41. %H  improved the index
  42. %H
  43. %H  Revision 3.2  1991/07/26  09:01:07  martin
  44. %H  changed |\GAP\ | to |{\GAP}|
  45. %H
  46. %H  Revision 3.1  1991/07/25  16:16:59  martin
  47. %H  fixed some minor typos
  48. %H
  49. %H  Revision 3.0  1991/04/11  11:31:19  martin
  50. %H  Initial revision under RCS
  51. %H
  52. %%
  53. \Chapter{Integers}%
  54. \index{type!integer}
  55.  
  56. One of the most  fundamental  datatypes in every programming  language is
  57. the integer type.  {\GAP} is no exception.
  58.  
  59. {\GAP} integers are entered  as a sequence  of digits optionally preceded
  60. by a '+' sign for positive integers or a '-'  sign for negative integers.
  61. The size of integers in {\GAP} is only limited by the amount of available
  62. memory,  so you can compute with integers having thousands of digits.
  63.  
  64. |    gap> -1234;
  65.     -1234
  66.     gap> 123456789012345678901234567890123456789012345678901234567890;
  67.     123456789012345678901234567890123456789012345678901234567890 |
  68.  
  69. The first sections in this  chapter describe the operations applicable to
  70. integers  (see  "Comparisons of  Integers",  "Operations  for  Integers",
  71. "QuoInt" and "RemInt").
  72.  
  73. The  next sections describe the  functions that test whether an object is
  74. an integer (see "IsInt") and convert objects of various types to integers
  75. (see "Int").
  76.  
  77. The next sections describe functions related to  the ordering of integers
  78. (see "AbsInt", "SignInt").
  79.  
  80. The next section describes the function that computes a Chinese remainder
  81. (see "ChineseRem").
  82.  
  83. The next sections  describe  the  functions related  to  the  ordering of
  84. integers, logarithms, and roots ("LogInt", "RootInt", "SmallestRootInt").
  85.  
  86. The {\GAP} object 'Integers'  is the ring domain of all integers.  So all
  87. set theoretic functions  are also  applicable to this domain (see chapter
  88. "Domains"  and "Set  Functions for  Integers").  The  only serious use of
  89. this however seems to be the generation of random integers.
  90.  
  91. Since the  integers  form a  Euclidean ring  all  the ring  functions are
  92. applicable  to   integers  (see   chapter "Rings", "Ring   Functions  for
  93. Integers",  "Primes", "IsPrimeInt",  "IsPrimePowerInt",   "NextPrimeInt",
  94. "PrevPrimeInt",  "FactorsInt",  "DivisorsInt",  "Sigma",     "Tau",   and
  95. "MoebiusMu").
  96.  
  97. Since the integers are naturally embedded in  the field of  rationals all
  98. the field functions are applicable to  integers (see chapter "Fields" and
  99. "Field Functions for Rationals").
  100.  
  101. Many more functions that are mainly related to the prime residue group of
  102. integers modulo an integer are described in chapter "Number Theory".
  103.  
  104. The external functions are in the file 'LIBNAME/\"integer.g\"'.
  105.  
  106. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  107. \Section{Comparisons of Integers}%
  108. \index{comparisons!of integers}
  109.  
  110. '<n1> = <n2>' \\
  111. '<n1> \<> <n2>'
  112.  
  113. The  equality operator '='  evaluates to 'true'  if the integer   <n1> is
  114. equal to the integer <n2> and 'false' otherwise.  The inequality operator
  115. '\<>'  evaluates  to 'true'   if <n1>  is  not equal  to <n2> and 'false'
  116. otherwise.
  117.  
  118. Integers can also be compared to objects  of other types; of course, they
  119. are never equal.
  120.  
  121. |    gap> 1 = 1;
  122.     true
  123.     gap> 1 <> 0;
  124.     true
  125.     gap> 1 = (1,2);     # '(1,2)' is a permutation
  126.     false |
  127.  
  128. '<n1> \<\ <n2>' \\
  129. '<n1> \<= <n2>' \\
  130. '<n1> > <n2>' \\
  131. '<n1> >= <n2>'
  132.  
  133. The  operators  '\<',  '\<=', '>',  and '=>'  evaluate to  'true' if  the
  134. integer <n1> is  less  than,  less  than  or  equal to, greater than,  or
  135. greater than or equal to the integer <n2>, respectively.
  136.  
  137. Integers  can  also be  compared to  objects  of  other  types, they  are
  138. considered  smaller than any  other object,  except rationals, where  the
  139. ordering  reflects  the  ordering  of the rationals  (see "Comparisons of
  140. Rationals").
  141.  
  142. |    gap> 1 < 2;
  143.     true
  144.     gap> 1 < -1;
  145.     false
  146.     gap> 1 < 3/2;
  147.     true
  148.     gap> 1 < false;
  149.     true |
  150.  
  151. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  152. \Section{Operations for Integers}%
  153. \index{operations!for integers}
  154.  
  155. '<n1> + <n2>'
  156.  
  157. The operator '+' evaluates to the sum of the two integers <n1> and <n2>.
  158.  
  159. '<n1> - <n2>'
  160.  
  161. The operator '-' evaluates to the difference of the two integers <n1> and
  162. <n2>.
  163.  
  164. '<n1> \*\ <n2>'
  165.  
  166. The operator '\*' evaluates to the product of  the  two integers <n1> and
  167. <n2>.
  168.  
  169. '<n1> / <n2>'
  170.  
  171. The operator '/' evaluates to the quotient of the two  integers  <n1> and
  172. <n2>.  If the   divisor does not  divide  the dividend the  quotient is a
  173. rational (see "Rationals").  If the  divisor is 0  an error is signalled.
  174. The integer  part  of  the quotient can  be  computed with  'QuoInt' (see
  175. "QuoInt").
  176.  
  177. '<n1> mod <n2>'
  178.  
  179. The operator 'mod' evaluates  to the smallest positive representative  of
  180. the  residue  class of the left operand modulo  the right, i.e., '<i> mod
  181. <k>' is the unique <m> in the range '[0 ..  AbsInt(<k>)-1]' such that <k>
  182. divides '<i>  - <m>'.  If the right operand is  0 an error  is signalled.
  183. The  remainder of  the  division  can  be  computed  with  'RemInt'  (see
  184. "RemInt").
  185.  
  186. '<n1> \^\ <n2>'
  187.  
  188. The operator '\^' evaluates to the <n2>-th power of the integer <n1>.  If
  189. <n2> is a positive integer then '<n1>\^<n2>' is '<n1>\* <n1>\* ..\* <n1>'
  190. (<n2> factors).  If <n2> is a negative integer '<n1>\^<n2>' is defined as
  191. $1 / {<n1>^{-<n2>}}$.  If 0  is raised to  a negative power an  error  is
  192. signalled.  Any integer, even 0, raised to the zeroth power yields 1.
  193.  
  194. Since  integers  embed naturally into  the  field of  rationals  all  the
  195. rational  operations are available  for integers too (see "Operations for
  196. Rationals").
  197.  
  198. For the precedence of the operators see "Operations".
  199.  
  200. |    gap> 2 * 3 + 1;
  201.     7 |
  202.  
  203. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  204. \Section{QuoInt}%
  205. \index{integer part of a quotient}
  206.  
  207. 'QuoInt( <n1>, <n2> )'
  208.  
  209. 'QuoInt'  returns  the   integer part of   the  quotient  of  its integer
  210. operands.
  211.  
  212. If <n1>  and  <n2> are positive 'QuoInt(  <n1>,  <n2> )'  is the  largest
  213. positive integer <q> such that '<q>\*<n2> \<= <n1>'.  If <n1>  or <n2> or
  214. both are negative the absolute value of  the integer part of the quotient
  215. is the quotient of the absolute values of <n1> and <n2>,  and the sign of
  216. it is the product of the signs of <n1> and <n2>.
  217.  
  218. 'RemInt' (see "RemInt") can be used to compute the remainder.
  219.  
  220. |    gap> QuoInt(5,2);  QuoInt(-5,2);  QuoInt(5,-2);  QuoInt(-5,-2);
  221.     2
  222.     -2
  223.     -2
  224.     2 |
  225.  
  226. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  227. \Section{RemInt}%
  228. \index{remainder of a quotient}
  229.  
  230. 'RemInt( <n1>, <n2> )'
  231.  
  232. 'RemInt' returns the remainder of its two integer operands.
  233.  
  234. If  <n2> is not equal to  zero 'RemInt(  <n1>,   <n2> ) =   <n1> - <n2>\*
  235. QuoInt( <n1>,   <n2> )'.  Note   that  the rules  given for 'QuoInt' (see
  236. "QuoInt") imply that 'RemInt( <n1>, <n2> )' has the same sign as <n1> and
  237. its absolute  value is strictly  less  than the  absolute value  of <n2>.
  238. Dividing by 0 signals an error.
  239.  
  240. |    gap> RemInt(5,2);  RemInt(-5,2);  RemInt(5,-2);  RemInt(-5,-2);
  241.     1
  242.     -1
  243.     1
  244.     -1 |
  245.  
  246. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  247. \Section{IsInt}%
  248. \index{test!for an integer}
  249.  
  250. 'IsInt( <obj> )'
  251.  
  252. 'IsInt' returns 'true' if <obj>, which can be an  arbitrary object, is an
  253. integer and 'false' otherwise.  'IsInt' will signal an error if  <obj> is
  254. an unbound variable.
  255.  
  256. |    gap> IsInt( 1 );
  257.     true
  258.     gap> IsInt( IsInt );
  259.     false        # 'IsInt' is a function, not an integer |
  260.  
  261. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  262. \Section{Int}%
  263. \index{convert!to an integer}
  264.  
  265. 'Int( <obj> )'
  266.  
  267. 'Int' converts an  object <obj> to an  integer.   If <obj> is an  integer
  268. 'Int' will simply return <obj>.
  269.  
  270. If <obj> is a rational number (see "Rationals")  'Int' returns the unique
  271. integer that has  the same sign  as  <obj> and the largest absolute value
  272. not larger than the absolute value of <obj>.
  273.  
  274. If <obj> is an element  of the prime field  of a finite field <F>,  'Int'
  275. returns the least positive integer <n> such that '<n>\*  <F>.one = <obj>'
  276. (see "IntFFE").
  277.  
  278. If <obj> is not of one of the above types an error is signalled.
  279.  
  280. |    gap> Int( 17 );
  281.     17
  282.     gap> Int( 17 / 3 );
  283.     5
  284.     gap> Int( Z(5^3)^62 );
  285.     4  # $Z(5^3)^{62}=(Z(5^3)^{124/4})^2=Z(5)^2=PrimitiveRoot(5)^2=2^2$ |
  286.  
  287. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  288. \Section{AbsInt}%
  289. \index{absolute value of an integer}
  290.  
  291. 'AbsInt( <n> )'
  292.  
  293. 'AbsInt' returns the absolute value of the integer <n>,  i.e., <n> if <n>
  294. is positive, -<n> if <n> is negative and 0 if <n> is 0 (see "SignInt").
  295.  
  296. |    gap> AbsInt( 33 );
  297.     33
  298.     gap> AbsInt( -214378 );
  299.     214378
  300.     gap> AbsInt( 0 );
  301.     0 |
  302.  
  303. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  304. \Section{SignInt}%
  305. \index{sign!of an integer}
  306.  
  307. 'SignInt( <obj> )'
  308.  
  309. 'SignInt' returns  the  sign of the integer  <obj>, i.e.,  1 if <obj>  is
  310. positive, -1 if <obj> is negative and 0 if <obj> is 0 (see "AbsInt").
  311.  
  312. |    gap> SignInt( 33 );
  313.     1
  314.     gap> SignInt( -214378 );
  315.     -1
  316.     gap> SignInt( 0 );
  317.     0 |
  318.  
  319. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  320. \Section{ChineseRem}%
  321. \index{chinese remainder}
  322.  
  323. 'ChineseRem( <moduli>, <residues> )'
  324.  
  325. 'ChineseRem' returns the combination   of   the  <residues>  modulo   the
  326. <moduli>, i.e., the  unique integer <c>  from '[0..Lcm(<moduli>)-1]' such
  327. that  '<c>  = <residues>[i]' modulo '<moduli>[i]'   for  all  <i>, if  it
  328. exists.  If no such combination exists 'ChineseRem' signals an error.
  329.  
  330. Such    a    combination    does     exist    if    and     only     if\\
  331. '<residues>[<i>]=<residues>[<k>]'  mod 'Gcd(<moduli>[<i>],<moduli>[<k>])'
  332. for every pair <i>, <k>.  Note  that this implies that such a combination
  333. exists if the  moduli  are pairwise relatively prime.  This is called the
  334. Chinese remainder theorem.
  335.  
  336. |    gap> ChineseRem( [ 2, 3, 5, 7 ], [ 1, 2, 3, 4 ] );
  337.     53
  338.     gap> ChineseRem( [ 6, 10, 14 ], [ 1, 3, 5 ] );
  339.     103
  340.     gap> ChineseRem( [ 6, 10, 14 ], [ 1, 2, 3 ] );
  341.     Error, the residues must be equal modulo 2 |
  342.  
  343. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  344. \Section{LogInt}%
  345. \index{logarithm of an integer}
  346.  
  347. 'LogInt( <n>, <base> )'
  348.  
  349. 'LogInt'   returns  the  integer part  of  the logarithm of  the positive
  350. integer  <n> with  respect to   the positive integer   <base>, i.e.,  the
  351. largest  positive integer <exp> such  that $base^{exp}  \<= n$.  'LogInt'
  352. will signal an error if either <n> or <base> is not positive.
  353.  
  354. |    gap> LogInt( 1030, 2 );
  355.     10        # $2^{10} = 1024$
  356.     gap> LogInt( 1, 10 );
  357.     0 |
  358.  
  359. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  360. \Section{RootInt}%
  361. \index{root!of an integer}\index{square root!of an integer}
  362.  
  363. 'RootInt( <n> )' \\
  364. 'RootInt( <n>, <k> )'
  365.  
  366. 'RootInt' returns the integer part of the <k>th root  of the integer <n>.
  367. If the optional integer argument <k> is not given it defaults to 2, i.e.,
  368. 'RootInt' returns the integer part of the square root in this case.
  369.  
  370. If  <n> is positive  'RootInt' returns  the  largest positive integer $r$
  371. such that $r^k \<=  n$.  If <n>  is negative and  <k>  is  odd  'RootInt'
  372. returns '-RootInt( -<n>,  <k> )'.  If  <n> is negative   and <k> is  even
  373. 'RootInt' will cause an error.  'RootInt' will also cause an error if <k>
  374. is 0 or negative.
  375.  
  376. |    gap> RootInt( 361 );
  377.     19
  378.     gap> RootInt( 2 * 10^12 );
  379.     1414213
  380.     gap> RootInt( 17000, 5 );
  381.     7        # $7^5 = 16807$ |
  382.  
  383. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  384. \Section{SmallestRootInt}%
  385. \index{root!of an integer, smallest}
  386.  
  387. 'SmallestRootInt( <n> )'
  388.  
  389. 'SmallestRootInt' returns the smallest root of the integer <n>.
  390.  
  391. The  smallest  root of an  integer $n$  is  the  integer $r$  of smallest
  392. absolute  value for which  a  positive integer $k$ exists such  that $n =
  393. r^k$.
  394.  
  395. |    gap> SmallestRootInt( 2^30 );
  396.     2
  397.     gap> SmallestRootInt( -(2^30) );
  398.     -4        # note that $(-2)^{30} = +(2^{30})$
  399.     gap> SmallestRootInt( 279936 );
  400.     6
  401.     gap> LogInt( 279936, 6 );
  402.     7
  403.     gap> SmallestRootInt( 1001 );
  404.     1001 |
  405.  
  406. 'SmallestRootInt' can be used to  identify and decompose powers of primes
  407. as is demonstrated in the following example (see "IsPrimePowerInt")
  408.  
  409. |    p := SmallestRootInt( q );  n := LogInt( q, p );
  410.     if not IsPrimeInt(p) then Error("GF: <q> must be a primepower"); fi;|
  411.  
  412. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  413. \Section{Set Functions for Integers}
  414.  
  415. As already mentioned in the first section of  this chapter, 'Integers' is
  416. the  domain  of  all integers.   Thus  in  principle  all  set  theoretic
  417. functions, for  example 'Intersection', 'Size',  and so on can be applied
  418. to this domain.  This seems generally of little use.
  419.  
  420. |    gap> Intersection( Integers, [ 0, 1/2, 1, 3/2 ] );
  421.     [ 0, 1 ]
  422.     gap> Size( Integers );
  423.     "infinity" |
  424.  
  425. 'Random( Integers )'
  426.  
  427. This seems to be the only useful  domain function that can  be applied to
  428. the domain 'Integers'.  It returns pseudo random integers between -10 and
  429. 10 distributed according to a binomial distribution.
  430.  
  431. |    gap> Random( Integers );
  432.     1
  433.     gap> Random( Integers );
  434.     -4 |
  435.  
  436. To  generate  uniformly  distributed  integers from   a  range,  use  the
  437. construct 'Random( [ <low> .. <high> ] )'.
  438.  
  439. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  440. \Section{Ring Functions for Integers}
  441.  
  442. As was already noted  in the introduction  to this  chapter  the integers
  443. form  a Euclidean ring, so all  ring functions (see chapter "Rings")  are
  444. applicable to the integers.   This section comments on the implementation
  445. of those functions  for  the integers and tells you how  you can call the
  446. corresponding functions directly, for example to save time.
  447.  
  448. 'IsPrime( Integers, <n> )'
  449.  
  450. This is implemented by 'IsPrimeInt', which you can  call directly to save
  451. a little bit of time (see "IsPrimeInt").
  452.  
  453. 'Factors( Integers, <n> )'
  454.  
  455. This is  implemented as by 'FactorsInt', which  you  can call directly to
  456. save a little bit of time (see "FactorsInt").
  457.  
  458. 'EuclideanDegree( Integers, <n> )'
  459.  
  460. The Euclidean degree of an integer is of course simply the absolute value
  461. of the integer.  Calling 'AbsInt' directly will be a little bit faster.
  462.  
  463. 'Mod( Integers, <n>, <m> )'
  464.  
  465. This is implemented as '<n> mod <m>', which  you can use directly to save
  466. a lot of time.
  467.  
  468. 'QuotientMod( Integers, <n1>, <n2>, <m> )'
  469.  
  470. This  is implemented as   '(<n1> /  <n2>)  mod <m>',   which  you can use
  471. directly to save a lot of time.
  472.  
  473. 'PowerMod( Integers, <n>, <e>, <m> )'
  474.  
  475. This is implemented by 'PowerModInt', which you can call directly to save
  476. a  little bit  of  time.   Note that  using  '<n> \^\  <e> mod <m>'  will
  477. generally  be slower, because it can not reduce intermediate results like
  478. 'PowerMod'.
  479.  
  480. 'Gcd( Integers, <n1>, <n2>.. )'
  481.  
  482. This is implemented by  'GcdInt', which you  can call  directly to save a
  483. lot of time.  Note that 'GcdInt' takes only two arguments, not several as
  484. 'Gcd' does.
  485.  
  486. 'Gcdex( <n1>, <n2> )'
  487.  
  488. 'Gcdex'  returns a record.  The component  'gcd' is the   gcd of <n1> and
  489. <n2>.
  490.  
  491. The components   'coeff1' and  'coeff2' are  integer  cofactors such that\\
  492. '<g>.gcd =  <g>.coeff1\*<n1> +  <g>.coeff2\*<n2>'.\\
  493. If <n1> and <n2> both are nonzero, 'AbsInt( <g>.coeff1 )' is less than or
  494. equal to 'AbsInt(<n2>) / (2\*<g>.gcd)' and 'AbsInt( <g>.coeff2 )' is less
  495. than or equal to 'AbsInt(<n1>) / (2\*<g>.gcd)'.
  496.  
  497. The components 'coeff3' and 'coeff4'  are  integer  cofactors  such  that\\
  498. '0 = <g>.coeff3\*<n1>  + <g>.coeff4\*<n2>'.\\
  499. If <n1> or <n2>  or are  both nonzero  'coeff3' is '-<n2>  / <g>.gcd' and
  500. 'coeff4' is '<n1> / <g>.gcd'.
  501.  
  502. The coefficients always form a  unimodular matrix, i.e., the determinant\\
  503. '<g>.coeff1\*<g>.coeff4 -  <g>.coeff3\*<g>.coeff2'\\
  504. is 1 or -1.
  505.  
  506. |    gap> Gcdex( 123, 66 );
  507.     rec(
  508.       gcd := 3,
  509.       coeff1 := 7,
  510.       coeff2 := -13,
  511.       coeff3 := -22,
  512.       coeff4 := 41 )
  513.           # 3 = 7\*123 - 13\*66, 0 = -22\*123 + 41\*66
  514.     gap> Gcdex( 0, -3 );
  515.     rec(
  516.       gcd := 3,
  517.       coeff1 := 0,
  518.       coeff2 := -1,
  519.       coeff3 := 1,
  520.       coeff4 := 0 )
  521.     gap> Gcdex( 0, 0 );
  522.     rec(
  523.       gcd := 0,
  524.       coeff1 := 1,
  525.       coeff2 := 0,
  526.       coeff3 := 0,
  527.       coeff4 := 1 ) |
  528.  
  529. 'Lcm( Integers, <n1>, <n2>.. )'
  530.  
  531. This is implemented  as 'LcmInt', which  you can call directly to  save a
  532. little bit of  time.  Note that 'LcmInt'  takes  only two arguments,  not
  533. several as 'Lcm' does.
  534.  
  535. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  536. \Section{Primes}%
  537. \index{list!of primes}
  538.  
  539. 'Primes[ <n> ]'
  540.  
  541. 'Primes' is a set, i.e., a sorted list, of the 168 primes less than 1000.
  542.  
  543. 'Primes' is used in 'IsPrimeInt' (see "IsPrimeInt") and 'FactorsInt' (see
  544. "FactorsInt") to cast out small prime divisors quickly.
  545.  
  546. |    gap> Primes[1];
  547.     2
  548.     gap> Primes[100];
  549.     541 |
  550.  
  551. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  552. \Section{IsPrimeInt}%
  553. \index{test!for a prime}
  554.  
  555. 'IsPrimeInt( <n> )'
  556.  
  557. 'IsPrimeInt'  returns 'true' if  the integer  <n>  is a prime and 'false'
  558. otherwise.  By convention 'IsPrimeInt( 0 ) = IsPrimeInt( 1 ) = false' and
  559. we define 'IsPrimeInt( -<n> ) = IsPrimeInt( <n> )'.
  560.  
  561. Note  that 'IsPrimeInt' implements  a probabilistic   primality test.  If
  562. 'IsPrimeInt' returns 'false' then its argument definitely is not a prime.
  563. However if 'IsPrimeInt' returns  'true' there is a small  chance that <n>
  564. is composite.  This does not happen  for integers smaller than $25\*10^9$
  565. and happens with probability less than $0.1\%$ for larger integers.
  566.  
  567. The time taken by 'IsPrimeInt' is approximately proportional to the third
  568. power  of  the number  of  digits of <n>.   Testing numbers  with several
  569. hundreds digits is quite feasible.
  570.  
  571. |    gap> IsPrimeInt( 2^31 - 1 );
  572.     true
  573.     gap> IsPrimeInt( 10^42 + 1 );
  574.     false |
  575.  
  576. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  577. \Section{IsPrimePowerInt}%
  578. \index{test!for a power of a prime}
  579.  
  580. 'IsPrimePowerInt( <n> )'
  581.  
  582. 'IsPrimePowerInt' returns 'true' if the integer <n>  is a prime power and
  583. 'false' otherwise.
  584.  
  585. $n$ is a *prime power* if there exists a prime $p$ and a positive integer
  586. $i$ such that $p^i = n$.  If $n$ is negative the  condition is that there
  587. must exist a negative prime $p$ and an odd positive integer $i$ such that
  588. $p^i = n$.  1 and -1 are not prime powers.
  589.  
  590. Note    that 'IsPrimePowerInt'      uses       'SmallestRootInt'     (see
  591. "SmallestRootInt") and a probabilistic primality test (see "IsPrimeInt").
  592.  
  593. |    gap> IsPrimePowerInt( 31^5 );
  594.     true
  595.     gap> IsPrimePowerInt( 2^31-1 );
  596.     true        # $2^{31}-1$ is actually a prime
  597.     gap> IsPrimePowerInt( 2^63-1 );
  598.     false
  599.     gap> Filtered( [-10..10], IsPrimePowerInt );
  600.     [ -8, -7, -5, -3, -2, 2, 3, 4, 5, 7, 8, 9 ] |
  601.  
  602. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  603. \Section{NextPrimeInt}%
  604. \index{larger prime}
  605.  
  606. 'NextPrimeInt( <n> )'
  607.  
  608. 'NextPrimeInt' returns the smallest  prime which  is strictly larger than
  609. the integer <n>.
  610.  
  611. Note that 'NextPrimeInt' uses a probabilistic test (see "IsPrimeInt").
  612.  
  613. |    gap> NextPrimeInt( 541 );
  614.     547
  615.     gap> NextPrimeInt( -1 );
  616.     2 |
  617.  
  618. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  619. \Section{PrevPrimeInt}%
  620. \index{smaller prime}
  621.  
  622. 'PrevPrimeInt( <n> )'
  623.  
  624. 'PrevPrimeInt' returns the largest prime  which is  strictly smaller than
  625. the integer <n>.
  626.  
  627. Note that 'PrevPrimeInt' uses a probabilistic test (see "IsPrimeInt").
  628.  
  629. |    gap> PrevPrimeInt( 541 );
  630.     523
  631.     gap> PrevPrimeInt( 1 );
  632.     -2 |
  633.  
  634. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  635. \Section{FactorsInt}%
  636. \index{factorization!of an integer}
  637.  
  638. 'FactorsInt( <n> )'
  639.  
  640. 'FactorsInt' returns a list of the prime factors of the  integer <n>.  If
  641. the <i>th power of a prime divides <n> this prime appears <i> times.  The
  642. list is sorted, that is the smallest prime factors come first.  The first
  643. element has  the same  sign  as  <n>, the others   are positive.  For any
  644. integer <n> it holds that 'Product( FactorsInt( <n> ) ) = <n>'.
  645.  
  646. Note  that 'FactorsInt'   uses  a    probabilistic primality  test   (see
  647. "IsPrimeInt").  Thus  'FactorsInt'  might  return a   list which contains
  648. composite integers.
  649.  
  650. The time taken by   'FactorsInt'  is approximately  proportional to   the
  651. square root of the second largest prime factor  of <n>, which is the last
  652. one that 'FactorsInt'  has to find,   since the largest  factor is simply
  653. what remains when all others have been removed.  Thus the time is roughly
  654. bounded by  the fourth  root of <n>.   'FactorsInt' is guaranteed to find
  655. all factors   less than  $10^6$  and will find  most    factors less than
  656. $10^{10}$.    If <n>    contains   multiple  factors   larger  than  that
  657. 'FactorsInt' may not be able to factor <n> and will then signal an error.
  658.  
  659. |    gap> FactorsInt( -Factorial(6) );
  660.     [ -2, 2, 2, 2, 3, 3, 5 ]
  661.     gap> Set( FactorsInt( Factorial(13)/11 ) );
  662.     [ 2, 3, 5, 7, 13 ]
  663.     gap> FactorsInt( 2^63 - 1 );
  664.     [ 7, 7, 73, 127, 337, 92737, 649657 ]
  665.     gap> FactorsInt( 10^42 + 1 );
  666.     [ 29, 101, 281, 9901, 226549, 121499449, 4458192223320340849 ] |
  667.  
  668. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  669. \Section{DivisorsInt}%
  670. \index{divisors!of an integer}
  671.  
  672. 'DivisorsInt( <n> )'
  673.  
  674. 'DivisorsInt'  returns a list of all  positive  *divisors* of the integer
  675. <n>.  The list  is sorted, so  it starts with 1 and  ends with <n>.    We
  676. define 'DivisorsInt(  -<n> ) =  DivisorsInt(  <n> )'.   Since the  set of
  677. divisors of 0 is infinite calling 'DivisorsInt( 0 )' causes an error.
  678.  
  679. 'DivisorsInt' calls 'FactorsInt' (see  "FactorsInt")  to obtain the prime
  680. factors.  'Sigma' (see  "Sigma") computes the sum,  'Tau' (see "Tau") the
  681. number of positive divisors.
  682.  
  683. |    gap> DivisorsInt( 1 );
  684.     [ 1 ]
  685.     gap> DivisorsInt( 20 );
  686.     [ 1, 2, 4, 5, 10, 20 ]
  687.     gap> DivisorsInt( 541 );
  688.     [ 1, 541 ] |
  689.  
  690. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  691. \Section{Sigma}%
  692. \index{sum!of divisors of an integer}%
  693. \index{mersenne primes}\index{primes!mersenne}
  694.  
  695. 'Sigma( <n> )'
  696.  
  697. 'Sigma' returns  the sum of the  positive divisors (see "DivisorsInt") of
  698. the integer <n>.
  699.  
  700. 'Sigma' is a multiplicative arithmetic function, i.e., if $n$ and $m$ are
  701. relatively  prime we have $\sigma(n m) = \sigma(n)  \sigma(m)$.  Together
  702. with  the  formula $\sigma(p^e) = (p^{e+1}-1) / (p-1)$ this allows you to
  703. compute $\sigma(n)$.
  704.  
  705. Integers  $n$ for which $\sigma(n)=2 n$ are called perfect.  Even perfect
  706. integers are exactly of the form $2^{n-1}(2^n-1)$ where $2^n-1$ is prime.
  707. Primes of the form  $2^n-1$ are called *Mersenne  primes*, the known ones
  708. are obtained for $n =$ 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521,
  709. 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701,
  710. 23209, 44497, 86243, 110503, 132049, 216091, and 756839.  It is not known
  711. whether  odd  perfect integers exist,  however \cite{BC89} show  that any
  712. such integer must have at least 300 decimal digits.
  713.  
  714. 'Sigma' usually spends most of its time factoring <n> (see "FactorsInt").
  715.  
  716. |    gap> Sigma( 0 );
  717.     Error, Sigma: <n> must not be 0
  718.     gap> Sigma( 1 );
  719.     1
  720.     gap> Sigma( 1009 );
  721.     1010        # thus 1009 is a prime
  722.     gap> Sigma( 8128 ) = 2*8128;
  723.     true        # thus 8128 is a perfect number |
  724.  
  725. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  726. \Section{Tau}%
  727. \index{number!of divisors of an integer}
  728.  
  729. 'Tau( <n> )'
  730.  
  731. 'Tau' returns the number of  the positive divisors (see "DivisorsInt") of
  732. the integer <n>.
  733.  
  734. 'Tau' is a multiplicative  arithmetic function,  i.e., if $n$ and $m$ are
  735. relatively  prime we have $\tau(n  m) =  \tau(n) \tau(m)$.  Together with
  736. the formula $\tau(p^e) = e+1$ this allows us to compute $\tau(n)$.
  737.  
  738. 'Tau' usually spends most of its time factoring <n> (see "FactorsInt").
  739.  
  740. |    gap> Tau( 0 );
  741.     Error, Tau: <n> must not be 0
  742.     gap> Tau( 1 );
  743.     1
  744.     gap> Tau( 1013 );
  745.     2        # thus 1013 is a prime
  746.     gap> Tau( 8128 );
  747.     14
  748.     gap> Tau( 36 );
  749.     9        # $\tau(n)$ is odd if and only if $n$ is a perfect square |
  750.  
  751. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  752. \Section{MoebiusMu}%
  753. \index{Moebius inversion function}
  754.  
  755. 'MoebiusMu( <n> )'
  756.  
  757. 'MoebiusMu' computes the value of the *Moebius  function* for the integer
  758. <n>.  This is 0  for  integers which  are not squarefree, i.e., which are
  759. divisible by a square $r^2$.  Otherwise it is 1 if <n> has an even number
  760. and -1 if <n> has an odd number of prime factors.
  761.  
  762. The importance   of $\mu$ stems  from the   so called  inversion formula.
  763. Suppose $f(n)$  is a function  defined on the  positive integers and  let
  764. $g(n)=\sum_{d \mid n}{f(d)}$. Then $f(n)=\sum_{d \mid n}{\mu(d) g(n/d)}$.
  765. As a special case we have  $\phi(n) = \sum_{d  \mid n}{\mu(d) n/d}$ since
  766. $n = \sum_{d \mid n}{\phi(d)}$ (see "Phi").
  767.  
  768. 'MoebiusMu' usually   spends  all of   its    time   factoring <n>   (see
  769. "FactorsInt").
  770.  
  771. |    gap> MoebiusMu( 60 );
  772.     0
  773.     gap> MoebiusMu( 61 );
  774.     -1
  775.     gap> MoebiusMu( 62 );
  776.     1 |
  777.  
  778. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  779. %%
  780. %E  Emacs . . . . . . . . . . . . . . . . . . . . . local Emacs variables
  781. %%
  782. %%  Local Variables:
  783. %%  mode:               outline
  784. %%  outline-regexp:     "\\\\Chapter\\|\\\\Section"
  785. %%  fill-column:        73
  786. %%  eval:               (hide-body)
  787. %%  End:
  788. %%
  789.  
  790.  
  791.  
  792.