Debian Codegraph @

doc/ref/chap15.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 15: Number Theory</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="chap15" 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="chap14.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap16.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap15_mj.html">[MathJax on]</a></p>
23 <p><a id="X7FB995737B7ED8A2" name="X7FB995737B7ED8A2"></a></p>
24 <div class="ChapSects"><a href="chap15.html#X7FB995737B7ED8A2">15 <span class="Heading">Number Theory</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X7845C1F97A1742C7">15.1 <span class="Heading">InfoNumtheor (Info Class)</span></a>
26 </span>
27 <div class="ContSSBlock">
28 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X796F0DFE7D5D211C">15.1-1 InfoNumtheor</a></span>
29 </div></div>
30 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X823386567DAC22E6">15.2 <span class="Heading">Prime Residues</span></a>
31 </span>
32 <div class="ContSSBlock">
33 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X7FA3F5347B7004BA">15.2-1 PrimeResidues</a></span>
34 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X85A0C67982D9057A">15.2-2 Phi</a></span>
35 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X85296F3087611B03">15.2-3 Lambda</a></span>
36 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X7D191CF67E5018BE">15.2-4 GeneratorsPrimeResidues</a></span>
37 </div></div>
38 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X83103A5385821BAE">15.3 <span class="Heading">Primitive Roots and Discrete Logarithms</span></a>
39 </span>
40 <div class="ContSSBlock">
41 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X82373F3D8277EE9E">15.3-1 OrderMod</a></span>
42 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X81AD9C7779A7BA89">15.3-2 LogMod</a></span>
43 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X84A138947E8C49A8">15.3-3 DLog</a></span>
44 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X82440BB9812FF148">15.3-4 PrimitiveRootMod</a></span>
45 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X790466C07BD90E20">15.3-5 IsPrimitiveRootMod</a></span>
46 </div></div>
47 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X7F9069D77AC48054">15.4 <span class="Heading">Roots Modulo Integers</span></a>
48 </span>
49 <div class="ContSSBlock">
50 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X83449DBC80495971">15.4-1 Jacobi</a></span>
51 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X81464ABF7F10E544">15.4-2 Legendre</a></span>
52 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X83E3ED577B7A04ED">15.4-3 RootMod</a></span>
53 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X84D3F03B862841F8">15.4-4 RootsMod</a></span>
54 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X81F856E682A8ECBA">15.4-5 RootsUnityMod</a></span>
55 </div></div>
56 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X7B3A5A0378A32F83">15.5 <span class="Heading">Multiplicative Arithmetic Functions</span></a>
57 </span>
58 <div class="ContSSBlock">
59 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X823707DF821E79A0">15.5-1 Sigma</a></span>
60 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X798C62847EE0372E">15.5-2 Tau</a></span>
61 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X79C1DA36827C2959">15.5-3 MoebiusMu</a></span>
62 </div></div>
63 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X7B2E061C835159B9">15.6 <span class="Heading">Continued Fractions</span></a>
64 </span>
65 <div class="ContSSBlock">
66 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X874C161B83416092">15.6-1 ContinuedFractionExpansionOfRoot</a></span>
67 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X8059667580A039A6">15.6-2 ContinuedFractionApproximationOfRoot</a></span>
68 </div></div>
69 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap15.html#X7C5563A37D566DA5">15.7 <span class="Heading">Miscellaneous</span></a>
70 </span>
71 <div class="ContSSBlock">
72 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X8243EAA586D78ED4">15.7-1 PValuation</a></span>
73 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap15.html#X85E1EFC484F648A4">15.7-2 TwoSquares</a></span>
74 </div></div>
75 </div>
76
77 <h3>15 <span class="Heading">Number Theory</span></h3>
78
79 <p><strong class="pkg">GAP</strong> provides a couple of elementary number theoretic functions. Most of these deal with the group of integers coprime to <span class="SimpleMath">m</span>, called the <em>prime residue group</em>. The order of this group is <span class="SimpleMath">ϕ(m)</span> (see <code class="func">Phi</code> (<a href="chap15.html#X85A0C67982D9057A"><span class="RefLink">15.2-2</span></a>)), and <span class="SimpleMath">λ(m)</span> (see <code class="func">Lambda</code> (<a href="chap15.html#X85296F3087611B03"><span class="RefLink">15.2-3</span></a>)) is its exponent. This group is cyclic if and only if <span class="SimpleMath">m</span> is 2, 4, an odd prime power <span class="SimpleMath">p^n</span>, or twice an odd prime power <span class="SimpleMath">2 p^n</span>. In this case the generators of the group, i.e., elements of order <span class="SimpleMath">ϕ(m)</span>, are called <em>primitive roots</em> (see <code class="func">PrimitiveRootMod</code> (<a href="chap15.html#X82440BB9812FF148"><span class="RefLink">15.3-4</span></a>)).</p>
80
81 <p>Note that neither the arguments nor the return values of the functions listed below are groups or group elements in the sense of <strong class="pkg">GAP</strong>. The arguments are simply integers.</p>
82
83 <p><a id="X7845C1F97A1742C7" name="X7845C1F97A1742C7"></a></p>
84
85 <h4>15.1 <span class="Heading">InfoNumtheor (Info Class)</span></h4>
86
87 <p><a id="X796F0DFE7D5D211C" name="X796F0DFE7D5D211C"></a></p>
88
89 <h5>15.1-1 InfoNumtheor</h5>
90
91 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; InfoNumtheor</code></td><td class="tdright">(&nbsp;info class&nbsp;)</td></tr></table></div>
92 <p><code class="func">InfoNumtheor</code> is the info class (see <a href="chap7.html#X7A9C902479CB6F7C"><span class="RefLink">7.4</span></a>) for the functions in the number theory chapter.</p>
93
94 <p><a id="X823386567DAC22E6" name="X823386567DAC22E6"></a></p>
95
96 <h4>15.2 <span class="Heading">Prime Residues</span></h4>
97
98 <p><a id="X7FA3F5347B7004BA" name="X7FA3F5347B7004BA"></a></p>
99
100 <h5>15.2-1 PrimeResidues</h5>
101
102 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrimeResidues</code>( <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
103 <p><code class="func">PrimeResidues</code> returns the set of integers from the range <code class="code">[ 0 .. Abs( <var class="Arg">m</var> )-1 ]</code> that are coprime to the integer <var class="Arg">m</var>.</p>
104
105 <p><code class="code">Abs(<var class="Arg">m</var>)</code> must be less than <span class="SimpleMath">2^28</span>, otherwise the set would probably be too large anyhow.</p>
106
107
108 <div class="example"><pre>
109 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimeResidues( 0 ); PrimeResidues( 1 ); PrimeResidues( 20 );</span>
110 [ ]
111 [ 0 ]
112 [ 1, 3, 7, 9, 11, 13, 17, 19 ]
113 </pre></div>
114
115 <p><a id="X85A0C67982D9057A" name="X85A0C67982D9057A"></a></p>
116
117 <h5>15.2-2 Phi</h5>
118
119 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Phi</code>( <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
120 <p><code class="func">Phi</code> returns the number <span class="SimpleMath">ϕ(<var class="Arg">m</var>)</span> of positive integers less than the positive integer <var class="Arg">m</var> that are coprime to <var class="Arg">m</var>.</p>
121
122 <p>Suppose that <span class="SimpleMath">m = p_1^{e_1} p_2^{e_2} ⋯ p_k^{e_k}</span>. Then <span class="SimpleMath">ϕ(m)</span> is <span class="SimpleMath">p_1^{e_1-1} (p_1-1) p_2^{e_2-1} (p_2-1) ⋯ p_k^{e_k-1} (p_k-1)</span>.</p>
123
124
125 <div class="example"><pre>
126 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Phi( 12 );</span>
127 4
128 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Phi( 2^13-1 ); # this proves that 2^(13)-1 is a prime</span>
129 8190
130 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Phi( 2^15-1 );</span>
131 27000
132 </pre></div>
133
134 <p><a id="X85296F3087611B03" name="X85296F3087611B03"></a></p>
135
136 <h5>15.2-3 Lambda</h5>
137
138 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Lambda</code>( <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
139 <p><code class="func">Lambda</code> returns the exponent <span class="SimpleMath">λ(<var class="Arg">m</var>)</span> of the group of prime residues modulo the integer <var class="Arg">m</var>.</p>
140
141 <p><span class="SimpleMath">λ(<var class="Arg">m</var>)</span> is the smallest positive integer <span class="SimpleMath">l</span> such that for every <span class="SimpleMath">a</span> relatively prime to <var class="Arg">m</var> we have <span class="SimpleMath">a^l ≡ 1 mod <var class="Arg">m</var></span>. Fermat's theorem asserts <span class="SimpleMath">a^{ϕ(<var class="Arg">m</var>)} ≡ 1 mod <var class="Arg">m</var></span>; thus <span class="SimpleMath">λ(<var class="Arg">m</var>)</span> divides <span class="SimpleMath">ϕ(<var class="Arg">m</var>)</span> (see <code class="func">Phi</code> (<a href="chap15.html#X85A0C67982D9057A"><span class="RefLink">15.2-2</span></a>)).</p>
142
143 <p>Carmichael's theorem states that <span class="SimpleMath">λ</span> can be computed as follows: <span class="SimpleMath">λ(2) = 1</span>, <span class="SimpleMath">λ(4) = 2</span> and <span class="SimpleMath">λ(2^e) = 2^{e-2}</span> if <span class="SimpleMath">3 ≤ e</span>, <span class="SimpleMath">λ(p^e) = (p-1) p^{e-1}</span> (i.e. <span class="SimpleMath">ϕ(m)</span>) if <span class="SimpleMath">p</span> is an odd prime and <span class="SimpleMath">λ(m*n) =</span><code class="code">Lcm</code><span class="SimpleMath">( λ(m), λ(n) )</span> if <span class="SimpleMath">m, n</span> are coprime.</p>
144
145 <p>Composites for which <span class="SimpleMath">λ(m)</span> divides <span class="SimpleMath">m - 1</span> are called Carmichaels. If <span class="SimpleMath">6k+1</span>, <span class="SimpleMath">12k+1</span> and <span class="SimpleMath">18k+1</span> are primes their product is such a number. There are only 1547 Carmichaels below <span class="SimpleMath">10^10</span> but 455052511 primes.</p>
146
147
148 <div class="example"><pre>
149 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Lambda( 10 );</span>
150 4
151 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Lambda( 30 );</span>
152 4
153 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Lambda( 561 ); # 561 is the smallest Carmichael number</span>
154 80
155 </pre></div>
156
157 <p><a id="X7D191CF67E5018BE" name="X7D191CF67E5018BE"></a></p>
158
159 <h5>15.2-4 GeneratorsPrimeResidues</h5>
160
161 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; GeneratorsPrimeResidues</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
162 <p>Let <var class="Arg">n</var> be a positive integer. <code class="func">GeneratorsPrimeResidues</code> returns a description of generators of the group of prime residues modulo <var class="Arg">n</var>. The return value is a record with components</p>
163
164
165 <dl>
166 <dt><strong class="Mark"><code class="code">primes</code>: </strong></dt>
167 <dd><p>a list of the prime factors of <var class="Arg">n</var>,</p>
168
169 </dd>
170 <dt><strong class="Mark"><code class="code">exponents</code>: </strong></dt>
171 <dd><p>a list of the exponents of these primes in the factorization of <var class="Arg">n</var>, and</p>
172
173 </dd>
174 <dt><strong class="Mark"><code class="code">generators</code>: </strong></dt>
175 <dd><p>a list describing generators of the group of prime residues; for the prime factor <span class="SimpleMath">2</span>, either a primitive root or a list of two generators is stored, for each other prime factor of <var class="Arg">n</var>, a primitive root is stored.</p>
176
177 </dd>
178 </dl>
179
180 <div class="example"><pre>
181 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GeneratorsPrimeResidues( 1 );</span>
182 rec( exponents := [ ], generators := [ ], primes := [ ] )
183 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GeneratorsPrimeResidues( 4*3 );</span>
184 rec( exponents := [ 2, 1 ], generators := [ 7, 5 ],
185 primes := [ 2, 3 ] )
186 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">GeneratorsPrimeResidues( 8*9*5 );</span>
187 rec( exponents := [ 3, 2, 1 ],
188 generators := [ [ 271, 181 ], 281, 217 ], primes := [ 2, 3, 5 ] )
189 </pre></div>
190
191 <p><a id="X83103A5385821BAE" name="X83103A5385821BAE"></a></p>
192
193 <h4>15.3 <span class="Heading">Primitive Roots and Discrete Logarithms</span></h4>
194
195 <p><a id="X82373F3D8277EE9E" name="X82373F3D8277EE9E"></a></p>
196
197 <h5>15.3-1 OrderMod</h5>
198
199 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; OrderMod</code>( <var class="Arg">n</var>, <var class="Arg">m</var>[, <var class="Arg">bound</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
200 <p><code class="func">OrderMod</code> returns the multiplicative order of the integer <var class="Arg">n</var> modulo the positive integer <var class="Arg">m</var>. If <var class="Arg">n</var> and <var class="Arg">m</var> are not coprime the order of <var class="Arg">n</var> is not defined and <code class="func">OrderMod</code> will return <code class="code">0</code>.</p>
201
202 <p>If <var class="Arg">n</var> and <var class="Arg">m</var> are relatively prime the multiplicative order of <var class="Arg">n</var> modulo <var class="Arg">m</var> is the smallest positive integer <span class="SimpleMath">i</span> such that <span class="SimpleMath"><var class="Arg">n</var>^i ≡ 1 mod <var class="Arg">m</var></span>. If the group of prime residues modulo <var class="Arg">m</var> is cyclic then each element of maximal order is called a primitive root modulo <var class="Arg">m</var> (see <code class="func">IsPrimitiveRootMod</code> (<a href="chap15.html#X790466C07BD90E20"><span class="RefLink">15.3-5</span></a>)).</p>
203
204 <p>If no a priori known multiple <var class="Arg">bound</var> of the desired order is given, <code class="func">OrderMod</code> usually spends most of its time factoring <var class="Arg">m</var> for computing <span class="SimpleMath">λ(<var class="Arg">m</var>)</span> (see <code class="func">Lambda</code> (<a href="chap15.html#X85296F3087611B03"><span class="RefLink">15.2-3</span></a>)) as the default for <var class="Arg">bound</var>, and then factoring <var class="Arg">bound</var> (see <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>)).</p>
205
206 <p>If an incorrect <var class="Arg">bound</var> is given then the result will be wrong.</p>
207
208
209 <div class="example"><pre>
210 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OrderMod( 2, 7 );</span>
211 3
212 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OrderMod( 3, 7 ); # 3 is a primitive root modulo 7</span>
213 6
214 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m:= (5^166-1) / 167;; # about 10^113</span>
215 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">OrderMod( 5, m, 166 ); # needs minutes without third argument</span>
216 166
217 </pre></div>
218
219 <p><a id="X81AD9C7779A7BA89" name="X81AD9C7779A7BA89"></a></p>
220
221 <h5>15.3-2 LogMod</h5>
222
223 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LogMod</code>( <var class="Arg">n</var>, <var class="Arg">r</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
224 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; LogModShanks</code>( <var class="Arg">n</var>, <var class="Arg">r</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
225 <p>computes the discrete <var class="Arg">r</var>-logarithm of the integer <var class="Arg">n</var> modulo the integer <var class="Arg">m</var>. It returns a number <var class="Arg">l</var> such that <span class="SimpleMath"><var class="Arg">r</var>^<var class="Arg">l</var> ≡ <var class="Arg">n</var> mod <var class="Arg">m</var></span> if such a number exists. Otherwise <code class="keyw">fail</code> is returned.</p>
226
227 <p><code class="func">LogModShanks</code> uses the Baby Step - Giant Step Method of Shanks (see for example <a href="chapBib.html#biBCoh93">[Coh93, section 5.4.1]</a>) and in general requires more memory than a call to <code class="func">LogMod</code>.</p>
228
229
230 <div class="example"><pre>
231 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= LogMod( 2, 5, 7 ); 5^l mod 7 = 2;</span>
232 4
233 true
234 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">LogMod( 1, 3, 3 ); LogMod( 2, 3, 3 );</span>
235 0
236 fail
237 </pre></div>
238
239 <p><a id="X84A138947E8C49A8" name="X84A138947E8C49A8"></a></p>
240
241 <h5>15.3-3 DLog</h5>
242
243 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DLog</code>( <var class="Arg">base</var>, <var class="Arg">x</var>[, <var class="Arg">m</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
244 <p>Returns: an integer</p>
245
246 <p>The argument <var class="Arg">base</var> must be a multiplicative element and <var class="Arg">x</var> must lie in the cyclic group generated by <var class="Arg">base</var>. The third argument <var class="Arg">m</var> must be the order of <var class="Arg">base</var> or its factorization. If <var class="Arg">m</var> is not given, it is computed first. This function returns the discrete logarithm, that is an integer <span class="SimpleMath">e</span> such that <var class="Arg">base</var><span class="SimpleMath">^e =</span> <var class="Arg">x</var>.</p>
247
248 <p>If <var class="Arg">m</var> is prime then Shanks' algorithm is used (which needs <span class="SimpleMath">O(sqrt<var class="Arg">m</var>)</span> space and time). Otherwise let <var class="Arg">m</var> <span class="SimpleMath">= r l</span> and <span class="SimpleMath">e = a + b r</span> with <span class="SimpleMath">0 ≤ a &lt; r</span>. Then <span class="SimpleMath">a =</span> <code class="code">DLog</code><span class="SimpleMath">(<var class="Arg">base</var>^l, <var class="Arg">x</var>^l, r)</span> and <span class="SimpleMath">b =</span> <code class="code">DLog</code><span class="SimpleMath">(<var class="Arg">base</var>^r, <var class="Arg">x</var>/<var class="Arg">base</var>^a, l)</span>.</p>
249
250 <p>This function is used for a method of <code class="func">LogFFE</code> (<a href="chap59.html#X7B049A3478B369E4"><span class="RefLink">59.2-2</span></a>).</p>
251
252
253 <div class="example"><pre>
254 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">q:= 67^12;</span>
255 8182718904632857144561
256 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">z:= Z(q);;</span>
257 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">DLog(z, z+1);</span>
258 2874413785388345993274
259 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">DLog(z, z^2+1);</span>
260 1667375214152688471247
261 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">DLog(z, Z(67));</span>
262 123980589464134199160
263 </pre></div>
264
265 <p><a id="X82440BB9812FF148" name="X82440BB9812FF148"></a></p>
266
267 <h5>15.3-4 PrimitiveRootMod</h5>
268
269 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PrimitiveRootMod</code>( <var class="Arg">m</var>[, <var class="Arg">start</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
270 <p><code class="func">PrimitiveRootMod</code> returns the smallest primitive root modulo the positive integer <var class="Arg">m</var> and <code class="keyw">fail</code> if no such primitive root exists. If the optional second integer argument <var class="Arg">start</var> is given <code class="func">PrimitiveRootMod</code> returns the smallest primitive root that is strictly larger than <var class="Arg">start</var>.</p>
271
272
273 <div class="example"><pre>
274 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># largest primitive root for a prime less than 2000:</span>
275 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimitiveRootMod( 409 );</span>
276 21
277 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimitiveRootMod( 541, 2 );</span>
278 10
279 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># 327 is the largest primitive root mod 337:</span>
280 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimitiveRootMod( 337, 327 );</span>
281 fail
282 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># there exists no primitive root modulo 30:</span>
283 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PrimitiveRootMod( 30 );</span>
284 fail
285 </pre></div>
286
287 <p><a id="X790466C07BD90E20" name="X790466C07BD90E20"></a></p>
288
289 <h5>15.3-5 IsPrimitiveRootMod</h5>
290
291 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsPrimitiveRootMod</code>( <var class="Arg">r</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
292 <p><code class="func">IsPrimitiveRootMod</code> returns <code class="keyw">true</code> if the integer <var class="Arg">r</var> is a primitive root modulo the positive integer <var class="Arg">m</var>, and <code class="keyw">false</code> otherwise. If <var class="Arg">r</var> is less than 0 or larger than <var class="Arg">m</var> it is replaced by its remainder.</p>
293
294
295 <div class="example"><pre>
296 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimitiveRootMod( 2, 541 );</span>
297 true
298 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimitiveRootMod( -539, 541 ); # same computation as above;</span>
299 true
300 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsPrimitiveRootMod( 4, 541 );</span>
301 false
302 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ForAny( [1..29], r -&gt; IsPrimitiveRootMod( r, 30 ) );</span>
303 false
304 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># there is no a primitive root modulo 30</span>
305 </pre></div>
306
307 <p><a id="X7F9069D77AC48054" name="X7F9069D77AC48054"></a></p>
308
309 <h4>15.4 <span class="Heading">Roots Modulo Integers</span></h4>
310
311 <p><a id="X83449DBC80495971" name="X83449DBC80495971"></a></p>
312
313 <h5>15.4-1 Jacobi</h5>
314
315 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Jacobi</code>( <var class="Arg">n</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
316 <p><code class="func">Jacobi</code> returns the value of the <em>Kronecker-Jacobi symbol</em> <span class="SimpleMath">J(<var class="Arg">n</var>,<var class="Arg">m</var>)</span> of the integer <var class="Arg">n</var> modulo the integer <var class="Arg">m</var>. It is defined as follows:</p>
317
318 <p>If <span class="SimpleMath">n</span> and <span class="SimpleMath">m</span> are not coprime then <span class="SimpleMath">J(n,m) = 0</span>. Furthermore, <span class="SimpleMath">J(n,1) = 1</span> and <span class="SimpleMath">J(n,-1) = -1</span> if <span class="SimpleMath">m &lt; 0</span> and <span class="SimpleMath">+1</span> otherwise. And for odd <span class="SimpleMath">n</span> it is <span class="SimpleMath">J(n,2) = (-1)^k</span> with <span class="SimpleMath">k = (n^2-1)/8</span>. For odd primes <span class="SimpleMath">m</span> which are coprime to <span class="SimpleMath">n</span> the Kronecker-Jacobi symbol has the same value as the Legendre symbol (see <code class="func">Legendre</code> (<a href="chap15.html#X81464ABF7F10E544"><span class="RefLink">15.4-2</span></a>)).</p>
319
320 <p>For the general case suppose that <span class="SimpleMath">m = p_1 ⋅ p_2 ⋯ p_k</span> is a product of <span class="SimpleMath">-1</span> and of primes, not necessarily distinct, and that <span class="SimpleMath">n</span> is coprime to <span class="SimpleMath">m</span>. Then <span class="SimpleMath">J(n,m) = J(n,p_1) ⋅ J(n,p_2) ⋯ J(n,p_k)</span>.</p>
321
322 <p>Note that the Kronecker-Jacobi symbol coincides with the Jacobi symbol that is defined for odd <span class="SimpleMath">m</span> in many number theory books. For odd primes <span class="SimpleMath">m</span> and <span class="SimpleMath">n</span> coprime to <span class="SimpleMath">m</span> it coincides with the Legendre symbol.</p>
323
324 <p><code class="func">Jacobi</code> is very efficient, even for large values of <var class="Arg">n</var> and <var class="Arg">m</var>, it is about as fast as the Euclidean algorithm (see <code class="func">Gcd</code> (<a href="chap56.html#X7DE207718456F98F"><span class="RefLink">56.7-1</span></a>)).</p>
325
326
327 <div class="example"><pre>
328 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Jacobi( 11, 35 ); # 9^2 = 11 mod 35</span>
329 1
330 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># this is -1, thus there is no r such that r^2 = 6 mod 35</span>
331 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Jacobi( 6, 35 );</span>
332 -1
333 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># this is 1 even though there is no r with r^2 = 3 mod 35</span>
334 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Jacobi( 3, 35 );</span>
335 1
336 </pre></div>
337
338 <p><a id="X81464ABF7F10E544" name="X81464ABF7F10E544"></a></p>
339
340 <h5>15.4-2 Legendre</h5>
341
342 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Legendre</code>( <var class="Arg">n</var>, <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
343 <p><code class="func">Legendre</code> returns the value of the <em>Legendre symbol</em> of the integer <var class="Arg">n</var> modulo the positive integer <var class="Arg">m</var>.</p>
344
345 <p>The value of the Legendre symbol <span class="SimpleMath">L(n/m)</span> is 1 if <span class="SimpleMath">n</span> is a <em>quadratic residue</em> modulo <span class="SimpleMath">m</span>, i.e., if there exists an integer <span class="SimpleMath">r</span> such that <span class="SimpleMath">r^2 ≡ n mod m</span> and <span class="SimpleMath">-1</span> otherwise.</p>
346
347 <p>If a root of <var class="Arg">n</var> exists it can be found by <code class="func">RootMod</code> (<a href="chap15.html#X83E3ED577B7A04ED"><span class="RefLink">15.4-3</span></a>).</p>
348
349 <p>While the value of the Legendre symbol usually is only defined for <var class="Arg">m</var> a prime, we have extended the definition to include composite moduli too. The Jacobi symbol (see <code class="func">Jacobi</code> (<a href="chap15.html#X83449DBC80495971"><span class="RefLink">15.4-1</span></a>)) is another generalization of the Legendre symbol for composite moduli that is much cheaper to compute, because it does not need the factorization of <var class="Arg">m</var> (see <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>)).</p>
350
351 <p>A description of the Jacobi symbol, the Legendre symbol, and related topics can be found in <a href="chapBib.html#biBBaker84">[Bak84]</a>.</p>
352
353
354 <div class="example"><pre>
355 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Legendre( 5, 11 ); # 4^2 = 5 mod 11</span>
356 1
357 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># this is -1, thus there is no r such that r^2 = 6 mod 11</span>
358 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Legendre( 6, 11 );</span>
359 -1
360 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># this is -1, thus there is no r such that r^2 = 3 mod 35</span>
361 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Legendre( 3, 35 );</span>
362 -1
363 </pre></div>
364
365 <p><a id="X83E3ED577B7A04ED" name="X83E3ED577B7A04ED"></a></p>
366
367 <h5>15.4-3 RootMod</h5>
368
369 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RootMod</code>( <var class="Arg">n</var>[, <var class="Arg">k</var>], <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
370 <p><code class="func">RootMod</code> computes a <var class="Arg">k</var>th root of the integer <var class="Arg">n</var> modulo the positive integer <var class="Arg">m</var>, i.e., a <span class="SimpleMath">r</span> such that <span class="SimpleMath">r^<var class="Arg">k</var> ≡ <var class="Arg">n</var> mod <var class="Arg">m</var></span>. If no such root exists <code class="func">RootMod</code> returns <code class="keyw">fail</code>. If only the arguments <var class="Arg">n</var> and <var class="Arg">m</var> are given, the default value for <var class="Arg">k</var> is <span class="SimpleMath">2</span>.</p>
371
372 <p>A square root of <var class="Arg">n</var> exists only if <code class="code">Legendre(<var class="Arg">n</var>,<var class="Arg">m</var>) = 1</code> (see <code class="func">Legendre</code> (<a href="chap15.html#X81464ABF7F10E544"><span class="RefLink">15.4-2</span></a>)). If <var class="Arg">m</var> has <span class="SimpleMath">r</span> different prime factors then there are <span class="SimpleMath">2^r</span> different roots of <var class="Arg">n</var> mod <var class="Arg">m</var>. It is unspecified which one <code class="func">RootMod</code> returns. You can, however, use <code class="func">RootsMod</code> (<a href="chap15.html#X84D3F03B862841F8"><span class="RefLink">15.4-4</span></a>) to compute the full set of roots.</p>
373
374 <p><code class="func">RootMod</code> is efficient even for large values of <var class="Arg">m</var>, in fact the most time is usually spent factoring <var class="Arg">m</var> (see <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>)).</p>
375
376
377 <div class="example"><pre>
378 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># note 'RootMod' does not return 8 in this case but -8:</span>
379 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootMod( 64, 1009 );</span>
380 1001
381 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootMod( 64, 3, 1009 );</span>
382 518
383 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootMod( 64, 5, 1009 );</span>
384 656
385 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List( RootMod( 64, 1009 ) * RootsUnityMod( 1009 ),</span>
386 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> x -&gt; x mod 1009 ); # set of all square roots of 64 mod 1009</span>
387 [ 1001, 8 ]
388 </pre></div>
389
390 <p><a id="X84D3F03B862841F8" name="X84D3F03B862841F8"></a></p>
391
392 <h5>15.4-4 RootsMod</h5>
393
394 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RootsMod</code>( <var class="Arg">n</var>[, <var class="Arg">k</var>], <var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
395 <p><code class="func">RootsMod</code> computes the set of <var class="Arg">k</var>th roots of the integer <var class="Arg">n</var> modulo the positive integer <var class="Arg">m</var>, i.e., the list of all <span class="SimpleMath">r</span> such that <span class="SimpleMath">r^<var class="Arg">k</var> ≡ <var class="Arg">n</var> mod <var class="Arg">m</var></span>. If only the arguments <var class="Arg">n</var> and <var class="Arg">m</var> are given, the default value for <var class="Arg">k</var> is <span class="SimpleMath">2</span>.</p>
396
397
398 <div class="example"><pre>
399 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootsMod( 1, 7*31 ); # the same as `RootsUnityMod( 7*31 )'</span>
400 [ 1, 92, 125, 216 ]
401 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootsMod( 7, 7*31 );</span>
402 [ 21, 196 ]
403 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootsMod( 5, 7*31 );</span>
404 [ ]
405 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootsMod( 1, 5, 7*31 );</span>
406 [ 1, 8, 64, 78, 190 ]
407 </pre></div>
408
409 <p><a id="X81F856E682A8ECBA" name="X81F856E682A8ECBA"></a></p>
410
411 <h5>15.4-5 RootsUnityMod</h5>
412
413 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RootsUnityMod</code>( [<var class="Arg">k</var>, ]<var class="Arg">m</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
414 <p><code class="func">RootsUnityMod</code> returns the set of <var class="Arg">k</var>-th roots of unity modulo the positive integer <var class="Arg">m</var>, i.e., the list of all solutions <span class="SimpleMath">r</span> of <span class="SimpleMath">r^<var class="Arg">k</var> ≡ <var class="Arg">n</var> mod <var class="Arg">m</var></span>. If only the argument <var class="Arg">m</var> is given, the default value for <var class="Arg">k</var> is <span class="SimpleMath">2</span>.</p>
415
416 <p>In general there are <span class="SimpleMath"><var class="Arg">k</var>^n</span> such roots if the modulus <var class="Arg">m</var> has <span class="SimpleMath">n</span> different prime factors <span class="SimpleMath">p</span> such that <span class="SimpleMath">p ≡ 1 mod <var class="Arg">k</var></span>. If <span class="SimpleMath"><var class="Arg">k</var>^2</span> divides <var class="Arg">m</var> then there are <span class="SimpleMath"><var class="Arg">k</var>^{n+1}</span> such roots; and especially if <span class="SimpleMath"><var class="Arg">k</var> = 2</span> and 8 divides <var class="Arg">m</var> there are <span class="SimpleMath">2^{n+2}</span> such roots.</p>
417
418 <p>In the current implementation <var class="Arg">k</var> must be a prime.</p>
419
420
421 <div class="example"><pre>
422 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootsUnityMod( 7*31 ); RootsUnityMod( 3, 7*31 );</span>
423 [ 1, 92, 125, 216 ]
424 [ 1, 25, 32, 36, 67, 149, 156, 191, 211 ]
425 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RootsUnityMod( 5, 7*31 );</span>
426 [ 1, 8, 64, 78, 190 ]
427 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">List( RootMod( 64, 1009 ) * RootsUnityMod( 1009 ),</span>
428 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> x -&gt; x mod 1009 ); # set of all square roots of 64 mod 1009</span>
429 [ 1001, 8 ]
430 </pre></div>
431
432 <p><a id="X7B3A5A0378A32F83" name="X7B3A5A0378A32F83"></a></p>
433
434 <h4>15.5 <span class="Heading">Multiplicative Arithmetic Functions</span></h4>
435
436 <p><a id="X823707DF821E79A0" name="X823707DF821E79A0"></a></p>
437
438 <h5>15.5-1 Sigma</h5>
439
440 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Sigma</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
441 <p><code class="func">Sigma</code> returns the sum of the positive divisors of the nonzero integer <var class="Arg">n</var>.</p>
442
443 <p><code class="func">Sigma</code> is a multiplicative arithmetic function, i.e., if <span class="SimpleMath">n</span> and <span class="SimpleMath">m</span> are relatively prime we have that <span class="SimpleMath">σ(n ⋅ m) = σ(n) σ(m)</span>.</p>
444
445 <p>Together with the formula <span class="SimpleMath">σ(p^k) = (p^{k+1}-1) / (p-1)</span> this allows us to compute <span class="SimpleMath">σ(<var class="Arg">n</var>)</span>.</p>
446
447 <p>Integers <var class="Arg">n</var> for which <span class="SimpleMath">σ(<var class="Arg">n</var>) = 2 <var class="Arg">n</var></span> are called perfect. Even perfect integers are exactly of the form <span class="SimpleMath">2^{<var class="Arg">n</var>-1}(2^<var class="Arg">n</var>-1)</span> where <span class="SimpleMath">2^<var class="Arg">n</var>-1</span> is prime. Primes of the form <span class="SimpleMath">2^<var class="Arg">n</var>-1</span> are called <em>Mersenne primes</em>, and 42 among the known Mersenne primes are obtained for <var class="Arg">n</var> <span class="SimpleMath">=</span> 2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583 and 25964951. Please find more up to date information about Mersenne primes at <span class="URL"><a href="https://www.mersenne.org">https://www.mersenne.org</a></span>. It is not known whether odd perfect integers exist, however <a href="chapBib.html#biBBC89">[BC89]</a> show that any such integer must have at least 300 decimal digits.</p>
448
449 <p><code class="func">Sigma</code> usually spends most of its time factoring <var class="Arg">n</var> (see <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>)).</p>
450
451
452 <div class="example"><pre>
453 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sigma( 1 );</span>
454 1
455 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sigma( 1009 ); # 1009 is a prime</span>
456 1010
457 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Sigma( 8128 ) = 2*8128; # 8128 is a perfect number</span>
458 true
459 </pre></div>
460
461 <p><a id="X798C62847EE0372E" name="X798C62847EE0372E"></a></p>
462
463 <h5>15.5-2 Tau</h5>
464
465 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Tau</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
466 <p><code class="func">Tau</code> returns the number of the positive divisors of the nonzero integer <var class="Arg">n</var>.</p>
467
468 <p><code class="func">Tau</code> is a multiplicative arithmetic function, i.e., if <span class="SimpleMath">n</span> and <span class="SimpleMath">m</span> are relative prime we have <span class="SimpleMath">τ(n ⋅ m) = τ(n) τ(m)</span>. Together with the formula <span class="SimpleMath">τ(p^k) = k+1</span> this allows us to compute <span class="SimpleMath">τ(<var class="Arg">n</var>)</span>.</p>
469
470 <p><code class="func">Tau</code> usually spends most of its time factoring <var class="Arg">n</var> (see <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>)).</p>
471
472
473 <div class="example"><pre>
474 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Tau( 1 );</span>
475 1
476 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Tau( 1013 ); # thus 1013 is a prime</span>
477 2
478 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Tau( 8128 );</span>
479 14
480 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># result is odd if and only if argument is a perfect square:</span>
481 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Tau( 36 );</span>
482 9
483 </pre></div>
484
485 <p><a id="X79C1DA36827C2959" name="X79C1DA36827C2959"></a></p>
486
487 <h5>15.5-3 MoebiusMu</h5>
488
489 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MoebiusMu</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
490 <p><code class="func">MoebiusMu</code> computes the value of Moebius inversion function for the nonzero integer <var class="Arg">n</var>. This is 0 for integers which are not squarefree, i.e., which are divided by a square <span class="SimpleMath">r^2</span>. Otherwise it is 1 if <var class="Arg">n</var> has a even number and <span class="SimpleMath">-1</span> if <var class="Arg">n</var> has an odd number of prime factors.</p>
491
492 <p>The importance of <span class="SimpleMath">μ</span> stems from the so called inversion formula. Suppose <span class="SimpleMath">f</span> is a multiplicative arithmetic function defined on the positive integers and let <span class="SimpleMath">g(n) = ∑_{d ∣ n} f(d)</span>. Then <span class="SimpleMath">f(n) = ∑_{d ∣ n} μ(d) g(n/d)</span>. As a special case we have <span class="SimpleMath">ϕ(n) = ∑_{d ∣ n} μ(d) n/d</span> since <span class="SimpleMath">n = ∑_{d ∣ n} ϕ(d)</span> (see <code class="func">Phi</code> (<a href="chap15.html#X85A0C67982D9057A"><span class="RefLink">15.2-2</span></a>)).</p>
493
494 <p><code class="func">MoebiusMu</code> usually spends all of its time factoring <var class="Arg">n</var> (see <code class="func">FactorsInt</code> (<a href="chap14.html#X82C989DB84744B36"><span class="RefLink">14.4-7</span></a>)).</p>
495
496
497 <div class="example"><pre>
498 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">MoebiusMu( 60 ); MoebiusMu( 61 ); MoebiusMu( 62 );</span>
499 0
500 -1
501 1
502 </pre></div>
503
504 <p><a id="X7B2E061C835159B9" name="X7B2E061C835159B9"></a></p>
505
506 <h4>15.6 <span class="Heading">Continued Fractions</span></h4>
507
508 <p><a id="X874C161B83416092" name="X874C161B83416092"></a></p>
509
510 <h5>15.6-1 ContinuedFractionExpansionOfRoot</h5>
511
512 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ContinuedFractionExpansionOfRoot</code>( <var class="Arg">f</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
513 <p>The first <var class="Arg">n</var> terms of the continued fraction expansion of the only positive real root of the polynomial <var class="Arg">f</var> with integer coefficients. The leading coefficient of <var class="Arg">f</var> must be positive and the value of <var class="Arg">f</var> at 0 must be negative. If the degree of <var class="Arg">f</var> is 2 and <var class="Arg">n</var> = 0, the function computes one period of the continued fraction expansion of the root in question. Anything may happen if <var class="Arg">f</var> has three or more positive real roots.</p>
514
515
516 <div class="example"><pre>
517 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">x := Indeterminate(Integers);;</span>
518 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContinuedFractionExpansionOfRoot(x^2-7,20);</span>
519 [ 2, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1, 4, 1, 1, 1 ]
520 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContinuedFractionExpansionOfRoot(x^2-7,0);</span>
521 [ 2, 1, 1, 1, 4 ]
522 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContinuedFractionExpansionOfRoot(x^3-2,20);</span>
523 [ 1, 3, 1, 5, 1, 1, 4, 1, 1, 8, 1, 14, 1, 10, 2, 1, 4, 12, 2, 3 ]
524 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContinuedFractionExpansionOfRoot(x^5-x-1,50);</span>
525 [ 1, 5, 1, 42, 1, 3, 24, 2, 2, 1, 16, 1, 11, 1, 1, 2, 31, 1, 12, 5,
526 1, 7, 11, 1, 4, 1, 4, 2, 2, 3, 4, 2, 1, 1, 11, 1, 41, 12, 1, 8, 1,
527 1, 1, 1, 1, 9, 2, 1, 5, 4 ]
528 </pre></div>
529
530 <p><a id="X8059667580A039A6" name="X8059667580A039A6"></a></p>
531
532 <h5>15.6-2 ContinuedFractionApproximationOfRoot</h5>
533
534 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ContinuedFractionApproximationOfRoot</code>( <var class="Arg">f</var>, <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
535 <p>The <var class="Arg">n</var>th continued fraction approximation of the only positive real root of the polynomial <var class="Arg">f</var> with integer coefficients. The leading coefficient of <var class="Arg">f</var> must be positive and the value of <var class="Arg">f</var> at 0 must be negative. Anything may happen if <var class="Arg">f</var> has three or more positive real roots.</p>
536
537
538 <div class="example"><pre>
539 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContinuedFractionApproximationOfRoot(x^2-2,10);</span>
540 3363/2378
541 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">3363^2-2*2378^2;</span>
542 1
543 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">z := ContinuedFractionApproximationOfRoot(x^5-x-1,20);</span>
544 499898783527/428250732317
545 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">z^5-z-1;</span>
546 486192462527432755459620441970617283/
547 14404247382319842421697357558805709031116987826242631261357
548 </pre></div>
549
550 <p><a id="X7C5563A37D566DA5" name="X7C5563A37D566DA5"></a></p>
551
552 <h4>15.7 <span class="Heading">Miscellaneous</span></h4>
553
554 <p><a id="X8243EAA586D78ED4" name="X8243EAA586D78ED4"></a></p>
555
556 <h5>15.7-1 PValuation</h5>
557
558 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PValuation</code>( <var class="Arg">n</var>, <var class="Arg">p</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
559 <p>For an integer <var class="Arg">n</var> and a prime <var class="Arg">p</var> this function returns the <var class="Arg">p</var>-valuation of <var class="Arg">n</var>, that is the exponent <span class="SimpleMath">e</span> such that <span class="SimpleMath">p^e</span> is the largest power of <var class="Arg">p</var> that divides <var class="Arg">n</var>. The valuation of zero is infinity.</p>
560
561
562 <div class="example"><pre>
563 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PValuation(100,2);</span>
564 2
565 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">PValuation(100,3);</span>
566 0
567 </pre></div>
568
569 <p><a id="X85E1EFC484F648A4" name="X85E1EFC484F648A4"></a></p>
570
571 <h5>15.7-2 TwoSquares</h5>
572
573 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TwoSquares</code>( <var class="Arg">n</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
574 <p><code class="func">TwoSquares</code> returns a list of two integers <span class="SimpleMath">x ≤ y</span> such that the sum of the squares of <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> is equal to the nonnegative integer <var class="Arg">n</var>, i.e., <span class="SimpleMath">n = x^2 + y^2</span>. If no such representation exists <code class="func">TwoSquares</code> will return <code class="keyw">fail</code>. <code class="func">TwoSquares</code> will return a representation for which the gcd of <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> is as small as possible. It is not specified which representation <code class="func">TwoSquares</code> returns if there is more than one.</p>
575
576 <p>Let <span class="SimpleMath">a</span> be the product of all maximal powers of primes of the form <span class="SimpleMath">4k+3</span> dividing <var class="Arg">n</var>. A representation of <var class="Arg">n</var> as a sum of two squares exists if and only if <span class="SimpleMath">a</span> is a perfect square. Let <span class="SimpleMath">b</span> be the maximal power of <span class="SimpleMath">2</span> dividing <var class="Arg">n</var> or its half, whichever is a perfect square. Then the minimal possible gcd of <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span> is the square root <span class="SimpleMath">c</span> of <span class="SimpleMath">a ⋅ b</span>. The number of different minimal representation with <span class="SimpleMath">x ≤ y</span> is <span class="SimpleMath">2^{l-1}</span>, where <span class="SimpleMath">l</span> is the number of different prime factors of the form <span class="SimpleMath">4k+1</span> of <var class="Arg">n</var>.</p>
577
578 <p>The algorithm first finds a square root <span class="SimpleMath">r</span> of <span class="SimpleMath">-1</span> modulo <span class="SimpleMath"><var class="Arg">n</var> / (a ⋅ b)</span>, which must exist, and applies the Euclidean algorithm to <span class="SimpleMath">r</span> and <var class="Arg">n</var>. The first residues in the sequence that are smaller than <span class="SimpleMath">sqrt{<var class="Arg">n</var>/(a ⋅ b)}</span> times <span class="SimpleMath">c</span> are a possible pair <span class="SimpleMath">x</span> and <span class="SimpleMath">y</span>.</p>
579
580 <p>Better descriptions of the algorithm and related topics can be found in <a href="chapBib.html#biBWagon90">[Wag90]</a> and <a href="chapBib.html#biBZagier90">[Zag90]</a>.</p>
581
582
583 <div class="example"><pre>
584 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 5 );</span>
585 [ 1, 2 ]
586 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 11 ); # there is no representation</span>
587 fail
588 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 16 );</span>
589 [ 0, 4 ]
590 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># 3 is the minimal possible gcd because 9 divides 45:</span>
591 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 45 );</span>
592 [ 3, 6 ]
593 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># it is not [5,10] because their gcd is not minimal:</span>
594 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 125 );</span>
595 [ 2, 11 ]
596 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># [10,11] would be the other possible representation:</span>
597 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 13*17 );</span>
598 [ 5, 14 ]
599 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TwoSquares( 848654483879497562821 ); # argument is prime</span>
600 [ 6305894639, 28440994650 ]
601 </pre></div>
602
603
604 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap14.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap16.html">[Next Chapter]</a>&nbsp; </div>
605
606
607 <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>
608
609 <hr />
610 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
611 </body>
612 </html>