Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Chinese Remainder Theorem Calculator #1985

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Calculators/Chinese-Remainder-Theorem-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# <p align="center">Chinese Remainder Theorem Calculator</p>

## 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)
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">
<link rel="stylesheet" href="style.css">
<title>Chinese Remainder Theorem Calculator</title>
</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>
76 changes: 76 additions & 0 deletions Calculators/Chinese-Remainder-Theorem-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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;
}
75 changes: 75 additions & 0 deletions Calculators/Chinese-Remainder-Theorem-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions calculators.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Loading