Skip to content

Commit 0e77982

Browse files
1 parent e6d07df commit 0e77982

File tree

7 files changed

+182
-182
lines changed

7 files changed

+182
-182
lines changed

1570/algebra/phi-function.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8263,7 +8263,7 @@
82638263
<ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
82648264

82658265
Last update:
8266-
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="December 5, 2025 05:44:03 UTC">December 5, 2025</span>&emsp;
8266+
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="December 6, 2025 18:54:47 UTC">December 6, 2025</span>&emsp;
82678267

82688268
<!-- Tags -->
82698269

@@ -8384,8 +8384,14 @@ <h3 id="finding-the-totient-from-1-to-n-using-the-divisor-sum-property">Finding
83848384
<span class="p">}</span>
83858385
</code></pre></div>
83868386
<h3 id="finding-the-totient-from-l-to-r-using-the-segmented-sieve">Finding the totient from <span class="arithmatex">$L$</span> to <span class="arithmatex">$R$</span> using the <a href="sieve-of-eratosthenes.html#segmented-sieve">segmented sieve</a><a class="headerlink" href="#finding-the-totient-from-l-to-r-using-the-segmented-sieve" title="Permanent link">&para;</a></h3>
8387-
<p>If we need the totient of all numbers between <span class="arithmatex">$L$</span> and <span class="arithmatex">$R$</span>, we can use the <a href="sieve-of-eratosthenes.html#segmented-sieve">segmented sieve</a> approach.
8388-
This implementation is based on the divisor sum property, but uses the <a href="sieve-of-eratosthenes.html#segmented-sieve">segmented sieve</a> approach to compute the totient of all numbers between <span class="arithmatex">$L$</span> and <span class="arithmatex">$R$</span> in <span class="arithmatex">$O((R - L + 1) \log \log R)$</span>.</p>
8387+
<p>If we need the totient of all numbers between <span class="arithmatex">$L$</span> and <span class="arithmatex">$R$</span>, we can use the <a href="sieve-of-eratosthenes.html#segmented-sieve">segmented sieve</a> approach.</p>
8388+
<p>The algorithm works by first precomputing all primes up to <span class="arithmatex">$\sqrt{R}$</span> using a <a href="prime-sieve-linear.html">linear sieve</a> in <span class="arithmatex">$O(\sqrt{R})$</span> time and space. Then, for each number in the range <span class="arithmatex">$[L, R]$</span>, we apply the standard φ formula <span class="arithmatex">$\phi(n) = n \cdot \prod_{p | n} \left(1 - \frac{1}{p}\right)$</span> by iterating over the precomputed primes. We maintain a <code>rem</code> array to track the remaining unfactored part of each number; if <code>rem[i] &gt; 1</code> after processing all small primes, then the number has a large prime factor greater than <span class="arithmatex">$\sqrt{R}$</span>, which we handle in a final pass.</p>
8389+
<p><strong>Complexity:</strong></p>
8390+
<ul>
8391+
<li>Preprocessing: <span class="arithmatex">$O(\sqrt{R})$</span> time and space for the linear sieve</li>
8392+
<li>Query: <span class="arithmatex">$O((R - L + 1) \log \log R)$</span> for computing φ over the range</li>
8393+
</ul>
8394+
<p><strong>Usage:</strong> Call <code>primes = linear_sieve(sqrt(MAX_R) + 1)</code> once at startup. Then <code>phi[i - L]</code> gives <span class="arithmatex">$\phi(i)$</span> for each <span class="arithmatex">$i \in [L, R]$</span>.</p>
83898395
<div class="highlight"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">MAX_RANGE</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1e6</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="n">MAX_R</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1e14</span><span class="p">;</span>
83908396
<span class="n">vector</span><span class="o">&lt;</span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="o">&gt;</span><span class="w"> </span><span class="n">primes</span><span class="p">;</span>
83918397
<span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">phi</span><span class="p">[</span><span class="n">MAX_RANGE</span><span class="p">],</span><span class="w"> </span><span class="n">rem</span><span class="p">[</span><span class="n">MAX_RANGE</span><span class="p">];</span>
@@ -8406,12 +8412,6 @@ <h3 id="finding-the-totient-from-l-to-r-using-the-segmented-sieve">Finding the t
84068412
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">prime</span><span class="p">;</span>
84078413
<span class="p">}</span>
84088414

