Skip to content

Commit

Permalink
Added Partition Number Calculator (#1953)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhhsinghh authored Jan 4, 2025
1 parent 808c10a commit 8c934d0
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Calculators/Partition-Number-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# <p align="center">Partition Number Calculator</p>

## Description :-

Partition Number Calculator is a calculator that calculates the number of distinct partitions for a given non-negative integer. In mathematics, a partition of a number 𝑛 is a way of writing 𝑛 as a sum of positive integers, disregarding the order of the summands.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Screenshots:-

![image](https://github.com/user-attachments/assets/a5cbf57b-0fa7-4600-949c-dc627b1d2c8a)
29 changes: 29 additions & 0 deletions Calculators/Partition-Number-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!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>Partition Number Calculator</title>
</head>

<body>
<div class="container">
<h1>✨ Partition Number Calculator ✨</h1>
<p class="description">Discover the number of ways to partition a given number!</p>
<form id="partitionForm">
<div class="input-group">
<input type="number" id="numberInput" placeholder="Enter a number" min="0" aria-label="Enter a non-negative number" required>
<span id="errorMessage" class="error-message"></span>
</div>
<button type="submit" id="calculateButton">🚀 Calculate</button>
</form>
<div id="result" class="result-box"></div>
<div id="spinner" class="spinner hidden"></div>
</div>

<script src="script.js"></script>
</body>

</html>
48 changes: 48 additions & 0 deletions Calculators/Partition-Number-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// DOM Elements
const form = document.getElementById("partitionForm");
const numberInput = document.getElementById("numberInput");
const resultDiv = document.getElementById("result");
const errorMessage = document.getElementById("errorMessage");
const spinner = document.getElementById("spinner");
const calculateButton = document.getElementById("calculateButton");

// Partition Calculation Function
function calculatePartition(num) {
const partitions = Array(num + 1).fill(0);
partitions[0] = 1;

for (let i = 1; i <= num; i++) {
for (let j = i; j <= num; j++) {
partitions[j] += partitions[j - i];
}
}
return partitions[num];
}

// Form Submission Event
form.addEventListener("submit", (e) => {
e.preventDefault();

const input = numberInput.value.trim();
const num = parseInt(input, 10);

// Validation
if (isNaN(num) || num < 0) {
errorMessage.textContent = "⚠️ Please enter a valid non-negative number.";
resultDiv.textContent = "";
return;
}

errorMessage.textContent = "";
resultDiv.textContent = "";
spinner.classList.remove("hidden");
calculateButton.disabled = true;

// Simulate Computation
setTimeout(() => {
const result = calculatePartition(num);
resultDiv.innerHTML = `🎉 Partition number for <strong>${num}</strong> is: <strong>${result}</strong>`;
spinner.classList.add("hidden");
calculateButton.disabled = false;
}, Math.min(500 + num * 5, 2000));
});
132 changes: 132 additions & 0 deletions Calculators/Partition-Number-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
body {
font-family: "Poppins", Arial, sans-serif;
margin: 0;
padding: 0;
background: linear-gradient(135deg, #a18cd1, #fbc2eb);
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}

/* Main Container */
.container {
background: #ffffff;
padding: 30px;
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
text-align: center;
max-width: 500px;
width: 90%;
position: relative;
}

h1 {
color: #6c63ff;
margin-bottom: 10px;
font-size: clamp(1.5rem, 5vw, 1.8rem);
}

.description {
font-size: 1rem;
color: #555;
margin-bottom: 20px;
}

/* Input Group */
.input-group {
margin-bottom: 20px;
}

input {
padding: 12px;
font-size: 16px;
width: 100%;
border: 2px solid #ddd;
border-radius: 8px;
outline: none;
transition: 0.3s ease;
}

input:focus {
border-color: #6c63ff;
box-shadow: 0 0 8px rgba(108, 99, 255, 0.3);
}

.error-message {
display: block;
margin-top: 5px;
font-size: 0.85rem;
color: #ff1744;
min-height: 1rem;
}

/* Button */
button {
padding: 12px 30px;
font-size: 16px;
color: #fff;
background-color: #6c63ff;
border: none;
border-radius: 8px;
cursor: pointer;
transition: 0.3s ease;
}

button:disabled {
background-color: #ccc;
cursor: not-allowed;
}

button:hover:not(:disabled) {
background-color: #5848e0;
}

/* Result Box */
.result-box {
margin-top: 20px;
padding: 15px;
background: #f3f3f3;
border-radius: 10px;
font-size: 1.2rem;
color: #444;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* Spinner */
.spinner {
margin: 20px auto;
border: 6px solid #f3f3f3;
border-top: 6px solid #6c63ff;
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 1s linear infinite;
}

.spinner.hidden {
display: none;
}

@keyframes spin {
0% {
transform: rotate(0deg);
}

100% {
transform: rotate(360deg);
}
}

/* Footer */
footer {
margin-top: 20px;
text-align: center;
font-size: 0.9rem;
color: #fff;
}
6 changes: 6 additions & 0 deletions calculators.json
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,12 @@
"link": "./Calculators/Partial-Derivative-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Partial-Derivative-Calculator"
},
{
"title": "Partition Number Calculator",
"description": "Calculates the number of distinct partitions for a given non-negative integer.",
"link": "./Calculators/Partition-Number-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Partition-Number-Calculator"
},
{
"title": "Password Strength Calculator",
"description": "Calculates the strength of the Password.",
Expand Down

0 comments on commit 8c934d0

Please sign in to comment.