diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/assests/background.jpg b/Calculators/Chinese-Remainder-Theorem-Calculator/assests/background.jpg new file mode 100644 index 000000000..c425cb0d7 Binary files /dev/null and b/Calculators/Chinese-Remainder-Theorem-Calculator/assests/background.jpg differ diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/index.html b/Calculators/Chinese-Remainder-Theorem-Calculator/index.html new file mode 100644 index 000000000..c9ea57894 --- /dev/null +++ b/Calculators/Chinese-Remainder-Theorem-Calculator/index.html @@ -0,0 +1,27 @@ + + + + + + Chinese Remainder Theorem Calculator + + + +
+

Chinese Remainder Theorem Calculator

+

Add equations you want to solve:

+
+
+ + mod + + +
+
+ + +
+
+ + + diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/script.js b/Calculators/Chinese-Remainder-Theorem-Calculator/script.js new file mode 100644 index 000000000..731ba6441 --- /dev/null +++ b/Calculators/Chinese-Remainder-Theorem-Calculator/script.js @@ -0,0 +1,77 @@ +function addEquation() { + const inputsDiv = document.getElementById("inputs"); + const newEquation = document.createElement("div"); + newEquation.className = "equation"; + newEquation.innerHTML = ` + + mod + + + `; + inputsDiv.appendChild(newEquation); + } + + function removeEquation(button) { + button.parentElement.remove(); + } + + function solve() { + const remainders = Array.from(document.querySelectorAll(".remainder")).map(input => parseInt(input.value)); + const modulos = Array.from(document.querySelectorAll(".modulo")).map(input => parseInt(input.value)); + + if (remainders.includes(NaN) || modulos.includes(NaN) || modulos.includes(0)) { + alert("Please fill all fields correctly. Modulo cannot be 0."); + return; + } + + if (!areCoprime(modulos)) { + document.getElementById("output").innerHTML = "The modulos are not pairwise coprime. Please use coprime values."; + return; + } + + const m = modulos.reduce((acc, curr) => acc * curr, 1); // Find m + const M = modulos.map(mod => m / mod); // Find Mi + const y = M.map((Mi, index) => modularInverse(Mi, modulos[index])); // Find yi + const x = remainders.reduce((sum, a, index) => sum + a * M[index] * y[index], 0) % m; + + // Generate explanation + let explanation = `

Using Chinese Remainder Theorem, solve the system of equations:

`; + explanation += `

${remainders.map((a, i) => `x ≡ ${a} (mod ${modulos[i]})`).join("
")}

`; + explanation += `

1. Verify moduli are pairwise coprime: ${modulos.map((m1, i) => + modulos.slice(i + 1).map(m2 => `gcd(${m1}, ${m2}) = ${gcd(m1, m2)}`).join(", ") + ).join(", ")}.

`; + explanation += `

2. Calculate M = ${modulos.join(" × ")} = ${m}

`; + explanation += `

3. Calculate Mi and modular inverses:

`; + M.forEach((Mi, index) => { + explanation += `

M${index + 1} = ${Mi}, inverse (mod ${modulos[index]}) = ${y[index]}

`; + }); + explanation += `

4. Calculate x using: x = Σ(ai * Mi * yi) mod M = ${x}

`; + explanation += `

Final Answer: x = ${x}

`; + + document.getElementById("output").innerHTML = explanation; + } + + function areCoprime(numbers) { + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (gcd(numbers[i], numbers[j]) !== 1) return false; + } + } + return true; + } + + function gcd(a, b) { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return a; + } + + function modularInverse(a, m) { + a = a % m; + for (let x = 1; x < m; x++) { + if ((a * x) % m === 1) return x; + } + return null; + } + \ No newline at end of file diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/style.css b/Calculators/Chinese-Remainder-Theorem-Calculator/style.css new file mode 100644 index 000000000..38e23b21b --- /dev/null +++ b/Calculators/Chinese-Remainder-Theorem-Calculator/style.css @@ -0,0 +1,76 @@ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; + background: url(./assests/background.jpg); + background-size: cover; + color: white; + + } + + .container { + max-width: 800px; + margin: 50px auto; + background: rgba(0, 0, 0, 0.8); + padding: 20px; + border-radius: 10px; + box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5); + } + + h1 { + text-align: center; + } + + p { + text-align: center; + } + + #inputs .equation { + display: flex; + align-items: center; + justify-content: center; + margin-bottom: 10px; + } + + #inputs .equation input { + margin: 0 5px; + padding: 5px; + width: 80px; + border-radius: 5px; + border: 1px solid #ccc; + } + + #inputs .equation span { + font-size: 16px; + margin: 0 5px; + } + + button { + cursor: pointer; + padding: 8px 15px; + font-size: 14px; + border-radius: 5px; + border: none; + } + + .add-btn, + .solve-btn { + display: block; + margin: 20px auto; + background: #6200ea; + color: white; + } + + .remove-btn { + background: red; + color: white; + } + + .output { + margin-top: 20px; + padding: 10px; + background: lightblue; + color: black; + border-radius: 5px; + } + \ No newline at end of file