diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/README.md b/Calculators/Chinese-Remainder-Theorem-Calculator/README.md new file mode 100644 index 000000000..63d05769e --- /dev/null +++ b/Calculators/Chinese-Remainder-Theorem-Calculator/README.md @@ -0,0 +1,15 @@ +#

Chinese Remainder Theorem Calculator

+ +## Description :- + +Calculator that solves systems of modular equations using the Chinese Remainder Theorem + +## Tech Stacks :- + +- HTML +- CSS +- JavaScript + +## Screenshots :- + +![image](https://github.com/user-attachments/assets/d65c8135-7a28-4075-a730-78aaa1f12422) diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/assets/background.jpg b/Calculators/Chinese-Remainder-Theorem-Calculator/assets/background.jpg new file mode 100644 index 000000000..8ce463848 Binary files /dev/null and b/Calculators/Chinese-Remainder-Theorem-Calculator/assets/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..6f1788535 --- /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 + + +
+
+ + +
+
+ + + \ No newline at end of file diff --git a/Calculators/Chinese-Remainder-Theorem-Calculator/script.js b/Calculators/Chinese-Remainder-Theorem-Calculator/script.js new file mode 100644 index 000000000..03ce24b99 --- /dev/null +++ b/Calculators/Chinese-Remainder-Theorem-Calculator/script.js @@ -0,0 +1,76 @@ +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..87c27bc8f --- /dev/null +++ b/Calculators/Chinese-Remainder-Theorem-Calculator/style.css @@ -0,0 +1,75 @@ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; + background: url(assets/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 diff --git a/calculators.json b/calculators.json index 4500a0254..17d605a67 100644 --- a/calculators.json +++ b/calculators.json @@ -479,6 +479,12 @@ "link": "./Calculators/Chi-Square-Calculator/index.html", "source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Chi-Square-Calculator" }, + { + "title": "Chinese Remainder Theorem Calculator", + "description": "Solves systems of modular equations using the Chinese Remainder Theorem.", + "link": "./Calculators/Chinese-Remainder-Theorem-Calculator/index.html", + "source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Chinese-Remainder-Theorem-Calculator" + }, { "title": "Chinese Zodiac Calculator", "description": "Calculates the Chinese zodiac sign of the user by taking the year of birth as input.",