Skip to content

Commit

Permalink
Added CRT Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaharwal committed Jan 14, 2025
1 parent e0cbb81 commit 08e2ca6
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions Calculators/Chinese-Remainder-Theorem-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chinese Remainder Theorem Calculator</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>Chinese Remainder Theorem Calculator</h1>
<p>Add equations you want to solve:</p>
<div id="inputs">
<div class="equation">
<input type="number" placeholder="Remainder" class="remainder" />
<span>mod</span>
<input type="number" placeholder="Modulo" class="modulo" />
<button class="remove-btn" onclick="removeEquation(this)">-</button>
</div>
</div>
<button class="add-btn" onclick="addEquation()">+</button>
<button class="solve-btn" onclick="solve()">SOLVE ></button>
<div id="output" class="output"></div>
</div>
<script src="script.js"></script>
</body>
</html>
77 changes: 77 additions & 0 deletions Calculators/Chinese-Remainder-Theorem-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function addEquation() {
const inputsDiv = document.getElementById("inputs");
const newEquation = document.createElement("div");
newEquation.className = "equation";
newEquation.innerHTML = `
<input type="number" placeholder="Remainder" class="remainder">
<span>mod</span>
<input type="number" placeholder="Modulo" class="modulo">
<button class="remove-btn" onclick="removeEquation(this)">-</button>
`;
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 = `<p>Using Chinese Remainder Theorem, solve the system of equations:</p>`;
explanation += `<p>${remainders.map((a, i) => `x ≡ ${a} (mod ${modulos[i]})`).join("<br>")}</p>`;
explanation += `<p>1. Verify moduli are pairwise coprime: ${modulos.map((m1, i) =>
modulos.slice(i + 1).map(m2 => `gcd(${m1}, ${m2}) = ${gcd(m1, m2)}`).join(", ")
).join(", ")}.</p>`;
explanation += `<p>2. Calculate M = ${modulos.join(" × ")} = ${m}</p>`;
explanation += `<p>3. Calculate Mi and modular inverses:</p>`;
M.forEach((Mi, index) => {
explanation += `<p>M${index + 1} = ${Mi}, inverse (mod ${modulos[index]}) = ${y[index]}</p>`;
});
explanation += `<p>4. Calculate x using: x = Σ(ai * Mi * yi) mod M = ${x}</p>`;
explanation += `<p><strong>Final Answer: x = ${x}</strong></p>`;

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;
}

76 changes: 76 additions & 0 deletions Calculators/Chinese-Remainder-Theorem-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 08e2ca6

Please sign in to comment.