Compare commits

...

3 Commits

Author SHA1 Message Date
filifa
e57a1c0335 add ability to sieve custom function 2026-05-04 22:25:30 -04:00
filifa
14b640b2e3 set margin on figures 2026-04-15 20:39:04 -04:00
filifa
8a871e935b add missing tag 2026-04-15 20:26:19 -04:00
3 changed files with 29 additions and 2 deletions

View File

@@ -20,6 +20,7 @@
<hgroup> <hgroup>
<h1>Sieving Multiplicative Functions</h1> <h1>Sieving Multiplicative Functions</h1>
<p>Posted <time datetime="2026-04-15">April 15, 2026</time></p> <p>Posted <time datetime="2026-04-15">April 15, 2026</time></p>
<p>Updated <time datetime="2026-05-04">May 4, 2026</time></p>
</hgroup> </hgroup>
<p>The <p>The
@@ -29,7 +30,7 @@
particular, a segmented version can be implemented to limit memory particular, a segmented version can be implemented to limit memory
usage.</p> usage.</p>
Furthermore, a similar approach can be used to "sieve" the values of <p>Furthermore, a similar approach can be used to "sieve" the values of
<a href="https://en.wikipedia.org/wiki/Multiplicative_function">multiplicative functions</a>. <a href="https://en.wikipedia.org/wiki/Multiplicative_function">multiplicative functions</a>.
For instance, For instance,
<a href="https://cp-algorithms.com/algebra/phi-function.html">this page</a> <a href="https://cp-algorithms.com/algebra/phi-function.html">this page</a>
@@ -76,8 +77,15 @@
<option value="mobius">Mobius</option> <option value="mobius">Mobius</option>
<option value="big-omega">Prime omega (with multiplicity)</option> <option value="big-omega">Prime omega (with multiplicity)</option>
<option value="little-omega">Prime omega (without multiplicity)</option> <option value="little-omega">Prime omega (without multiplicity)</option>
<option value="custom">Custom</option>
</select> </select>
<label hidden id="custom-function-label" for="custom-function">Custom function definition</label>
<textarea hidden spellcheck=false placeholder="return Math.pow(p, k-1) * (p-1);" id="custom-function"></textarea>
<label hidden id="custom-function-additive-label" for="custom-function-additive">Function is additive</label>
<input hidden type="checkbox" id="custom-function-additive" />
<input type="button" id="enter" value="Enter" /> <input type="button" id="enter" value="Enter" />
</div> </div>
@@ -1125,7 +1133,8 @@ function mobius(p, k) {
<p>Many multiplicative and additive functions have simple formulas <p>Many multiplicative and additive functions have simple formulas
for prime powers, which makes adding a new function to sieve very for prime powers, which makes adding a new function to sieve very
easy with this approach.</p> easy with this approach. The demo even lets you write your own custom
function to sieve - try it out!</p>
</section> </section>
<section> <section>

View File

@@ -133,6 +133,14 @@ function sieveMultiples(p, q, l, r, y, sieve, quo, additive) {
} }
} }
function setCustomFunctionVisibility(e) {
const isHidden = e.target.value !== "custom";
document.querySelector("#custom-function-label").hidden = isHidden;
document.querySelector("#custom-function").hidden = isHidden;
document.querySelector("#custom-function-additive-label").hidden = isHidden;
document.querySelector("#custom-function-additive").hidden = isHidden;
}
function getFunc(func) { function getFunc(func) {
switch (func.value) { switch (func.value) {
case "num-divisors": case "num-divisors":
@@ -149,6 +157,11 @@ function getFunc(func) {
return [littleOmega, true]; return [littleOmega, true];
case "big-omega": case "big-omega":
return [bigOmega, true]; return [bigOmega, true];
case "custom":
const def = document.querySelector("#custom-function");
const isAdditive = document.querySelector("#custom-function-additive");
const f = new Function("p", "k", def.value);
return [f, isAdditive.checked];
default: default:
throw new Error("function not implemented!"); throw new Error("function not implemented!");
} }
@@ -219,4 +232,5 @@ function calculate() {
result.appendChild(table); result.appendChild(table);
} }
document.querySelector("#function").addEventListener("change", (e) => setCustomFunctionVisibility(e))
document.querySelector("#enter").addEventListener("click", () => calculate()) document.querySelector("#enter").addEventListener("click", () => calculate())

View File

@@ -3,6 +3,10 @@
overflow-y: hidden; overflow-y: hidden;
} }
figure {
margin: 1vw;
}
pre { pre {
overflow: auto; overflow: auto;
background-color: black; background-color: black;