add ability to sieve custom function

This commit is contained in:
filifa
2026-05-04 22:25:30 -04:00
parent 14b640b2e3
commit e57a1c0335
2 changed files with 24 additions and 1 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
@@ -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())