Neon Number in a Range Using recursion
In this approach, the function will calculate the square of the number and then sum its digits. If the sum equals the original number, it’s a Neon number. Then, we’ll recursively call the function for the next number in the range until we reach the end.
Example: The below code example Uses the recursion Method to find a Neon number in a Range in JavaScript.
Javascript
function sumOfDigits(number) { if (number === 0) { return 0; } return (number % 10) + sumOfDigits(Math.floor(number / 10)); } function isNeon(number) { let square = number * number; let sum = sumOfDigits(square); return sum === number; } function findNeonInRangeRecursive(start, end, result = []) { if (start > end) { return result; } if (isNeon(start)) { result.push(start); } return findNeonInRangeRecursive(start + 1, end, result); } console.log(findNeonInRangeRecursive(1, 100)); |
[ 1, 9 ]
JavaScript Program to Find Neon Number in a Range
A Neon number is a number where the sum of digits of the square of the number is equal to the number itself. For example, 9 is a Neon number because 9^2 = 81 and the sum of digits of 81 is 8 + 1 = 9, which is the number itself. We want to find all the Neon numbers within a given range.
Below are the approaches to find Neon Numbers in a Range:
Table of Content
- Using Iteration
- Using recursion
- Using JavaScript built-in methods