Skip to content

Commit

Permalink
Added check number option in Armstrong Number Calculator (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhhsinghh authored Dec 20, 2024
1 parent fb25146 commit 872bd32
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 113 deletions.
10 changes: 8 additions & 2 deletions Calculators/Armstrong-Number-Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## Description :-

Calculates all the Armstrong Numbers within a range measured by the number of digits.
This tool helps you explore Armstrong numbers.
- Use the Checker to verify if a specific number is an Armstrong number.
- Use the Finder to generate Armstrong numbers for a given number of digits.

## Tech Stacks :-

Expand All @@ -12,4 +14,8 @@ Calculates all the Armstrong Numbers within a range measured by the number of di

## Screenshots :-

![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/1c41cfd8-dce2-4529-9dcf-a4e33c24e5c8)
![image](https://github.com/user-attachments/assets/942bff47-a7f1-4d19-b835-4c2addcf2364)

![image](https://github.com/user-attachments/assets/d3f3bccc-eeeb-40d9-9428-8345a5015aa5)

![image](https://github.com/user-attachments/assets/377d52d9-4d3c-494f-9456-1e358c4f9622)
56 changes: 41 additions & 15 deletions Calculators/Armstrong-Number-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,52 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Armstrong Number Calculator</title>
<link rel="stylesheet" href="style.css">
<title>Armstrong Number Calculator</title>
</head>

<body>
<header>
<h1>Armstrong Number Calculator for N-Digits</h1>
</header>
<main>
<div class="container">
<form name="myform">
<label for="no">Enter Number of Digits (should be <=7):</label>
<input type="number" name="numOfDigits" id="no">
<button type="button" onclick="findArmstrongNumbers()">Find Armstrong Numbers</button>
</form>
<div id="armstrongNumbers"></div>
<div id="additionalContent"></div>
<!-- Navigation Bar -->
<nav class="navbar">
<div class="nav-container">
<a href="#" class="logo">Armstrong Utility</a>
<ul class="nav-links">
<li><a href="#" onclick="showSection('description')">Home</a></li>
<li><a href="#" onclick="showSection('checker')">Checker</a></li>
<li><a href="#" onclick="showSection('finder')">Finder</a></li>
</ul>
</div>
</main>
</nav>

<!-- Main Content -->
<div class="container">
<div id="description" class="card">
<h2>Welcome to Armstrong Utility</h2>
<p>
Explore Armstrong numbers!<br><br>
• Use <strong>Checker</strong> to verify if a specific number is an Armstrong number.<br>
• Use <strong>Finder</strong> to generate Armstrong numbers for a given number of digits.
</p>
</div>

<div id="checker" class="card hidden">
<h2>Armstrong Number Checker</h2>
<p>Check if a specific number is an Armstrong number:</p>
<input type="text" id="number" placeholder="Enter a number" />
<button onclick="checkArmstrong()">Check</button>
<div id="result" class="result"></div>
</div>

<div id="finder" class="card hidden">
<h2>Find Armstrong Numbers</h2>
<p>Find Armstrong numbers by specifying the number of digits:</p>
<input type="number" id="numOfDigits" min="1" max="7" placeholder="Enter number of digits" />
<button onclick="findArmstrongNumbers()">Find Numbers</button>
<div id="armstrongNumbers" class="result"></div>
</div>
</div>

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

</html>
</html>
89 changes: 49 additions & 40 deletions Calculators/Armstrong-Number-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
// Function to find Armstrong numbers based on user input
function findArmstrongNumbers() {
const numOfDigits = document.forms.myform.numOfDigits.value;
// Show the selected section and hide others
function showSection(sectionId) {
const sections = document.querySelectorAll('.card');
sections.forEach((section) => {
if (section.id === sectionId) {
section.classList.remove('hidden');
} else {
section.classList.add('hidden');
}
});
}

// Validate user input
if (numOfDigits === "" || Number.isNaN(numOfDigits) || numOfDigits < 1 || numOfDigits > 7) {
alert("Please enter a valid number between 1 and 7");
} else {
const n = parseInt(numOfDigits);
const myElement = document.getElementById("armstrongNumbers");
const chunkSize = 1000;
// Check if a number is Armstrong
function checkArmstrong() {
const numberInput = document.getElementById("number").value;
const resultDiv = document.getElementById("result");

let startRange = Math.pow(10, n - 1);
let endRange = Math.pow(10, n);
if (!numberInput || isNaN(numberInput)) {
resultDiv.innerHTML = "<span style='color: yellow; font-weight: 600; font-size: 19px'>Please enter a valid number.</span>";
return;
}

let armstrongNumbers = [];
const number = parseInt(numberInput, 10);
const digits = numberInput.split("").map(Number);
const power = digits.length;
const sum = digits.reduce((acc, digit) => acc + Math.pow(digit, power), 0);

// Process the range in smaller chunks to improve responsiveness
for (let chunkStart = startRange; chunkStart <= endRange; chunkStart += chunkSize) {
let chunkEnd = Math.min(chunkStart + chunkSize, endRange);
if (sum === number) {
resultDiv.innerHTML = `<span style="color: #63f54c; font-weight: 600; font-size: 19px">Yes! ${number} is an Armstrong number.</span>`;
} else {
resultDiv.innerHTML = `<span style="color: yellow; font-weight: 600; font-size: 19px">No, ${number} is not an Armstrong number.</span>`;
}
}

// Check for Armstrong numbers in the current chunk
for (let num = chunkStart; num < chunkEnd; num++) {
if (isArmstrongOptimized(num, n)) {
armstrongNumbers.push(num);
}
}
}
// Find Armstrong numbers
function findArmstrongNumbers() {
const numOfDigits = document.getElementById("numOfDigits").value;
const resultDiv = document.getElementById("armstrongNumbers");

myElement.textContent = "Armstrong Numbers: " + armstrongNumbers.join(", ");
if (!numOfDigits || isNaN(numOfDigits) || numOfDigits < 1 || numOfDigits > 7) {
resultDiv.innerHTML = "<span style='color: yellow; font-weight: 600; font-size: 19px'>Please enter a valid number between 1 and 7.</span>";
return;
}
}

// Function to check if a number is an Armstrong number
function isArmstrongOptimized(num, power) {
let temp = num;
let sumOfPowers = 0;
const n = parseInt(numOfDigits, 10);
const start = Math.pow(10, n - 1);
const end = Math.pow(10, n);
const armstrongNumbers = [];

// Compute the sum of digits raised to the power
while (temp > 0) {
let digit = temp % 10;
sumOfPowers += Math.pow(digit, power);
temp = Math.floor(temp / 10);
for (let i = start; i < end; i++) {
const digits = i.toString().split("").map(Number);
const sum = digits.reduce((acc, digit) => acc + Math.pow(digit, n), 0);
if (sum === i) {
armstrongNumbers.push(i);
}
}
const additionalContent = document.getElementById('additionalContent');
additionalContent.innerText = 'Explanation: A number that is equal to the sum of cubes of its digits.';

// Check whether sum of powers equals the original number or not
return sumOfPowers === num;
}

resultDiv.innerHTML = `<span style='color: yellow; font-weight: 600; font-size: 19px'>Armstrong Numbers: ${armstrongNumbers.join(", ")}</span>`;
}
Loading

0 comments on commit 872bd32

Please sign in to comment.