8409-
<span class="cm">/*</span>
8410-
<span class="cm"> * Find the totient of numbers from `L` to `R` with preprocess SQRT(MAX_R)</span>
8411-
<span class="cm"> * @note run linear_sieve(sqrt(MAX_R) + 1) at main</span>
8412-
<span class="cm"> * @note Complexity : O((R - L + 1) * log(log(R)) + sqrt(R))</span>
8413-
<span class="cm"> * @note phi(i) is phi[i - L] where i [L, R]</span>
8414-
<span class="cm">*/</span>
84158415
<span class="kt">void</span><span class="w"> </span><span class="n">segmented_phi</span><span class="p">(</span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">L</span><span class="p">,</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">R</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
84168416
<span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">L</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="n">R</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
84178417
<span class="w"> </span><span class="n">rem</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">L</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
@@ -8494,7 +8494,7 @@ <h2 id="practice-problems">Practice Problems<a class="headerlink" href="#practic
84948494

84958495
<ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
84968496
<span class="contributors-text">Contributors:</span>
8497-
<ul class="contributors" data-bi-name="contributors"><li><a href="https://github.com/jakobkogler" title="jakobkogler" data-bi-name="contributorprofile" target="_blank">jakobkogler</a> (31.27%)</li><li><a href="https://github.com/Elghrabawy" title="Elghrabawy" data-bi-name="contributorprofile" target="_blank">Elghrabawy</a> (19.31%)</li><li><a href="https://github.com/tanmay-sinha" title="tanmay-sinha" data-bi-name="contributorprofile" target="_blank">tanmay-sinha</a> (18.53%)</li><li><a href="https://github.com/samsidx" title="samsidx" data-bi-name="contributorprofile" target="_blank">samsidx</a> (9.27%)</li><li><a href="https://github.com/adamant-pwn" title="adamant-pwn" data-bi-name="contributorprofile" target="_blank">adamant-pwn</a> (5.79%)</li><li><a href="https://github.com/Morass" title="Morass" data-bi-name="contributorprofile" target="_blank">Morass</a> (4.63%)</li><li><a href="https://github.com/tcNickolas" title="tcNickolas" data-bi-name="contributorprofile" target="_blank">tcNickolas</a> (2.7%)</li><li><a href="https://github.com/jxu" title="jxu" data-bi-name="contributorprofile" target="_blank">jxu</a> (1.93%)</li><li><a href="https://github.com/RodionGork" title="RodionGork" data-bi-name="contributorprofile" target="_blank">RodionGork</a> (1.93%)</li><li><a href="https://github.com/wikku" title="wikku" data-bi-name="contributorprofile" target="_blank">wikku</a> (0.77%)</li><li><a href="https://github.com/algmyr" title="algmyr" data-bi-name="contributorprofile" target="_blank">algmyr</a> (0.77%)</li><li><a href="https://github.com/Harpreet287" title="Harpreet287" data-bi-name="contributorprofile" target="_blank">Harpreet287</a> (0.39%)</li><li><a href="https://github.com/Animmah" title="Animmah" data-bi-name="contributorprofile" target="_blank">Animmah</a> (0.39%)</li><li><a href="https://github.com/SiddharthEEE" title="SiddharthEEE" data-bi-name="contributorprofile" target="_blank">SiddharthEEE</a> (0.39%)</li><li><a href="https://github.com/iAngkur" title="iAngkur" data-bi-name="contributorprofile" target="_blank">iAngkur</a> (0.39%)</li><li><a href="https://github.com/lrvideckis" title="lrvideckis" data-bi-name="contributorprofile" target="_blank">lrvideckis</a> (0.39%)</li><li><a href="https://github.com/rkharsan" title="rkharsan" data-bi-name="contributorprofile" target="_blank">rkharsan</a> (0.39%)</li><li><a href="https://github.com/harshhx17" title="harshhx17" data-bi-name="contributorprofile" target="_blank">harshhx17</a> (0.39%)</li><li><a href="https://github.com/likecs" title="likecs" data-bi-name="contributorprofile" target="_blank">likecs</a> (0.39%)</li></ul>
8497+
<ul class="contributors" data-bi-name="contributors"><li><a href="https://github.com/jakobkogler" title="jakobkogler" data-bi-name="contributorprofile" target="_blank">jakobkogler</a> (30.92%)</li><li><a href="https://github.com/Elghrabawy" title="Elghrabawy" data-bi-name="contributorprofile" target="_blank">Elghrabawy</a> (20.23%)</li><li><a href="https://github.com/tanmay-sinha" title="tanmay-sinha" data-bi-name="contributorprofile" target="_blank">tanmay-sinha</a> (18.32%)</li><li><a href="https://github.com/samsidx" title="samsidx" data-bi-name="contributorprofile" target="_blank">samsidx</a> (9.16%)</li><li><a href="https://github.com/adamant-pwn" title="adamant-pwn" data-bi-name="contributorprofile" target="_blank">adamant-pwn</a> (5.73%)</li><li><a href="https://github.com/Morass" title="Morass" data-bi-name="contributorprofile" target="_blank">Morass</a> (4.58%)</li><li><a href="https://github.com/tcNickolas" title="tcNickolas" data-bi-name="contributorprofile" target="_blank">tcNickolas</a> (2.67%)</li><li><a href="https://github.com/jxu" title="jxu" data-bi-name="contributorprofile" target="_blank">jxu</a> (1.91%)</li><li><a href="https://github.com/RodionGork" title="RodionGork" data-bi-name="contributorprofile" target="_blank">RodionGork</a> (1.91%)</li><li><a href="https://github.com/wikku" title="wikku" data-bi-name="contributorprofile" target="_blank">wikku</a> (0.76%)</li><li><a href="https://github.com/algmyr" title="algmyr" data-bi-name="contributorprofile" target="_blank">algmyr</a> (0.76%)</li><li><a href="https://github.com/Harpreet287" title="Harpreet287" data-bi-name="contributorprofile" target="_blank">Harpreet287</a> (0.38%)</li><li><a href="https://github.com/Animmah" title="Animmah" data-bi-name="contributorprofile" target="_blank">Animmah</a> (0.38%)</li><li><a href="https://github.com/SiddharthEEE" title="SiddharthEEE" data-bi-name="contributorprofile" target="_blank">SiddharthEEE</a> (0.38%)</li><li><a href="https://github.com/iAngkur" title="iAngkur" data-bi-name="contributorprofile" target="_blank">iAngkur</a> (0.38%)</li><li><a href="https://github.com/lrvideckis" title="lrvideckis" data-bi-name="contributorprofile" target="_blank">lrvideckis</a> (0.38%)</li><li><a href="https://github.com/rkharsan" title="rkharsan" data-bi-name="contributorprofile" target="_blank">rkharsan</a> (0.38%)</li><li><a href="https://github.com/harshhx17" title="harshhx17" data-bi-name="contributorprofile" target="_blank">harshhx17</a> (0.38%)</li><li><a href="https://github.com/likecs" title="likecs" data-bi-name="contributorprofile" target="_blank">likecs</a> (0.38%)</li></ul>
84988498
</ul>
84998499

85008500
</article>

0 commit comments

Comments
 (0)