Skip to content

Instantly share code, notes, and snippets.

@YoosufAathil
Created April 11, 2024 17:58
Show Gist options
  • Select an option

  • Save YoosufAathil/4066798a99a34c73bc0ae5925251cba3 to your computer and use it in GitHub Desktop.

Select an option

Save YoosufAathil/4066798a99a34c73bc0ae5925251cba3 to your computer and use it in GitHub Desktop.
This gist contains the JavaScript solution for the quiz component of the DuoThan 4.0 competition. The code efficiently identifies all Armstrong numbers within the range of 0 to 100,000 using a systematic algorithm. It utilizes functions to check if a number is an Armstrong number and to find all Armstrong numbers within the specified range.
// Function to check if a number is an Armstrong number
function isArmstrong(number) {
// Convert the number to a string to determine its length
let numStr = String(number);
let numDigits = numStr.length;
// Calculate the sum of the digits raised to the power of the number of digits
let sumOfDigits = numStr.split('').reduce((sum, digit) => sum + Math.pow(parseInt(digit), numDigits), 0);
// Check if the sum is equal to the original number
return sumOfDigits === number;
}
// Function to find Armstrong numbers within the given range
function findArmstrongNumbers() {
let armstrongNumbers = [];
for (let num = 0; num <= 100000; num++) {
if (isArmstrong(num)) {
armstrongNumbers.push(num);
}
}
return armstrongNumbers;
}
// Print the Armstrong numbers within the range
console.log("Armstrong numbers within the range of 0 to 100,000:");
console.log(findArmstrongNumbers());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment