-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added check number option in Armstrong Number Calculator (#1931)
- Loading branch information
1 parent
fb25146
commit 872bd32
Showing
5 changed files
with
231 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`; | ||
} |
Oops, something went wrong.