don't mod values when pushed

This commit is contained in:
filifa 2025-12-11 23:49:34 -05:00
parent 8a75177060
commit f8324b8a30
1 changed files with 13 additions and 5 deletions

18
main.js
View File

@ -70,21 +70,29 @@ function compute(queue, modulus) {
const stack = []; const stack = [];
for (const token of queue) { for (const token of queue) {
if (typeof token === "bigint") { if (typeof token === "bigint") {
stack.push(token % modulus); stack.push(token);
} else if (token === "+") { } else if (token === "+") {
const [a, b] = binaryOpPop(stack); let [a, b] = binaryOpPop(stack);
a %= modulus;
b %= modulus;
const c = (a + b) % modulus; const c = (a + b) % modulus;
stack.push(c); stack.push(c);
} else if (token === "-") { } else if (token === "-") {
const [a, b] = binaryOpPop(stack); let [a, b] = binaryOpPop(stack);
a %= modulus;
b %= modulus;
const c = (a - b) % modulus; const c = (a - b) % modulus;
stack.push(c); stack.push(c);
} else if (token === "*") { } else if (token === "*") {
const [a, b] = binaryOpPop(stack); let [a, b] = binaryOpPop(stack);
a %= modulus;
b %= modulus;
const c = (a * b) % modulus; const c = (a * b) % modulus;
stack.push(c); stack.push(c);
} else if (token === "/") { } else if (token === "/") {
const [a, b] = binaryOpPop(stack); let [a, b] = binaryOpPop(stack);
a %= modulus;
b %= modulus;
const binv = modinv(b, modulus); const binv = modinv(b, modulus);
const c = (a * binv) % modulus; const c = (a * binv) % modulus;
stack.push(c); stack.push(c);