Debian Codegraph @

doc/ref/chap14.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <title>GAP (ref) - Chapter 14: Integers</title>
9 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
10 <meta name="generator" content="GAPDoc2HTML" />
11 <link rel="stylesheet" type="text/css" href="manual.css" />
12 <script src="manual.js" type="text/javascript"></script>
13 <script type="text/javascript">overwriteStyle();</script>
14 </head>
15 <body class="chap14" onload="jscontent()">
16
17
18 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
19
20 <div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap13.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap15.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap14_mj.html">[MathJax on]</a></p>
23 <p><a id="X853DF11B80068ED5" name="X853DF11B80068ED5"></a></p>
24 <div class="ChapSects"><a href="chap14.html#X853DF11B80068ED5">14 <span class="Heading">Integers</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X838230CE810107A3">14.1 <span class="Heading">Integers: Global Variables</span></a>
26 </span>
27 <div class="ContSSBlock">
28 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7E20D82B79DE5129">14.1-1 Integers</a></span>
29 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X818683B17F8C97F3">14.1-2 IsIntegers</a></span>
30 </div></div>
31 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X80CF510B8080C7CA">14.2 <span class="Heading">Elementary Operations for Integers</span></a>
32 </span>
33 <div class="ContSSBlock">
34 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X87AEADF07DC8303B">14.2-1 IsInt</a></span>
35 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X82A854757DFA9C76">14.2-2 IsPosInt</a></span>
36 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X87CA734380B5F68C">14.2-3 Int</a></span>
37 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X87DD1EEE7EF18036">14.2-4 IsEvenInt</a></span>
38 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8621BA927CD12EFB">14.2-5 IsOddInt</a></span>
39 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X782095927FB9F1DB">14.2-6 AbsInt</a></span>
40 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X842614817FE48D62">14.2-7 SignInt</a></span>
41 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8197C4E882BAF14E">14.2-8 LogInt</a></span>
42 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X83D9B5C87EEA2A77">14.2-9 RootInt</a></span>
43 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7F98A0CE7B9FD366">14.2-10 SmallestRootInt</a></span>
44 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X83B998E486893FED">14.2-11 IsSquareInt</a></span>
45 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X862D1BD786EFFDA9">14.2-12 ListOfDigits</a></span>
46 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8185784B7E228DEA">14.2-13 Random</a></span>
47 </div></div>
48 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X7A9FD25D81D88D1B">14.3 <span class="Heading">Quotients and Remainders</span></a>
49 </span>
50 <div class="ContSSBlock">
51 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X849D0F807F697D35">14.3-1 QuoInt</a></span>
52 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X795170A385AC8FEE">14.3-2 BestQuoInt</a></span>
53 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X805ADD5A826D844D">14.3-3 RemInt</a></span>
54 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7A4FEFCA8128E3C3">14.3-4 GcdInt</a></span>
55 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8775930486BD0C5B">14.3-5 Gcdex</a></span>
56 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7B33143E78A8DDE3">14.3-6 LcmInt</a></span>
57 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X79B466E984CD52D4">14.3-7 CoefficientsQadic</a></span>
58 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X83124F86839DC7E6">14.3-8 CoefficientsMultiadic</a></span>
59 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X84A1900E82902B5F">14.3-9 ChineseRem</a></span>
60 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7E404B1183DBC82A">14.3-10 PowerModInt</a></span>
61 </div></div>
62 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X82005E587F0CB02A">14.4 <span class="Heading">Prime Integers and Factorization</span></a>
63 </span>
64 <div class="ContSSBlock">
65 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X86F5E4CD82FEB9F4">14.4-1 Primes</a></span>
66 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X78FDA4437EDCA70C">14.4-2 IsPrimeInt</a></span>
67 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7CD977B17B4A7A4B">14.4-3 PrimalityProof</a></span>
68 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8443125D7FD6F2A6">14.4-4 IsPrimePowerInt</a></span>
69 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X78744C367A94C69F">14.4-5 NextPrimeInt</a></span>
70 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X819060E17E83728A">14.4-6 PrevPrimeInt</a></span>
71 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X82C989DB84744B36">14.4-7 FactorsInt</a></span>
72 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X80E7A5D381C64CC9">14.4-8 PrimeDivisors</a></span>
73 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X786FF92C7C54BF97">14.4-9 PartialFactorization</a></span>
74 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X803D431087B6FF28">14.4-10 PrintFactorsInt</a></span>
75 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X82148B347E294C87">14.4-11 PrimePowersInt</a></span>
76 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X809E0E1B83AF7695">14.4-12 DivisorsInt</a></span>
77 </div></div>
78 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X864BF040862409FC">14.5 <span class="Heading">Residue Class Rings</span></a>
79 </span>
80 <div class="ContSSBlock">
81 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X87B1210B8581D5B2"><code>14.5-1 \mod</code></a></span>
82 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X79CE76AD82B3E2B2">14.5-2 ZmodnZ</a></span>
83 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X838F36507D985EDA">14.5-3 ZmodnZObj</a></span>
84 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7D0107DD79753901">14.5-4 IsZmodnZObj</a></span>
85 </div></div>
86 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X7904B6D681EBF091">14.6 <span class="Heading">Check Digits</span></a>
87 </span>
88 <div class="ContSSBlock">
89 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X82BABA8F868BD425">14.6-1 CheckDigitISBN</a></span>
90 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X85F1A6A5870485B9">14.6-2 CheckDigitTestFunction</a></span>
91 </div></div>
92 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X85361FAE8088C006">14.7 <span class="Heading">Random Sources</span></a>
93 </span>
94 <div class="ContSSBlock">
95 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X82E31A697E389F1D">14.7-1 IsRandomSource</a></span>
96 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X821004F286282D49">14.7-2 Random</a></span>
97 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X86FFFBC9790F9742">14.7-3 <span class="Heading">State and Reset for Random Sources</span></a>
98 </span>
99 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7AC96008820FAF1F">14.7-4 <span class="Heading">Kinds of Random Sources</span></a>
100 </span>
101 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X7CB0B5BC82F8FD8F">14.7-5 RandomSource</a></span>
102 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8653AE447D94C1DC">14.7-6 <span class="Heading">Implementing new kinds of random sources</span></a>
103 </span>
104 </div></div>
105 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap14.html#X7A0311DF78DB4FD8">14.8 <span class="Heading">Bitfields</span></a>
106 </span>
107 <div class="ContSSBlock">
108 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X85C7BD9E7FCC6C10">14.8-1 MakeBitfields</a></span>
109 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap14.html#X8068CE3781F4003C">14.8-2 BuildBitfields</a></span>
110 </div></div>
111 </div>
112
113 <h3>14 <span class="Heading">Integers</span></h3>
114
115 <p>One of the most fundamental datatypes in every programming language is the integer type. <strong class="pkg">GAP</strong> is no exception.</p>
116
117 <p><strong class="pkg">GAP</strong> integers are entered as a sequence of decimal digits optionally preceded by a <q><code class="code">+</code></q> sign for positive integers or a <q><code class="code">-</code></q> sign for negative integers. The size of integers in <strong class="pkg">GAP</strong> is only limited by the amount of available memory, so you can compute with integers having thousands of digits.</p>
118
119
120 <div class="example"><pre>
121 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">-1234;</span>
122 -1234
123 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">123456789012345678901234567890123456789012345678901234567890;</span>
124 123456789012345678901234567890123456789012345678901234567890
125 </pre></div>
126
127 <p>Note that in a few places, only certain small integer values can be used. A <em>small integer</em> (also referred to as <em>immediate integer</em>) is an integer <span class="SimpleMath">n</span> satisfying <code class="code">INTOBJ_MIN</code> <span class="SimpleMath">≤ n ≤</span> <code class="code">INTOBJ_MAX</code>, where <code class="code">INTOBJ_MIN</code> and <code class="code">INTOBJ_MAX</code> equal either <span class="SimpleMath">-2^28</span> and <span class="SimpleMath">2^28-1</span> (on 32-bit systems) or <span class="SimpleMath">-2^60</span> and <span class="SimpleMath">2^60-1</span> (on 64-bit systems). For example, the elements of a range are restricted to small integers (see <a href="chap21.html#X79596BDE7CAF8491"><span class="RefLink">21.22</span></a>).</p>
128
129 <p>Many more functions that are mainly related to the prime residue group of integers modulo an integer are described in chapter <a href="chap15.html#X7FB995737B7ED8A2"><span class="RefLink">15</span></a>, and functions dealing with combinatorics can be found in chapter <a href="chap16.html#X7BDA99EE7CEADA7C"><span class="RefLink">16</span></a>.</p>
130
131 <p><a id="X838230CE810107A3" name="X838230CE810107A3"></a></p>
132
133 <h4>14.1 <span class="Heading">Integers: Global Variables</span></h4>
134
135 <p><a id="X7E20D82B79DE5129" name="X7E20D82B79DE5129"></a></p>
136
137 <h5>14.1-1 Integers</h5>
138
139 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Integers</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
140 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PositiveIntegers</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
141 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NonnegativeIntegers</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
142 <p>These global variables represent the ring of integers and the semirings of positive and nonnegative integers, respectively.</p>
143
144
145 <div class="example"><pre>
146 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Size( Integers ); 2 in Integers;</span>
147 infinity
148 true
149 </pre></div>
150
151 <p><code class="func">Integers</code> is a subset of <code class="func">Rationals</code> (<a href="chap17.html#X7B6029D18570C08A"><span class="RefLink">17.1-1</span></a>), which is a subset of <code class="func">Cyclotomics</code> (<a href="chap18.html#X863D1E017BC9EB7F"><span class="RefLink">18.1-2</span></a>). See Chapter <a href="chap18.html#X7DFC03C187DE4841"><span class="RefLink">18</span></a> for arithmetic operations and comparison of integers.</p>
152
153 <p><a id="X818683B17F8C97F3" name="X818683B17F8C97F3"></a></p>
154
155 <h5>14.1-2 IsIntegers</h5>
156
157 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsIntegers</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
158 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsPositiveIntegers</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
159 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsNonnegativeIntegers</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
160 <p>are the defining categories for the domains <code class="func">Integers</code> (<a href="chap14.html#X7E20D82B79DE5129"><span class="RefLink">14.1-1</span></a>), <code class="func">PositiveIntegers</code> (<a href="chap14.html#X7E20D82B79DE5129"><span class="RefLink">14.1-1</span></a>), and <code class="func">NonnegativeIntegers</code> (<a href="chap14.html#X7E20D82B79DE5129"><span class="RefLink">14.1-1</span></a>).</p>
161
162
163 <div class="example"><pre>
164 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsIntegers( Integers ); IsIntegers( Rationals ); IsIntegers( 7 );</span>
165 true
166 false
167 false
168 </pre></div>
169
170 <p><a id="X80CF510B8080C7CA" name="X80CF510B8080C7CA"></a></p>
171
172 <h4>14.2 <span class="Heading">Elementary Operations for Integers</span></h4>
173
174 <p><a id="X87AEADF07DC8303B" name="X87AEADF07DC8303B"></a></p>
175
176 <h5>14.2-1 IsInt</h5>
177
178 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsInt</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
179 <p>Every rational integer lies in the category <code class="func">IsInt</code>, which is a subcategory of <code class="func">IsRat</code> (<a href="chap17.html#X7ED018F5794935F7"><span class="RefLink">17.2-1</span></a>).</p>
180
181 <p><a id="X82A854757DFA9C76" name="X82A854757DFA9C76"></a></p>
182
183 <h5>14.2-2 IsPosInt</h5>
184
185 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsPosInt</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
186 <p>Every positive integer lies in the category <code class="func">IsPosInt</code>.</p>
187
188 <p><a id="X87CA734380B5F68C" name="X87CA734380B5F68C"></a></p>
189
190 <h5>14.2-3 Int</h5>
191
192 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Int</code>( <var class="Arg">elm</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
193 <p><code class="func">Int</code> returns an integer <code class="code">int</code> whose meaning depends on the type of <var class="Arg">elm</var>. For example:</p>
194
195 <p>If <var class="Arg">elm</var> is a rational number (see Chapter <a href="chap17.html#X87003045878E74DF"><span class="RefLink">17</span></a>) then <code class="code">int</code> is the integer part of the quotient of numerator and denominator of <var class="Arg">elm</var> (see <code class="func">QuoInt</code> (<a href="chap14.html#X849D0F807F697D35"><span class="RefLink">14.3-1</span></a>)).</p>
196
197 <p>If <var class="Arg">elm</var> is an element of a finite prime field (see Chapter <a href="chap59.html#X7893ABF67A028802"><span class="RefLink">59</span></a>) then <code class="code">int</code> is the smallest nonnegative integer such that <code class="code"><var class="Arg">elm</var> = int * One( <var class="Arg">elm</var> )</code>.</p>
198
199 <p>If <var class="Arg">elm</var> is a string (see Chapter <a href="chap27.html#X7D28329B7EDB8F47"><span class="RefLink">27</span></a>) consisting entirely of decimal digits <code class="code">'0'</code>, <code class="code">'1'</code>, <span class="SimpleMath">...</span>, <code class="code">'9'</code>, and optionally a sign <code class="code">'-'</code> (at the first position), then <code class="code">int</code> is the integer described by this string. For all other strings, <code class="code">fail</code> is returned. See <code class="func">Int</code> (<a href="chap27.html#X7B6D118184F692A0"><span class="RefLink">27.9-1</span></a>).</p>
200
201 <p>The operation <code class="func">String</code> (<a href="chap27.html#X81FB5BE27903EC32"><span class="RefLink">27.7-6</span></a>) can be used to compute a string for rational integers, in fact for all cyclotomics.</p>
202
203
204 <div class="example"><pre>
205 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Int( 4/3 ); Int( -2/3 );</span>
206 1
207 0
208 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">int:= Int( Z(5) ); int * One( Z(5) );</span>
209 2
210 Z(5)
211 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Int( "12345" ); Int( "-27" ); Int( "-27/3" );</span>
212 12345
213 -27
214 fail
215 </pre></div>
216
217 <p><a id="X87DD1EEE7EF18036" name="X87DD1EEE7EF18036"></a></p>
218
219 <h5>14.2-4 IsEvenInt</h5>
220
221 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsEvenInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
222 <p>tests if the integer <var class="Arg">n</var> is divisible by 2.</p>
223
224 <p><a id="X8621BA927CD12EFB" name="X8621BA927CD12EFB"></a></p>
225
226 <h5>14.2-5 IsOddInt</h5>
227
228 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsOddInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
229 <p>tests if the integer <var class="Arg">n</var> is not divisible by 2.</p>
230
231 <p><a id="X782095927FB9F1DB" name="X782095927FB9F1DB"></a></p>
232
233 <h5>14.2-6 AbsInt</h5>
234
235 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AbsInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
236 <p><code class="func">AbsInt</code> returns the absolute value of the integer <var class="Arg">n</var>, i.e., <var class="Arg">n</var> if <var class="Arg">n</var> is positive, -<var class="Arg">n</var> if <var class="Arg">n</var> is negative and 0 if <var class="Arg">n</var> is 0.</p>
237
238 <p><code class="func">AbsInt</code> is a special case of the general operation <code class="func">EuclideanDegree</code> (<a href="chap56.html#X784234088350D4E4"><span class="RefLink">56.6-2</span></a>).</p>
239
240 <p>See also <code class="func">AbsoluteValue</code> (<a href="chap18.html#X81DD58BB81FB3426"><span class="RefLink">18.1-8</span></a>).</p>
241
242
243 <div class="example"><pre>
244 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AbsInt( 33 );</span>
245 33
246 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AbsInt( -214378 );</span>
247 214378
248 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AbsInt( 0 );</span>
249 0
250 </pre></div>
251
252 <p><a id="X842614817FE48D62" name="X842614817FE48D62"></a></p>
253
254 <h5>14.2-7 SignInt</h5>
255
256 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SignInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
257 <p><code class="func">SignInt</code> returns the sign of the integer <var class="Arg">n</var>, i.e., 1 if <var class="Arg">n</var> is positive, -1 if <var class="Arg">n</var> is negative and 0 if <var class="Arg">n</var> is 0.</p>
258
259
260 <div class="example"><pre>
261 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SignInt( 33 );</span>
262 1
263 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SignInt( -214378 );</span>
264 -1
265 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SignInt( 0 );</span>
266 0
267 </pre></div>
268
269 <p><a id="X8197C4E882BAF14E" name="X8197C4E882BAF14E"></a></p>
270
271 <h5>14.2-8 LogInt</h5>
272
273 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LogInt</code>( <var class="Arg">n</var>, <var class="Arg">base</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
274 <p><code class="func">LogInt</code> returns the integer part of the logarithm of the positive integer <var class="Arg">n</var> with respect to the positive integer <var class="Arg">base</var>, i.e., the largest positive integer <span class="SimpleMath">e</span> such that <span class="SimpleMath"><var class="Arg">base</var>^e ≤ <var class="Arg">n</var></span>. The function <code class="func">LogInt</code> will signal an error if either <var class="Arg">n</var> or <var class="Arg">base</var> is not positive.</p>
275
276 <p>For <var class="Arg">base</var> <span class="SimpleMath">= 2</span> this is very efficient because the internal binary representation of the integer is used.</p>
277
278
279 <div class="example"><pre>
280 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">LogInt( 1030, 2 );</span>
281 10
282 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">2^10;</span>
283 1024
284 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">LogInt( 1, 10 );</span>
285 0
286 </pre></div>
287
288 <p><a id="X83D9B5C87EEA2A77" name="X83D9B5C87EEA2A77"></a></p>
289
290 <h5>14.2-9 RootInt</h5>
291
292 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RootInt</code>( <var class="Arg">n</var>[, <var class="Arg">k</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
293 <p><code class="func">RootInt</code> returns the integer part of the <var class="Arg">k</var>th root of the integer <var class="Arg">n</var>. If the optional integer argument <var class="Arg">k</var> is not given it defaults to 2, i.e., <code class="func">RootInt</code> returns the integer part of the square root in this case.</p>
294
295 <p>If <var class="Arg">n</var> is positive, <code class="func">RootInt</code> returns the largest positive integer <span class="SimpleMath">r</span> such that <span class="SimpleMath">r^<var class="Arg">k</var> ≤ <var class="Arg">n</var></span>. If <var class="Arg">n</var> is negative and <var class="Arg">k</var> is odd <code class="func">RootInt</code> returns <code class="code">-RootInt( -<var class="Arg">n</var>, <var class="Arg">k</var> )</code>. If <var class="Arg">n</var> is negative and <var class="Arg">k</var> is even <code class="func">RootInt</code> will cause an error. <code class="func">RootInt</code> will also cause an error if <var class="Arg">k</var> is 0 or negative.</p>
296
297
298 <div class="example"><pre>
299 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootInt( 361 );</span>
300 19
301 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootInt( 2 * 10^12 );</span>
302 1414213
303 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootInt( 17000, 5 );</span>
304 7
305 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">7^5;</span>
306 16807
307 </pre></div>
308
309 <p><a id="X7F98A0CE7B9FD366" name="X7F98A0CE7B9FD366"></a></p>
310
311 <h5>14.2-10 SmallestRootInt</h5>
312
313 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SmallestRootInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
314 <p><code class="func">SmallestRootInt</code> returns the smallest root of the integer <var class="Arg">n</var>.</p>
315
316 <p>The smallest root of an integer <var class="Arg">n</var> is the integer <span class="SimpleMath">r</span> of smallest absolute value for which a positive integer <span class="SimpleMath">k</span> exists such that <span class="SimpleMath"><var class="Arg">n</var> = r^k</span>.</p>
317
318
319 <div class="example"><pre>
320 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SmallestRootInt( 2^30 );</span>
321 2
322 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SmallestRootInt( -(2^30) );</span>
323 -4
324 </pre></div>
325
326 <p>Note that <span class="SimpleMath">(-2)^30 = +(2^30)</span>.</p>
327
328
329 <div class="example"><pre>
330 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SmallestRootInt( 279936 );</span>
331 6
332 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">LogInt( 279936, 6 );</span>
333 7
334 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SmallestRootInt( 1001 );</span>
335 1001
336 </pre></div>
337
338 <p><a id="X83B998E486893FED" name="X83B998E486893FED"></a></p>
339
340 <h5>14.2-11 IsSquareInt</h5>
341
342 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsSquareInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
343 <p><code class="func">IsSquareInt</code> tests whether the integer <var class="Arg">n</var> is the square of an integer or not. This test is much faster than the simpler <code class="code">RootInt</code><span class="SimpleMath">(n)^2=n</span> because it first tests whether <var class="Arg">n</var> is a square residue modulo some small integers.</p>
344
345 <p><a id="X862D1BD786EFFDA9" name="X862D1BD786EFFDA9"></a></p>
346
347 <h5>14.2-12 ListOfDigits</h5>
348
349 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ListOfDigits</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
350 <p>For a positive integer <var class="Arg">n</var> this function returns a list <var class="Arg">l</var>, consisting of the digits of <var class="Arg">n</var> in decimal representation.</p>
351
352
353 <div class="example"><pre>
354 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ListOfDigits(3142);</span>
355 [ 3, 1, 4, 2 ]
356 </pre></div>
357
358 <p><a id="X8185784B7E228DEA" name="X8185784B7E228DEA"></a></p>
359
360 <h5>14.2-13 Random</h5>
361
362 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Random</code>( <var class="Arg">Integers</var> )</td><td class="tdright">(&nbsp;method&nbsp;)</td></tr></table></div>
363 <p><code class="func">Random</code> for integers returns pseudo random integers between <span class="SimpleMath">-10</span> and <span class="SimpleMath">10</span> distributed according to a binomial distribution. To generate uniformly distributed integers from a range, use the construction <code class="code">Random( [ <var class="Arg">low</var> .. <var class="Arg">high</var> ] )</code>  (see <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>)).</p>
364
365 <p><a id="X7A9FD25D81D88D1B" name="X7A9FD25D81D88D1B"></a></p>
366
367 <h4>14.3 <span class="Heading">Quotients and Remainders</span></h4>
368
369 <p><a id="X849D0F807F697D35" name="X849D0F807F697D35"></a></p>
370
371 <h5>14.3-1 QuoInt</h5>
372
373 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; QuoInt</code>( <var class="Arg">n</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
374 <p><code class="func">QuoInt</code> returns the integer part of the quotient of its integer operands.</p>
375
376 <p>If <var class="Arg">n</var> and <var class="Arg">m</var> are positive, <code class="func">QuoInt</code> returns the largest positive integer <span class="SimpleMath">q</span> such that <span class="SimpleMath">q * <var class="Arg">m</var> ≤ <var class="Arg">n</var></span>. If <var class="Arg">n</var> or <var class="Arg">m</var> or both are negative the absolute value of the integer part of the quotient is the quotient of the absolute values of <var class="Arg">n</var> and <var class="Arg">m</var>, and the sign of it is the product of the signs of <var class="Arg">n</var> and <var class="Arg">m</var>.</p>
377
378 <p><code class="func">QuoInt</code> is used in a method for the general operation <code class="func">EuclideanQuotient</code> (<a href="chap56.html#X7A93FA788318B147"><span class="RefLink">56.6-3</span></a>).</p>
379
380
381 <div class="example"><pre>
382 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">QuoInt(5,3); QuoInt(-5,3); QuoInt(5,-3); QuoInt(-5,-3);</span>
383 1
384 -1
385 -1
386 1
387 </pre></div>
388
389 <p><a id="X795170A385AC8FEE" name="X795170A385AC8FEE"></a></p>
390
391 <h5>14.3-2 BestQuoInt</h5>
392
393 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; BestQuoInt</code>( <var class="Arg">n</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
394 <p><code class="func">BestQuoInt</code> returns the best quotient <span class="SimpleMath">q</span> of the integers <var class="Arg">n</var> and <var class="Arg">m</var>. This is the quotient such that <span class="SimpleMath"><var class="Arg">n</var>-q*<var class="Arg">m</var></span> has minimal absolute value. If there are two quotients whose remainders have the same absolute value, then the quotient with the smaller absolute value is chosen.</p>
395
396
397 <div class="example"><pre>
398 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">BestQuoInt( 5, 3 ); BestQuoInt( -5, 3 );</span>
399 2
400 -2
401 </pre></div>
402
403 <p><a id="X805ADD5A826D844D" name="X805ADD5A826D844D"></a></p>
404
405 <h5>14.3-3 RemInt</h5>
406
407 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RemInt</code>( <var class="Arg">n</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
408 <p><code class="func">RemInt</code> returns the remainder of its two integer operands.</p>
409
410 <p>If <var class="Arg">m</var> is not equal to zero, <code class="func">RemInt</code> returns <code class="code"><var class="Arg">n</var> - <var class="Arg">m</var> * QuoInt( <var class="Arg">n</var>, <var class="Arg">m</var> )</code>. Note that the rules given for <code class="func">QuoInt</code> (<a href="chap14.html#X849D0F807F697D35"><span class="RefLink">14.3-1</span></a>) imply that the return value of <code class="func">RemInt</code> has the same sign as <var class="Arg">n</var> and its absolute value is strictly less than the absolute value of <var class="Arg">m</var>. Note also that the return value equals <code class="code"><var class="Arg">n</var> mod <var class="Arg">m</var></code> when both <var class="Arg">n</var> and <var class="Arg">m</var> are nonnegative. Dividing by <code class="code">0</code> signals an error.</p>
411
412 <p><code class="func">RemInt</code> is used in a method for the general operation <code class="func">EuclideanRemainder</code> (<a href="chap56.html#X7B5E9639865E91BA"><span class="RefLink">56.6-4</span></a>).</p>
413
414
415 <div class="example"><pre>
416 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RemInt(5,3); RemInt(-5,3); RemInt(5,-3); RemInt(-5,-3);</span>
417 2
418 -2
419 2
420 -2
421 </pre></div>
422
423 <p><a id="X7A4FEFCA8128E3C3" name="X7A4FEFCA8128E3C3"></a></p>
424
425 <h5>14.3-4 GcdInt</h5>
426
427 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; GcdInt</code>( <var class="Arg">m</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
428 <p><code class="func">GcdInt</code> returns the greatest common divisor of its two integer operands <var class="Arg">m</var> and <var class="Arg">n</var>, i.e., the greatest integer that divides both <var class="Arg">m</var> and <var class="Arg">n</var>. The greatest common divisor is never negative, even if the arguments are. We define <code class="code">GcdInt( <var class="Arg">m</var>, 0 ) = GcdInt( 0, <var class="Arg">m</var> ) = AbsInt( <var class="Arg">m</var> )</code> and <code class="code">GcdInt( 0, 0 ) = 0</code>.</p>
429
430 <p><code class="func">GcdInt</code> is a method used by the general function <code class="func">Gcd</code> (<a href="chap56.html#X7DE207718456F98F"><span class="RefLink">56.7-1</span></a>).</p>
431
432
433 <div class="example"><pre>
434 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GcdInt( 123, 66 );</span>
435 3
436 </pre></div>
437
438 <p><a id="X8775930486BD0C5B" name="X8775930486BD0C5B"></a></p>
439
440 <h5>14.3-5 Gcdex</h5>
441
442 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Gcdex</code>( <var class="Arg">m</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
443 <p>returns a record <code class="code">g</code> describing the extended greatest common divisor of <var class="Arg">m</var> and <var class="Arg">n</var>. The component <code class="code">gcd</code> is this gcd, the components <code class="code">coeff1</code> and <code class="code">coeff2</code> are integer cofactors such that <code class="code">g.gcd = g.coeff1 * <var class="Arg">m</var> + g.coeff2 * <var class="Arg">n</var></code>, and the components <code class="code">coeff3</code> and <code class="code">coeff4</code> are integer cofactors such that <code class="code">0 = g.coeff3 * <var class="Arg">m</var> + g.coeff4 * <var class="Arg">n</var></code>.</p>
444
445 <p>If <var class="Arg">m</var> and <var class="Arg">n</var> both are nonzero, <code class="code">AbsInt( g.coeff1 )</code> is less than or equal to <code class="code">AbsInt(<var class="Arg">n</var>) / (2 * g.gcd)</code>, and <code class="code">AbsInt( g.coeff2 )</code> is less than or equal to <code class="code">AbsInt(<var class="Arg">m</var>) / (2 * g.gcd)</code>.</p>
446
447 <p>If <var class="Arg">m</var> or <var class="Arg">n</var> or both are zero <code class="code">coeff3</code> is <code class="code">-<var class="Arg">n</var> / g.gcd</code> and <code class="code">coeff4</code> is <code class="code"><var class="Arg">m</var> / g.gcd</code>.</p>
448
449 <p>The coefficients always form a unimodular matrix, i.e., the determinant <code class="code">g.coeff1 * g.coeff4 - g.coeff3 * g.coeff2</code> is <span class="SimpleMath">1</span> or <span class="SimpleMath">-1</span>.</p>
450
451
452 <div class="example"><pre>
453 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Gcdex( 123, 66 );</span>
454 rec( coeff1 := 7, coeff2 := -13, coeff3 := -22, coeff4 := 41,
455 gcd := 3 )
456 </pre></div>
457
458 <p>This means <span class="SimpleMath">3 = 7 * 123 - 13 * 66</span>, <span class="SimpleMath">0 = -22 * 123 + 41 * 66</span>.</p>
459
460
461 <div class="example"><pre>
462 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Gcdex( 0, -3 );</span>
463 rec( coeff1 := 0, coeff2 := -1, coeff3 := 1, coeff4 := 0, gcd := 3 )
464 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Gcdex( 0, 0 );</span>
465 rec( coeff1 := 1, coeff2 := 0, coeff3 := 0, coeff4 := 1, gcd := 0 )
466 </pre></div>
467
468 <p><code class="func">GcdRepresentation</code> (<a href="chap56.html#X7ABB91EF838075EF"><span class="RefLink">56.7-3</span></a>) provides similar functionality over arbitrary Euclidean rings.</p>
469
470 <p><a id="X7B33143E78A8DDE3" name="X7B33143E78A8DDE3"></a></p>
471
472 <h5>14.3-6 LcmInt</h5>
473
474 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LcmInt</code>( <var class="Arg">m</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
475 <p>returns the least common multiple of the integers <var class="Arg">m</var> and <var class="Arg">n</var>.</p>
476
477 <p><code class="func">LcmInt</code> is a method used by the general operation <code class="func">Lcm</code> (<a href="chap56.html#X7ABA92057DD6C7AF"><span class="RefLink">56.7-6</span></a>).</p>
478
479
480 <div class="example"><pre>
481 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">LcmInt( 123, 66 );</span>
482 2706
483 </pre></div>
484
485 <p><a id="X79B466E984CD52D4" name="X79B466E984CD52D4"></a></p>
486
487 <h5>14.3-7 CoefficientsQadic</h5>
488
489 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CoefficientsQadic</code>( <var class="Arg">i</var>, <var class="Arg">q</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
490 <p>returns the <var class="Arg">q</var>-adic representation of the integer <var class="Arg">i</var> as a list <span class="SimpleMath">l</span> of coefficients satisfying the equality <span class="SimpleMath"><var class="Arg">i</var> = ∑_{j = 0} <var class="Arg">q</var>^j ⋅ l[j+1]</span> for an integer <span class="SimpleMath"><var class="Arg">q</var> &gt; 1</span>.</p>
491
492
493 <div class="example"><pre>
494 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=CoefficientsQadic(462,3);</span>
495 [ 0, 1, 0, 2, 2, 1 ]
496 </pre></div>
497
498 <p><a id="X83124F86839DC7E6" name="X83124F86839DC7E6"></a></p>
499
500 <h5>14.3-8 CoefficientsMultiadic</h5>
501
502 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CoefficientsMultiadic</code>( <var class="Arg">ints</var>, <var class="Arg">int</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
503 <p>returns the multiadic expansion of the integer <var class="Arg">int</var> modulo the integers given in <var class="Arg">ints</var> (in ascending order). It returns a list of coefficients in the <em>reverse</em> order to that in <var class="Arg">ints</var>.</p>
504
505 <p><a id="X84A1900E82902B5F" name="X84A1900E82902B5F"></a></p>
506
507 <h5>14.3-9 ChineseRem</h5>
508
509 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ChineseRem</code>( <var class="Arg">moduli</var>, <var class="Arg">residues</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
510 <p><code class="func">ChineseRem</code> returns the combination of the <var class="Arg">residues</var> modulo the <var class="Arg">moduli</var>, i.e., the unique integer <code class="code">c</code> from <code class="code">[0..Lcm(<var class="Arg">moduli</var>)-1]</code> such that <code class="code">c = <var class="Arg">residues</var>[i]</code> modulo <code class="code"><var class="Arg">moduli</var>[i]</code> for all <code class="code">i</code>, if it exists. If no such combination exists <code class="func">ChineseRem</code> signals an error.</p>
511
512 <p>Such a combination does exist if and only if <code class="code"><var class="Arg">residues</var>[i] = <var class="Arg">residues</var>[k] mod Gcd( <var class="Arg">moduli</var>[i], <var class="Arg">moduli</var>[k] )</code> for every pair <code class="code">i</code>, <code class="code">k</code>. Note that this implies that such a combination exists if the moduli are pairwise relatively prime. This is called the Chinese remainder theorem.</p>
513
514
515 <div class="example"><pre>
516 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ChineseRem( [ 2, 3, 5, 7 ], [ 1, 2, 3, 4 ] );</span>
517 53
518 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ChineseRem( [ 6, 10, 14 ], [ 1, 3, 5 ] );</span>
519 103
520 </pre></div>
521
522
523 <div class="example"><pre>
524 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ChineseRem( [ 6, 10, 14 ], [ 1, 2, 3 ] );</span>
525 Error, the residues must be equal modulo 2
526 *[1] Error( "the residues must be equal modulo ", g.gcd );
527 @ GAPROOT/lib/integer.gi:391
528 &lt;function "ChineseRem"&gt;( &lt;arguments&gt; )
529 called from read-eval loop at *stdin*:2
530 you can 'quit;' to quit to outer loop, or
531 you can 'return;' to continue
532 <span class="GAPbrkprompt">brk&gt;</span>
533 </pre></div>
534
535 <p><a id="X7E404B1183DBC82A" name="X7E404B1183DBC82A"></a></p>
536
537 <h5>14.3-10 PowerModInt</h5>
538
539 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PowerModInt</code>( <var class="Arg">r</var>, <var class="Arg">e</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
540 <p>returns <span class="SimpleMath"><var class="Arg">r</var>^<var class="Arg">e</var> mod <var class="Arg">m</var></span> for integers <var class="Arg">r</var>, <var class="Arg">e</var> and <var class="Arg">m</var>.</p>
541
542 <p>Note that <code class="func">PowerModInt</code> can reduce intermediate results and thus will generally be faster than using <var class="Arg">r</var><code class="code">^</code><var class="Arg">e</var><code class="code"> mod </code><var class="Arg">m</var>, which would compute <span class="SimpleMath"><var class="Arg">r</var>^<var class="Arg">e</var></span> first and reduces the result afterwards.</p>
543
544 <p><code class="func">PowerModInt</code> is a method for the general operation <code class="func">PowerMod</code> (<a href="chap56.html#X805A35D684B7A952"><span class="RefLink">56.7-9</span></a>).</p>
545
546 <p><a id="X82005E587F0CB02A" name="X82005E587F0CB02A"></a></p>
547
548 <h4>14.4 <span class="Heading">Prime Integers and Factorization</span></h4>
549
550 <p><a id="X86F5E4CD82FEB9F4" name="X86F5E4CD82FEB9F4"></a></p>
551
552 <h5>14.4-1 Primes</h5>
553
554 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Primes</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
555 <p><code class="func">Primes</code> is a strictly sorted list of the 168 primes less than 1000.</p>
556
557 <p>This is used in <code class="func">IsPrimeInt</code> (<a href="chap14.html#X78FDA4437EDCA70C"><span class="RefLink">14.4-2</span></a>) and <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>) to cast out small primes quickly.</p>
558
559
560 <div class="example"><pre>
561 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Primes[1];</span>
562 2
563 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Primes[100];</span>
564 541
565 </pre></div>
566
567 <p><a id="X78FDA4437EDCA70C" name="X78FDA4437EDCA70C"></a></p>
568
569 <h5>14.4-2 IsPrimeInt</h5>
570
571 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsPrimeInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
572 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsProbablyPrimeInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
573 <p><code class="func">IsPrimeInt</code> returns <code class="keyw">false</code> if it can prove that the integer <var class="Arg">n</var> is composite and <code class="keyw">true</code> otherwise. By convention <code class="code">IsPrimeInt(0) = IsPrimeInt(1) = false</code> and we define <code class="code">IsPrimeInt(-</code><var class="Arg">n</var><code class="code">) = IsPrimeInt(</code><var class="Arg">n</var><code class="code">)</code>.</p>
574
575 <p><code class="func">IsPrimeInt</code> will return <code class="keyw">true</code> for every prime <var class="Arg">n</var>. <code class="func">IsPrimeInt</code> will return <code class="keyw">false</code> for all composite <var class="Arg">n</var> <span class="SimpleMath">&lt; 10^18</span> and for all composite <var class="Arg">n</var> that have a factor <span class="SimpleMath">p &lt; 1000</span>. So for integers <var class="Arg">n</var> <span class="SimpleMath">&lt; 10^18</span>, <code class="func">IsPrimeInt</code> is a proper primality test. It is conceivable that <code class="func">IsPrimeInt</code> may return <code class="keyw">true</code> for some composite <var class="Arg">n</var> <span class="SimpleMath">&gt; 10^18</span>, but no such <var class="Arg">n</var> is currently known. So for integers <var class="Arg">n</var> <span class="SimpleMath">&gt; 10^18</span>, <code class="func">IsPrimeInt</code> is a probable-primality test. <code class="func">IsPrimeInt</code> will issue a warning when its argument is probably prime but not a proven prime. (The function <code class="func">IsProbablyPrimeInt</code> will do a similar calculation but not issue a warning.) The warning can be switched off by <code class="code">SetInfoLevel( InfoPrimeInt, 0 );</code>, the default level is <span class="SimpleMath">1</span> (also see <code class="func">SetInfoLevel</code> (<a href="chap7.html#X7A43B9E68765EE9E"><span class="RefLink">7.4-3</span></a>) ).</p>
576
577 <p>If composites that fool <code class="func">IsPrimeInt</code> do exist, they would be extremely rare, and finding one by pure chance might be less likely than finding a bug in <strong class="pkg">GAP</strong>. We would appreciate being informed about any example of a composite number <var class="Arg">n</var> for which <code class="func">IsPrimeInt</code> returns <code class="keyw">true</code>.</p>
578
579 <p><code class="func">IsPrimeInt</code> is a deterministic algorithm, i.e., the computations involve no random numbers, and repeated calls will always return the same result. <code class="func">IsPrimeInt</code> first does trial divisions by the primes less than 1000. Then it tests that <var class="Arg">n</var> is a strong pseudoprime w.r.t. the base 2. Finally it tests whether <var class="Arg">n</var> is a Lucas pseudoprime w.r.t. the smallest quadratic nonresidue of <var class="Arg">n</var>. A better description can be found in the comment in the library file <code class="file">primality.gi</code>.</p>
580
581 <p>The time taken by <code class="func">IsPrimeInt</code> is approximately proportional to the third power of the number of digits of <var class="Arg">n</var>. Testing numbers with several hundreds digits is quite feasible.</p>
582
583 <p><code class="func">IsPrimeInt</code> is a method for the general operation <code class="func">IsPrime</code> (<a href="chap56.html#X7AA107AE7F79C6D8"><span class="RefLink">56.5-8</span></a>).</p>
584
585 <p>Remark: In future versions of <strong class="pkg">GAP</strong> we hope to change the definition of <code class="func">IsPrimeInt</code> to return <code class="keyw">true</code> only for proven primes (currently, we lack a sufficiently good primality proving function). In applications, use explicitly <code class="func">IsPrimeInt</code> or <code class="func">IsProbablyPrimeInt</code> with this change in mind.</p>
586
587
588 <div class="example"><pre>
589 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimeInt( 2^31 - 1 );</span>
590 true
591 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimeInt( 10^42 + 1 );</span>
592 false
593 </pre></div>
594
595 <p><a id="X7CD977B17B4A7A4B" name="X7CD977B17B4A7A4B"></a></p>
596
597 <h5>14.4-3 PrimalityProof</h5>
598
599 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrimalityProof</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
600 <p>Construct a machine verifiable proof of the primality of (the probable prime) <var class="Arg">n</var>, following the ideas of <a href="chapBib.html#biBBLS1975">[BLS75]</a>. The proof consists of various Fermat and Lucas pseudoprimality tests, which taken as a whole prove the primality. The proof is represented as a list of witnesses of two kinds. The first kind, <code class="code">[ "F", divisor, base ]</code>, indicates a successful Fermat pseudoprimality test, where <var class="Arg">n</var> is a strong pseudoprime at <code class="keyw">base</code> with order not divisible by <span class="SimpleMath">(<var class="Arg">n</var>-1)/divisor</span>. The second kind, <code class="code">[ "L", divisor, discriminant, P ]</code> indicates a successful Lucas pseudoprimality test, for a quadratic form of given <code class="keyw">discriminant</code> and middle term <code class="keyw">P</code> with an extra check at <span class="SimpleMath">(<var class="Arg">n</var>+1)/divisor</span>.</p>
601
602 <p><a id="X8443125D7FD6F2A6" name="X8443125D7FD6F2A6"></a></p>
603
604 <h5>14.4-4 IsPrimePowerInt</h5>
605
606 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsPrimePowerInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
607 <p><code class="func">IsPrimePowerInt</code> returns <code class="keyw">true</code> if the integer <var class="Arg">n</var> is a prime power and <code class="keyw">false</code> otherwise.</p>
608
609 <p>An integer <span class="SimpleMath">n</span> is a <em>prime power</em> if there exists a prime <span class="SimpleMath">p</span> and a positive integer <span class="SimpleMath">i</span> such that <span class="SimpleMath">p^i = n</span>. If <span class="SimpleMath">n</span> is negative the condition is that there must exist a negative prime <span class="SimpleMath">p</span> and an odd positive integer <span class="SimpleMath">i</span> such that <span class="SimpleMath">p^i = n</span>. The integers 1 and -1 are not prime powers.</p>
610
611 <p>Note that <code class="func">IsPrimePowerInt</code> uses <code class="func">SmallestRootInt</code> (<a href="chap14.html#X7F98A0CE7B9FD366"><span class="RefLink">14.2-10</span></a>) and a probable-primality test (see <code class="func">IsPrimeInt</code> (<a href="chap14.html#X78FDA4437EDCA70C"><span class="RefLink">14.4-2</span></a>)).</p>
612
613
614 <div class="example"><pre>
615 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimePowerInt( 31^5 );</span>
616 true
617 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimePowerInt( 2^31-1 ); # 2^31-1 is actually a prime</span>
618 true
619 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimePowerInt( 2^63-1 );</span>
620 false
621 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Filtered( [-10..10], IsPrimePowerInt );</span>
622 [ -8, -7, -5, -3, -2, 2, 3, 4, 5, 7, 8, 9 ]
623 </pre></div>
624
625 <p><a id="X78744C367A94C69F" name="X78744C367A94C69F"></a></p>
626
627 <h5>14.4-5 NextPrimeInt</h5>
628
629 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NextPrimeInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
630 <p><code class="func">NextPrimeInt</code> returns the smallest prime which is strictly larger than the integer <var class="Arg">n</var>.</p>
631
632 <p>Note that <code class="func">NextPrimeInt</code> uses a probable-primality test (see <code class="func">IsPrimeInt</code> (<a href="chap14.html#X78FDA4437EDCA70C"><span class="RefLink">14.4-2</span></a>)).</p>
633
634
635 <div class="example"><pre>
636 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">NextPrimeInt( 541 ); NextPrimeInt( -1 );</span>
637 547
638 2
639 </pre></div>
640
641 <p><a id="X819060E17E83728A" name="X819060E17E83728A"></a></p>
642
643 <h5>14.4-6 PrevPrimeInt</h5>
644
645 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrevPrimeInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
646 <p><code class="func">PrevPrimeInt</code> returns the largest prime which is strictly smaller than the integer <var class="Arg">n</var>.</p>
647
648 <p>Note that <code class="func">PrevPrimeInt</code> uses a probable-primality test (see <code class="func">IsPrimeInt</code> (<a href="chap14.html#X78FDA4437EDCA70C"><span class="RefLink">14.4-2</span></a>)).</p>
649
650
651 <div class="example"><pre>
652 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrevPrimeInt( 541 ); PrevPrimeInt( 1 );</span>
653 523
654 -2
655 </pre></div>
656
657 <p><a id="X82C989DB84744B36" name="X82C989DB84744B36"></a></p>
658
659 <h5>14.4-7 FactorsInt</h5>
660
661 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FactorsInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
662 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; FactorsInt</code>( <var class="Arg">n:</var> <var class="Arg">RhoTrials</var> <var class="Arg">:=</var> <var class="Arg">trials</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
663 <p><code class="func">FactorsInt</code> returns a list of factors of a given integer <var class="Arg">n</var> such that <code class="code">Product( FactorsInt( <var class="Arg">n</var> ) ) = <var class="Arg">n</var></code>. If <span class="SimpleMath">|n| ≤ 1</span> the list <code class="code">[<var class="Arg">n</var>]</code> is returned. Otherwise the result contains probable primes, sorted by absolute value. The entries will all be positive except for the first one in case of a negative <var class="Arg">n</var>.</p>
664
665 <p>See <code class="func">PrimeDivisors</code> (<a href="chap14.html#X80E7A5D381C64CC9"><span class="RefLink">14.4-8</span></a>) for a function that returns a set of (probable) primes dividing <var class="Arg">n</var>.</p>
666
667 <p>Note that <code class="func">FactorsInt</code> uses a probable-primality test (see <code class="func">IsPrimeInt</code> (<a href="chap14.html#X78FDA4437EDCA70C"><span class="RefLink">14.4-2</span></a>)). Thus <code class="func">FactorsInt</code> might return a list which contains composite integers. In such a case you will get a warning about the use of a probable prime. You can switch off these warnings by <code class="code">SetInfoLevel( InfoPrimeInt, 0 );</code> (also see <code class="func">SetInfoLevel</code> (<a href="chap7.html#X7A43B9E68765EE9E"><span class="RefLink">7.4-3</span></a>)).</p>
668
669 <p>The time taken by <code class="func">FactorsInt</code> is approximately proportional to the square root of the second largest prime factor of <var class="Arg">n</var>, which is the last one that <code class="func">FactorsInt</code> has to find, since the largest factor is simply what remains when all others have been removed. Thus the time is roughly bounded by the fourth root of <var class="Arg">n</var>. <code class="func">FactorsInt</code> is guaranteed to find all factors less than <span class="SimpleMath">10^6</span> and will find most factors less than <span class="SimpleMath">10^10</span>. If <var class="Arg">n</var> contains multiple factors larger than that <code class="func">FactorsInt</code> may not be able to factor <var class="Arg">n</var> and will then signal an error.</p>
670
671 <p><code class="func">FactorsInt</code> is used in a method for the general operation <code class="func">Factors</code> (<a href="chap56.html#X82D6EDC685D12AE2"><span class="RefLink">56.5-9</span></a>).</p>
672
673 <p>In the second form, <code class="func">FactorsInt</code> calls <code class="code">FactorsRho</code> with a limit of <var class="Arg">trials</var> on the number of trials it performs. The default is 8192. Note that Pollard's Rho is the fastest method for finding prime factors with roughly 5-10 decimal digits, but becomes more and more inferior to other factorization techniques like e.g. the Elliptic Curves Method (ECM) the bigger the prime factors are. Therefore instead of performing a huge number of Rho <var class="Arg">trials</var>, it is usually more advisable to install the <strong class="pkg">FactInt</strong> package and then simply to use the operation <code class="func">Factors</code> (<a href="chap56.html#X82D6EDC685D12AE2"><span class="RefLink">56.5-9</span></a>). The factorization of the 8-th Fermat number by Pollard's Rho below takes already a while.</p>
674
675
676 <div class="example"><pre>
677 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FactorsInt( -Factorial(6) );</span>
678 [ -2, 2, 2, 2, 3, 3, 5 ]
679 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Set( FactorsInt( Factorial(13)/11 ) );</span>
680 [ 2, 3, 5, 7, 13 ]
681 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FactorsInt( 2^63 - 1 );</span>
682 [ 7, 7, 73, 127, 337, 92737, 649657 ]
683 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FactorsInt( 10^42 + 1 );</span>
684 [ 29, 101, 281, 9901, 226549, 121499449, 4458192223320340849 ]
685 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">FactorsInt(2^256+1:RhoTrials:=100000000);</span>
686 [ 1238926361552897,
687 93461639715357977769163558199606896584051237541638188580280321 ]
688 </pre></div>
689
690 <p><a id="X80E7A5D381C64CC9" name="X80E7A5D381C64CC9"></a></p>
691
692 <h5>14.4-8 PrimeDivisors</h5>
693
694 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrimeDivisors</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
695 <p><code class="func">PrimeDivisors</code> returns for a non-zero integer <var class="Arg">n</var> a set of its positive (probable) primes divisors. In rare cases the result could contain a composite number which passed certain primality tests, see <code class="func">IsProbablyPrimeInt</code> (<a href="chap14.html#X78FDA4437EDCA70C"><span class="RefLink">14.4-2</span></a>) and <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>) for more details.</p>
696
697
698 <div class="example"><pre>
699 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimeDivisors(-12);</span>
700 [ 2, 3 ]
701 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimeDivisors(1);</span>
702 [ ]
703 </pre></div>
704
705 <p><a id="X786FF92C7C54BF97" name="X786FF92C7C54BF97"></a></p>
706
707 <h5>14.4-9 PartialFactorization</h5>
708
709 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PartialFactorization</code>( <var class="Arg">n</var>[, <var class="Arg">effort</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
710 <p><code class="func">PartialFactorization</code> returns a partial factorization of the integer <var class="Arg">n</var>. No assertions are made about the primality of the factors, except of those mentioned below.</p>
711
712 <p>The argument <var class="Arg">effort</var>, if given, specifies how intensively the function should try to determine factors of <var class="Arg">n</var>. The default is <var class="Arg">effort</var> = 5.</p>
713
714
715 <ul>
716 <li><p>If <var class="Arg">effort</var> = 0, trial division by the primes below 100 is done. Returned factors below <span class="SimpleMath">10^4</span> are guaranteed to be prime.</p>
717
718 </li>
719 <li><p>If <var class="Arg">effort</var> = 1, trial division by the primes below 1000 is done. Returned factors below <span class="SimpleMath">10^6</span> are guaranteed to be prime.</p>
720
721 </li>
722 <li><p>If <var class="Arg">effort</var> = 2, additionally trial division by the numbers in the lists <code class="code">Primes2</code> and <code class="code">ProbablePrimes2</code> is done, and perfect powers are detected. Returned factors below <span class="SimpleMath">10^6</span> are guaranteed to be prime.</p>
723
724 </li>
725 <li><p>If <var class="Arg">effort</var> = 3, additionally <code class="code">FactorsRho</code> (Pollard's Rho) with <code class="code">RhoTrials</code> = 256 is used.</p>
726
727 </li>
728 <li><p>If <var class="Arg">effort</var> = 4, as above, but <code class="code">RhoTrials</code> = 2048.</p>
729
730 </li>
731 <li><p>If <var class="Arg">effort</var> = 5, as above, but <code class="code">RhoTrials</code> = 8192. Returned factors below <span class="SimpleMath">10^12</span> are guaranteed to be prime, and all prime factors below <span class="SimpleMath">10^6</span> are guaranteed to be found.</p>
732
733 </li>
734 <li><p>If <var class="Arg">effort</var> = 6 and the package <strong class="pkg">FactInt</strong> is loaded, in addition to the above quite a number of special cases are handled.</p>
735
736 </li>
737 <li><p>If <var class="Arg">effort</var> = 7 and the package <strong class="pkg">FactInt</strong> is loaded, the only thing which is not attempted to obtain a full factorization into Baillie-Pomerance-Selfridge-Wagstaff pseudoprimes is the application of the MPQS to a remaining composite with more than 50 decimal digits.</p>
738
739 </li>
740 </ul>
741 <p>Increasing the value of the argument <var class="Arg">effort</var> by one usually results in an increase of the runtime requirements by a factor of (very roughly!) 3 to 10. (Also see <code class="func">CheapFactorsInt</code> (<a href="../../pkg/edim/doc/chap1.html#X7BAB977C7EB05067"><span class="RefLink">EDIM: CheapFactorsInt</span></a>)).</p>
742
743
744 <div class="example"><pre>
745 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([0..5],i-&gt;PartialFactorization(97^35-1,i));</span>
746 [ [ 2, 2, 2, 2, 2, 3, 11, 31, 43,
747 2446338959059521520901826365168917110105972824229555319002965029 ],
748 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967,
749 2529823122088440042297648774735177983563570655873376751812787 ],
750 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967,
751 2529823122088440042297648774735177983563570655873376751812787 ],
752 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321,
753 242549173950325921859769421435653153445616962914227 ],
754 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 687121,
755 352993394104278463123335513593170858474150787 ],
756 [ 2, 2, 2, 2, 2, 3, 11, 31, 43, 967, 39761, 262321, 687121,
757 20241187, 504769301, 34549173843451574629911361501 ] ]
758 </pre></div>
759
760 <p><a id="X803D431087B6FF28" name="X803D431087B6FF28"></a></p>
761
762 <h5>14.4-10 PrintFactorsInt</h5>
763
764 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrintFactorsInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
765 <p>prints the prime factorization of the integer <var class="Arg">n</var> in human-readable form. See also <code class="func">StringPP</code> (<a href="chap27.html#X7BB1059185AB4F84"><span class="RefLink">27.7-9</span></a>).</p>
766
767
768 <div class="example"><pre>
769 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrintFactorsInt( Factorial( 7 ) ); Print( "\n" );</span>
770 2^4*3^2*5*7
771 </pre></div>
772
773 <p><a id="X82148B347E294C87" name="X82148B347E294C87"></a></p>
774
775 <h5>14.4-11 PrimePowersInt</h5>
776
777 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrimePowersInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
778 <p>returns the prime factorization of the integer <var class="Arg">n</var> as a list <span class="SimpleMath">[ p_1, e_1, ..., p_k, e_k ]</span> with <var class="Arg">n</var> = <span class="SimpleMath">p_1^{e_1} â‹… p_2^{e_2} â‹… ... â‹… p_k^{e_k}</span>.</p>
779
780 <p>For negative integers, the absolute value is taken. Zero is not allowed as input.</p>
781
782
783 <div class="example"><pre>
784 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimePowersInt( Factorial( 7 ) );</span>
785 [ 2, 4, 3, 2, 5, 1, 7, 1 ]
786 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimePowersInt( 1 );</span>
787 [ ]
788 </pre></div>
789
790 <p><a id="X809E0E1B83AF7695" name="X809E0E1B83AF7695"></a></p>
791
792 <h5>14.4-12 DivisorsInt</h5>
793
794 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DivisorsInt</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
795 <p><code class="func">DivisorsInt</code> returns a list of all divisors of the integer <var class="Arg">n</var>. The list is sorted, so that it starts with 1 and ends with <var class="Arg">n</var>. We define that <code class="code">DivisorsInt( -<var class="Arg">n</var> ) = DivisorsInt( <var class="Arg">n</var> )</code>.</p>
796
797 <p>Since the set of divisors of 0 is infinite calling <code class="code">DivisorsInt( 0 )</code> causes an error.</p>
798
799 <p><code class="func">DivisorsInt</code> may call <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>) to obtain the prime factors. <code class="func">Sigma</code> (<a href="chap15.html#X823707DF821E79A0"><span class="RefLink">15.5-1</span></a>) and <code class="func">Tau</code> (<a href="chap15.html#X798C62847EE0372E"><span class="RefLink">15.5-2</span></a>) compute the sum and the number of positive divisors, respectively.</p>
800
801
802 <div class="example"><pre>
803 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">DivisorsInt( 1 ); DivisorsInt( 20 ); DivisorsInt( 541 );</span>
804 [ 1 ]
805 [ 1, 2, 4, 5, 10, 20 ]
806 [ 1, 541 ]
807 </pre></div>
808
809 <p><a id="X864BF040862409FC" name="X864BF040862409FC"></a></p>
810
811 <h4>14.5 <span class="Heading">Residue Class Rings</span></h4>
812
813 <p><code class="func">ZmodnZ</code> (<a href="chap14.html#X79CE76AD82B3E2B2"><span class="RefLink">14.5-2</span></a>) returns a residue class ring of <code class="func">Integers</code> (<a href="chap14.html#X853DF11B80068ED5"><span class="RefLink">14</span></a>) modulo an ideal. These residue class rings are rings, thus all operations for rings (see Chapter <a href="chap56.html#X81897F6082CACB59"><span class="RefLink">56</span></a>) apply. See also Chapters <a href="chap59.html#X7893ABF67A028802"><span class="RefLink">59</span></a> and <a href="chap15.html#X7FB995737B7ED8A2"><span class="RefLink">15</span></a>.</p>
814
815 <p><a id="X87B1210B8581D5B2" name="X87B1210B8581D5B2"></a></p>
816
817 <h5><code>14.5-1 \mod</code></h5>
818
819 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; \mod</code>( <var class="Arg">r/s</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
820 <p>If <var class="Arg">r</var>, <var class="Arg">s</var> and <var class="Arg">n</var> are integers, <code class="code"><var class="Arg">r</var> / <var class="Arg">s</var></code> as a reduced fraction is <code class="code">p/q</code>, where <code class="code">q</code> and <var class="Arg">n</var> are coprime, then <code class="code"><var class="Arg">r</var> / <var class="Arg">s</var> mod <var class="Arg">n</var></code> is defined to be the product of <code class="code">p</code> and the inverse of <code class="code">q</code> modulo <var class="Arg">n</var>. See Section <a href="chap4.html#X7B66C8707B5DE10A"><span class="RefLink">4.14</span></a> for more details and definitions.</p>
821
822 <p>With the above definition, <code class="code">4 / 6 mod 32</code> equals <code class="code">2 / 3 mod 32</code> and hence exists (and is equal to 22), despite the fact that 6 has no inverse modulo 32.</p>
823
824 <p><a id="X79CE76AD82B3E2B2" name="X79CE76AD82B3E2B2"></a></p>
825
826 <h5>14.5-2 ZmodnZ</h5>
827
828 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ZmodnZ</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
829 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ZmodpZ</code>( <var class="Arg">p</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
830 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ZmodpZNC</code>( <var class="Arg">p</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
831 <p><code class="func">ZmodnZ</code> returns a ring <span class="SimpleMath">R</span> isomorphic to the residue class ring of the integers modulo the ideal generated by <var class="Arg">n</var>. The element corresponding to the residue class of the integer <span class="SimpleMath">i</span> in this ring can be obtained by <code class="code">i * One( R )</code>, and a representative of the residue class corresponding to the element <span class="SimpleMath">x ∈ R</span> can be computed by <code class="code">Int</code><span class="SimpleMath">( x )</span>.</p>
832
833 <p><code class="code">ZmodnZ( <var class="Arg">n</var> )</code> is equal to <code class="code">Integers mod <var class="Arg">n</var></code>.</p>
834
835 <p><code class="func">ZmodpZ</code> does the same if the argument <var class="Arg">p</var> is a prime integer, additionally the result is a field. <code class="func">ZmodpZNC</code> omits the check whether <var class="Arg">p</var> is a prime.</p>
836
837 <p>Each ring returned by these functions contains the whole family of its elements if <var class="Arg">n</var> is not a prime, and is embedded into the family of finite field elements of characteristic <var class="Arg">n</var> if <var class="Arg">n</var> is a prime.</p>
838
839 <p><a id="X838F36507D985EDA" name="X838F36507D985EDA"></a></p>
840
841 <h5>14.5-3 ZmodnZObj</h5>
842
843 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ZmodnZObj</code>( <var class="Arg">Fam</var>, <var class="Arg">r</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
844 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ZmodnZObj</code>( <var class="Arg">r</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
845 <p>If the first argument is a residue class family <var class="Arg">Fam</var> then <code class="func">ZmodnZObj</code> returns the element in <var class="Arg">Fam</var> whose coset is represented by the integer <var class="Arg">r</var>.</p>
846
847 <p>If the two arguments are an integer <var class="Arg">r</var> and a positive integer <var class="Arg">n</var> then <code class="func">ZmodnZObj</code> returns the element in <code class="code">ZmodnZ( <var class="Arg">n</var> )</code> (see <code class="func">ZmodnZ</code> (<a href="chap14.html#X79CE76AD82B3E2B2"><span class="RefLink">14.5-2</span></a>)) whose coset is represented by the integer <var class="Arg">r</var>.</p>
848
849
850 <div class="example"><pre>
851 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">r:= ZmodnZ(15);</span>
852 (Integers mod 15)
853 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fam:=ElementsFamily(FamilyObj(r));;</span>
854 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a:= ZmodnZObj(fam,9);</span>
855 ZmodnZObj( 9, 15 )
856 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">a+a;</span>
857 ZmodnZObj( 3, 15 )
858 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Int(a+a);</span>
859 3
860 </pre></div>
861
862 <p><a id="X7D0107DD79753901" name="X7D0107DD79753901"></a></p>
863
864 <h5>14.5-4 IsZmodnZObj</h5>
865
866 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsZmodnZObj</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
867 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsZmodnZObjNonprime</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
868 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsZmodpZObj</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
869 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsZmodpZObjSmall</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
870 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsZmodpZObjLarge</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
871 <p>The elements in the rings <span class="SimpleMath">Z / n Z</span> are in the category <code class="func">IsZmodnZObj</code>. If <span class="SimpleMath">n</span> is a prime then the elements are of course also in the category <code class="func">IsFFE</code> (<a href="chap59.html#X7D3DF32C84FEBD25"><span class="RefLink">59.1-1</span></a>), otherwise they are in <code class="func">IsZmodnZObjNonprime</code>. <code class="func">IsZmodpZObj</code> is an abbreviation of <code class="code">IsZmodnZObj and IsFFE</code>. This category is the disjoint union of <code class="func">IsZmodpZObjSmall</code> and <code class="func">IsZmodpZObjLarge</code>, the former containing all elements with <span class="SimpleMath">n</span> at most <code class="code">MAXSIZE_GF_INTERNAL</code>.</p>
872
873 <p>The reasons to distinguish the prime case from the nonprime case are</p>
874
875
876 <ul>
877 <li><p>that objects in <code class="func">IsZmodnZObjNonprime</code> have an external representation (namely the residue in the range <span class="SimpleMath">[ 0, 1, ..., n-1 ]</span>),</p>
878
879 </li>
880 <li><p>that the comparison of elements can be defined as comparison of the residues, and</p>
881
882 </li>
883 <li><p>that the elements lie in a family of type <code class="code">IsZmodnZObjNonprimeFamily</code> (note that for prime <span class="SimpleMath">n</span>, the family must be an <code class="code">IsFFEFamily</code>).</p>
884
885 </li>
886 </ul>
887 <p>The reasons to distinguish the small and the large case are that for small <span class="SimpleMath">n</span> the elements must be compatible with the internal representation of finite field elements, whereas we are free to define comparison as comparison of residues for large <span class="SimpleMath">n</span>.</p>
888
889 <p>Note that we <em>cannot</em> claim that every finite field element of degree 1 is in <code class="func">IsZmodnZObj</code>, since finite field elements in internal representation may not know that they lie in the prime field.</p>
890
891 <p><a id="X7904B6D681EBF091" name="X7904B6D681EBF091"></a></p>
892
893 <h4>14.6 <span class="Heading">Check Digits</span></h4>
894
895 <p><a id="X82BABA8F868BD425" name="X82BABA8F868BD425"></a></p>
896
897 <h5>14.6-1 CheckDigitISBN</h5>
898
899 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CheckDigitISBN</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
900 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CheckDigitISBN13</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
901 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CheckDigitPostalMoneyOrder</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
902 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CheckDigitUPC</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
903 <p>These functions can be used to compute, or check, check digits for some everyday items. In each case what is submitted as input is either the number with check digit (in which case the function returns <code class="keyw">true</code> or <code class="keyw">false</code>), or the number without check digit (in which case the function returns the missing check digit). The number can be specified as integer, as string (for example in case of leading zeros) or as a sequence of arguments, each representing a single digit. The check digits tested are the 10-digit ISBN (International Standard Book Number) using <code class="func">CheckDigitISBN</code> (since arithmetic is module 11, a digit 11 is represented by an X); the newer 13-digit ISBN-13 using <code class="func">CheckDigitISBN13</code>; the numbers of 11-digit US postal money orders using <code class="func">CheckDigitPostalMoneyOrder</code>; and the 12-digit UPC bar code found on groceries using <code class="func">CheckDigitUPC</code>.</p>
904
905
906 <div class="example"><pre>
907 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitISBN("052166103");</span>
908 Check Digit is 'X'
909 'X'
910 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitISBN("052166103X");</span>
911 Checksum test satisfied
912 true
913 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitISBN(0,5,2,1,6,6,1,0,3,1);</span>
914 Checksum test failed
915 false
916 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitISBN(0,5,2,1,6,6,1,0,3,'X'); # note single quotes!</span>
917 Checksum test satisfied
918 true
919 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitISBN13("9781420094527");</span>
920 Checksum test satisfied
921 true
922 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitUPC("07164183001");</span>
923 Check Digit is 1
924 1
925 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CheckDigitPostalMoneyOrder(16786457155);</span>
926 Checksum test satisfied
927 true
928 </pre></div>
929
930 <p><a id="X85F1A6A5870485B9" name="X85F1A6A5870485B9"></a></p>
931
932 <h5>14.6-2 CheckDigitTestFunction</h5>
933
934 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CheckDigitTestFunction</code>( <var class="Arg">l</var>, <var class="Arg">m</var>, <var class="Arg">f</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
935 <p>This function creates check digit test functions such as <code class="func">CheckDigitISBN</code> (<a href="chap14.html#X82BABA8F868BD425"><span class="RefLink">14.6-1</span></a>) for check digit schemes that use the inner products with a fixed vector modulo a number. The scheme creates will use strings of <var class="Arg">l</var> digits (including the check digits), the check consists of taking the standard product of the vector of digits with the fixed vector <var class="Arg">f</var> modulo <var class="Arg">m</var>; the result needs to be 0. The function returns a function that then can be used for testing or determining check digits.</p>
936
937
938 <div class="example"><pre>
939 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">isbntest:=CheckDigitTestFunction(10,11,[1,2,3,4,5,6,7,8,9,-1]);</span>
940 function( arg... ) ... end
941 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">isbntest("038794680");</span>
942 Check Digit is 2
943 2
944 </pre></div>
945
946 <p><a id="X85361FAE8088C006" name="X85361FAE8088C006"></a></p>
947
948 <h4>14.7 <span class="Heading">Random Sources</span></h4>
949
950 <p><strong class="pkg">GAP</strong> provides <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>) methods for many collections of objects. On a lower level these methods use <em>random sources</em> which provide random integers and random choices from lists.</p>
951
952 <p>See <code class="func">IsRandomSource</code> (<a href="chap14.html#X82E31A697E389F1D"><span class="RefLink">14.7-1</span></a>) for the user interface for random sources, and Section <a href="chap14.html#X8653AE447D94C1DC"><span class="RefLink">14.7-6</span></a> for information about developing new kinds of random sources.</p>
953
954 <p><a id="X82E31A697E389F1D" name="X82E31A697E389F1D"></a></p>
955
956 <h5>14.7-1 IsRandomSource</h5>
957
958 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsRandomSource</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
959 <p>This is the category of random source objects. The <em>user interface</em> for these objects consists of the following functions.</p>
960
961 <p><code class="func">RandomSource</code> (<a href="chap14.html#X7CB0B5BC82F8FD8F"><span class="RefLink">14.7-5</span></a>) creates a new random source <var class="Arg">rs</var>, say.</p>
962
963 <p><code class="code">Random( <var class="Arg">rs</var>, <var class="Arg">list</var> )</code> yields a random element of the list <var class="Arg">list</var>, and <code class="code">Random( <var class="Arg">rs</var>, <var class="Arg">low</var>, <var class="Arg">high</var> )</code> yields a random integer between <var class="Arg">low</var> and <var class="Arg">high</var> (inclusive), see <code class="func">Random</code> (<a href="chap14.html#X821004F286282D49"><span class="RefLink">14.7-2</span></a>).</p>
964
965 <p>If <var class="Arg">rs</var> supports resetting (see <code class="func">State</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>)) then <code class="code">State( <var class="Arg">rs</var> )</code> yields a copy <var class="Arg">state</var>, say, of the current state of <var class="Arg">rs</var> such that <code class="code">Reset( <var class="Arg">rs</var>, <var class="Arg">state</var> )</code> resets <var class="Arg">rs</var> to the given state.</p>
966
967 <p>One idea behind providing several independent (pseudo) random sources is to make algorithms which use some sort of random choices deterministic. They can use their own new random source created with a fixed seed and so do exactly the same in different calls.</p>
968
969 <p>Random source objects lie in the family <code class="code">RandomSourcesFamily</code>.</p>
970
971 <p><a id="X821004F286282D49" name="X821004F286282D49"></a></p>
972
973 <h5>14.7-2 Random</h5>
974
975 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Random</code>( <var class="Arg">rs</var>, <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
976 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Random</code>( <var class="Arg">rs</var>, <var class="Arg">coll</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
977 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Random</code>( <var class="Arg">rs</var>, <var class="Arg">low</var>, <var class="Arg">high</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
978 <p>This operation returns a random element from the dense, nonempty list <var class="Arg">list</var> or the nonempty collection <var class="Arg">coll</var>, or an integer in the range from the given (possibly large) integers <var class="Arg">low</var> to <var class="Arg">high</var>, respectively.</p>
979
980 <p>The choice should only depend on the random source <var class="Arg">rs</var> and have no effect on other random sources.</p>
981
982 <p>It is not defined what happens if <var class="Arg">list</var> or <var class="Arg">coll</var> is empty, <var class="Arg">list</var> is not dense, or <var class="Arg">low</var> is larger than <var class="Arg">high</var>.</p>
983
984
985 <div class="example"><pre>
986 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">mysource := RandomSource(IsMersenneTwister, 42);;</span>
987 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Random(mysource, 1, 10^60);</span>
988 999331861769949319194941485000557997842686717712198687315183
989 </pre></div>
990
991 <p><a id="X86FFFBC9790F9742" name="X86FFFBC9790F9742"></a></p>
992
993 <h5>14.7-3 <span class="Heading">State and Reset for Random Sources</span></h5>
994
995 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; State</code>( <var class="Arg">rs</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
996 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Reset</code>( <var class="Arg">rs</var>[, <var class="Arg">seed</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
997 <p>These are the basic operations for random sources (see <code class="func">IsRandomSource</code> (<a href="chap14.html#X82E31A697E389F1D"><span class="RefLink">14.7-1</span></a>)).</p>
998
999 <p><code class="func">State</code> returns a data structure which admits recovering the state of the random source such that a sequence of random calls using this random source can be reproduced. If a random source cannot be reset (say, it uses truly random physical data) then <code class="func">State</code> returns <code class="keyw">fail</code>.</p>
1000
1001 <p><code class="code">Reset( <var class="Arg">rs</var>, <var class="Arg">seed</var> )</code> resets the random source <var class="Arg">rs</var> to a state described by <var class="Arg">seed</var>, if the random source can be reset; otherwise it does nothing. Here <var class="Arg">seed</var> can be an output of <code class="func">State</code> and then <var class="Arg">rs</var> gets reset to that state. For historical reasons, random sources accept integer values as <var class="Arg">seed</var>. We recommend that new code should not rely on this; always use the output of a prior call to <code class="func">State</code> as <var class="Arg">seed</var>, or omit it. Without the <var class="Arg">seed</var> argument a fixed default seed is used. <code class="func">Reset</code> returns the state of <var class="Arg">rs</var> before the call.</p>
1002
1003 <p>Most methods for <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>) in the <strong class="pkg">GAP</strong> library that do not take a random source as argument use the <code class="func">GlobalMersenneTwister</code> (<a href="chap14.html#X7AC96008820FAF1F"><span class="RefLink">14.7-4</span></a>) as random source. It can be reset into a known state as in the following example.</p>
1004
1005
1006 <div class="example"><pre>
1007 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">seed := Reset(GlobalMersenneTwister);;</span>
1008 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">seed = State(GlobalMersenneTwister);</span>
1009 true
1010 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1..10],i-&gt;Random(Integers));</span>
1011 [ -3, 2, -1, -2, -1, -1, 1, -4, 1, 0 ]
1012 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1..10],i-&gt;Random(Integers));</span>
1013 [ -1, -1, -1, 1, -1, 1, -2, -1, -2, 0 ]
1014 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Reset(GlobalMersenneTwister, seed);;</span>
1015 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List([1..10],i-&gt;Random(Integers));</span>
1016 [ -3, 2, -1, -2, -1, -1, 1, -4, 1, 0 ]
1017 </pre></div>
1018
1019 <p><a id="X7AC96008820FAF1F" name="X7AC96008820FAF1F"></a></p>
1020
1021 <h5>14.7-4 <span class="Heading">Kinds of Random Sources</span></h5>
1022
1023 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsMersenneTwister</code>( <var class="Arg">rs</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
1024 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsGAPRandomSource</code>( <var class="Arg">rs</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
1025 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsGlobalRandomSource</code>( <var class="Arg">rs</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
1026 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; GlobalMersenneTwister</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
1027 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; GlobalRandomSource</code></td><td class="tdright">(&nbsp;global variable&nbsp;)</td></tr></table></div>
1028 <p>Currently, the <strong class="pkg">GAP</strong> library provides three types of random sources, distinguished by the three listed categories.</p>
1029
1030 <p><code class="func">IsMersenneTwister</code> are random sources which use a fast random generator of 32 bit numbers, called the Mersenne twister. The pseudo random sequence has a period of <span class="SimpleMath">2^19937-1</span> and the numbers have a <span class="SimpleMath">623</span>-dimensional equidistribution. For more details and the origin of the code used in the <strong class="pkg">GAP</strong> kernel, see: <span class="URL"><a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html">http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html</a></span>.</p>
1031
1032 <p>Use the Mersenne twister if possible, in particular for generating many large random integers.</p>
1033
1034 <p>There is also a predefined global random source <code class="func">GlobalMersenneTwister</code> which is used as the default random source by those library methods for <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>) that do not take a random source as an argument.</p>
1035
1036 <p><code class="func">IsGAPRandomSource</code> uses the same number generator as <code class="func">IsGlobalRandomSource</code>, but you can create several of these random sources which generate their random numbers independently of all other random sources.</p>
1037
1038 <p><code class="func">IsGlobalRandomSource</code> gives access to the <em>classical</em> global random generator which was used by <strong class="pkg">GAP</strong> in former releases. You do not need to construct new random sources of this kind which would all use the same global data structure. Just use the existing random source <code class="func">GlobalRandomSource</code>. This uses the additive random number generator described in <a href="chapBib.html#biBTACP2">[Knu98]</a> (Algorithm A in 3.2.2 with lag <span class="SimpleMath">30</span>).</p>
1039
1040 <p>Other kinds of random sources are implemented by <strong class="pkg">GAP</strong> packages.</p>
1041
1042 <p><a id="X7CB0B5BC82F8FD8F" name="X7CB0B5BC82F8FD8F"></a></p>
1043
1044 <h5>14.7-5 RandomSource</h5>
1045
1046 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RandomSource</code>( <var class="Arg">cat</var>[, <var class="Arg">seed</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
1047 <p>This operation is used to create new random sources. The first argument <var class="Arg">cat</var> is the category describing the type of the random generator, for example one of the categories listed in Section <a href="chap14.html#X7AC96008820FAF1F"><span class="RefLink">14.7-4</span></a>.</p>
1048
1049 <p>An optional <var class="Arg">seed</var> can be given to specify the initial state. For details, see Section <a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>.</p>
1050
1051
1052 <div class="example"><pre>
1053 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">rs1 := RandomSource(IsMersenneTwister);</span>
1054 &lt;RandomSource in IsMersenneTwister&gt;
1055 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 := List([1..10000], i-&gt; Random(rs1, [1..6]));;</span>
1056 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">state1 := State(rs1);;</span>
1057 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">rs2 := RandomSource(IsMersenneTwister);;</span>
1058 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l2 := List([1..10000], i-&gt; Random(rs2, [1..6]));;</span>
1059 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 = l2;</span>
1060 true
1061 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l3 := List([1..10000], i-&gt; Random(rs1, [1..6]));;</span>
1062 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l1 = l3;</span>
1063 false
1064 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">rs3 := RandomSource(IsMersenneTwister, state1);;</span>
1065 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l4 := List([1..10000], i-&gt; Random(rs3, [1..6]));;</span>
1066 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l3 = l4;</span>
1067 true
1068 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">n := Random(rs1, 1, 2^220);</span>
1069 1077726777923092117987668044202944212469136000816111066409337432400
1070 </pre></div>
1071
1072 <p><a id="X8653AE447D94C1DC" name="X8653AE447D94C1DC"></a></p>
1073
1074 <h5>14.7-6 <span class="Heading">Implementing new kinds of random sources</span></h5>
1075
1076 <p>If one wants to implement a new kind of random sources then the first step is the declaration of a new category <code class="code">C</code>, say, that implies <code class="func">IsRandomSource</code> (<a href="chap14.html#X82E31A697E389F1D"><span class="RefLink">14.7-1</span></a>), analogous to the categories listed in Section <a href="chap14.html#X7AC96008820FAF1F"><span class="RefLink">14.7-4</span></a>, as follows.</p>
1077
1078 <p><code class="code">DeclareCategory( "C", IsRandomSource );</code>.</p>
1079
1080 <p>Then the following method installations are needed.</p>
1081
1082 <p><code class="code">InstallMethod( Init, [ C, IsObject ], function( prers, seed ) ... end );</code></p>
1083
1084 <p>Here <code class="code">prers</code> is an empty component object (which has already the filter <code class="code">C</code>), and <code class="code">seed</code> is an integer or a state value as returned by <code class="func">State</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>) that describes the initial state of the random source. The function should fill in the actual data and then return the (now initialized) object <code class="code">prers</code>. The default used for <code class="code">seed</code> is the integer <code class="code">1</code>. A given state value need not be copied by the function.</p>
1085
1086 <p><code class="code">InstallMethod( Random, [ C, IsInt, IsInt ], function( rs, low, high ) ... end );</code></p>
1087
1088 <p>Here <code class="code">rs</code> is an already initialized random source object in the category <code class="code">C</code>, and the function returns an integer between <code class="code">low</code> and <code class="code">high</code> (inclusive). It is not defined what happens when <code class="code">low</code> is larger than <code class="code">high</code>.</p>
1089
1090 <p><code class="code">InstallMethod( State, [ C ], function( rs ) ... end );</code></p>
1091
1092 <p>If <code class="code">rs</code> supports resetting then the function must return an object that describes the current state of <code class="code">rs</code>. This object must be an independent copy, that is, calling <code class="func">Random</code> (<a href="chap14.html#X821004F286282D49"><span class="RefLink">14.7-2</span></a>) for <code class="code">rs</code> must not change the object that was returned by <code class="func">State</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>); otherwise <code class="func">ReturnFail</code> (<a href="chap5.html#X7A0994DE7C258E55"><span class="RefLink">5.4-3</span></a>) should be installed.</p>
1093
1094 <p><code class="code">InstallMethod( Reset, [ C, IsObject ], function( rs, seed ) ... end );</code></p>
1095
1096 <p>If <code class="code">rs</code> supports resetting then the function must reinitialize <code class="code">rs</code> to the integer or <code class="func">State</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>) value <code class="code">seed</code> and must return the <code class="func">State</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>) value of <code class="code">rs</code> before these changes; if resetting is not supported then <code class="func">ReturnNothing</code> (<a href="chap5.html#X818EA8C47B46A634"><span class="RefLink">5.4-4</span></a>) should be installed. Reset need not copy a given state. Note that the generic unary <code class="func">Reset</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>) method uses the default seed <code class="code">1</code>.</p>
1097
1098 <p>Examples of implementations as described here are given by the random sources with defining filter <code class="func">IsMersenneTwister</code> (<a href="chap14.html#X7AC96008820FAF1F"><span class="RefLink">14.7-4</span></a>) or <code class="code">IsRealRandomSource</code>. (For the latter, see <code class="func">RandomSource</code> (<a href="../../pkg/io/doc/chap6.html#X7CB0B5BC82F8FD8F"><span class="RefLink">IO: RandomSource</span></a>) in the <strong class="pkg">GAP</strong> package <strong class="pkg">IO</strong>.)</p>
1099
1100 <p><a id="X7A0311DF78DB4FD8" name="X7A0311DF78DB4FD8"></a></p>
1101
1102 <h4>14.8 <span class="Heading">Bitfields</span></h4>
1103
1104 <p>Bitfields are a low-level feature intended to support efficient subdivision of immediate integers into bitfields of various widths. This is typically useful in implementing space-efficient and/or cache-efficient data structures. This feature should be used with care because (<em>inter alia</em>) it has different limitations on 32-bit and 64-bit architectures.</p>
1105
1106 <p><a id="X85C7BD9E7FCC6C10" name="X85C7BD9E7FCC6C10"></a></p>
1107
1108 <h5>14.8-1 MakeBitfields</h5>
1109
1110 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MakeBitfields</code>( <var class="Arg">width....</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1111 <p>This function sets up the machinery for a set of bitfields of the given widths. All bitfield values are treated as unsigned. The total of the widths must not exceed 60 bits on 64-bit architecture or 28 bits on a 32-bit architecture. For performance reasons some checks that one might wish to do are omitted. In particular, the builder and setter functions do not check if the value[s] passed to them are negative or too large (unless <strong class="pkg">GAP</strong> is specially compiled for debugging). Behaviour when such arguments are passed is undefined. You can tell which type of architecture you are running on by accessing <code class="code">GAPInfo.BytesPerVariable</code> which is 8 on 64-bits and 4 on 32. The return value when <span class="SimpleMath">n</span> widths are given is a record whose fields are</p>
1112
1113
1114 <dl>
1115 <dt><strong class="Mark"><code class="code">widths</code></strong></dt>
1116 <dd><p>a copy of the arguments, for convenience,</p>
1117
1118 </dd>
1119 <dt><strong class="Mark"><code class="code">getters</code></strong></dt>
1120 <dd><p>a list of <span class="SimpleMath">n</span> functions of one argument each of which extracts one of the fields from an immediate integer</p>
1121
1122 </dd>
1123 <dt><strong class="Mark"><code class="code">setters</code></strong></dt>
1124 <dd><p>a list of <span class="SimpleMath">n</span> functions each taking two arguments: a packed value and a new value for one of its fields and returning a new packed value. The <span class="SimpleMath">i</span>th function returned the new packed value in which the <span class="SimpleMath">i</span>th field has been replaced by the new value. Note that this does NOT modify the original packed value.</p>
1125
1126 </dd>
1127 </dl>
1128 <p>Two additional fields may be present if any of the field widths is one. Each is a list and only has entried bound in the positions corresponding to the width 1 fields.</p>
1129
1130
1131 <dl>
1132 <dt><strong class="Mark"><code class="code">booleanGetters</code></strong></dt>
1133 <dd><p>if the <span class="SimpleMath">i</span>th position of this list is set, it contains a function which extracts the <span class="SimpleMath">i</span>th field (which will have width one) and returns <code class="keyw">true</code> if it contains 1 and <code class="keyw">false</code> if it contains 0</p>
1134
1135 </dd>
1136 <dt><strong class="Mark"><code class="code">booleanSetters</code></strong></dt>
1137 <dd><p>if the <span class="SimpleMath">i</span>th position of this list is set, it contains a function of two arguments. The first argument is a packed value, the second is <code class="keyw">true</code> or <code class="keyw">false</code>. It returns a new packed value in which the <span class="SimpleMath">i</span>th field is set to 1 if the second argument was <code class="keyw">true</code> and 0 if it was <code class="keyw">false</code>. Behaviour for any other value is undefined.</p>
1138
1139 </dd>
1140 </dl>
1141 <p><a id="X8068CE3781F4003C" name="X8068CE3781F4003C"></a></p>
1142
1143 <h5>14.8-2 BuildBitfields</h5>
1144
1145 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; BuildBitfields</code>( <var class="Arg">widths</var>, <var class="Arg">vals...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
1146 <p>This function takes one or more argument. Its first argument is a list of field widths, as found in the <code class="code">widths</code> entry of a record returned by <code class="code">MakeBitfields</code>. The remaining arguments are unsigned integer values, equal in number to the entries of the list of field widths. It returns a small integer in which those entries are packed into bitfields of the given widths. The first entry occupies the least significant bits. DeclareGlobalFunction("BuildBitfields");</p>
1147
1148
1149 <div class="example"><pre>
1150 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">bf := MakeBitfields(1,2,3);</span>
1151 rec( booleanGetters := [ function( data ) ... end ],
1152 booleanSetters := [ function( data, val ) ... end ],
1153 getters := [ function( data ) ... end, function( data ) ... end,
1154 function( data ) ... end ],
1155 setters := [ function( data, val ) ... end, function( data, val ) ... end,
1156 function( data, val ) ... end ], widths := [ 1, 2, 3 ] )
1157 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">x := BuildBitfields(bf.widths,0,3,5);</span>
1158 46
1159 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">bf.getters[3](x);</span>
1160 5
1161 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">y := bf.setters[1](x,1);</span>
1162 47
1163 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">x;</span>
1164 46
1165 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">bf.booleanGetters[1](x);</span>
1166 false
1167 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">bf.booleanGetters[1](y);</span>
1168 true
1169 </pre></div>
1170
1171
1172 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap13.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap15.html">[Next Chapter]</a>&nbsp; </div>
1173
1174
1175 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
1176
1177 <hr />
1178 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
1179 </body>
1180 </html>