Exploring Narcissistic Numbers with JavaScript: A Complete Guide
Written on
Understanding Narcissistic Numbers
As we approach the festive season, the DevAdvent challenge continues with intriguing mathematical exercises. Today, I am excited to delve into the concept of Narcissistic Numbers, a fascinating topic discovered through CodeWars. Let's explore the problem at hand: "Does my number appear substantial in this context?"
A Narcissistic Number, also known as an Armstrong Number, is defined as a positive number that equals the sum of its own digits, each raised to the power of the total number of digits in that base. For this exercise, we will focus on base 10.
For instance, consider the number 153 (which has 3 digits):
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Conversely, the number 1652 (which has 4 digits) does not meet this criterion:
1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938
The Objective:
Your task is to create a function that returns either true or false, depending on whether a given number is a Narcissistic number in base 10. Note that input will strictly consist of valid positive integers, so no error checking for invalid inputs is necessary.
Breaking Down the Problem
One of the aspects I appreciate about such problems is their mathematical clarity. Starting from the definition, as noted in various sources:
In number theory, a Narcissistic number (also known as a pluperfect digital invariant (PPDI) or Armstrong number) in a specific base b is a number that equals the sum of its digits raised to the power of the number of digits.
Thus, the steps to solve this problem are as follows:
- Separate the number into its constituent digits.
- Determine the total number of digits.
- Raise each digit to the appropriate power.
- Sum the results.
Finally, a simple comparison between the calculated sum and the original number will reveal whether the number is Narcissistic.
Implementing the Solution in JavaScript
Here's how I translated the aforementioned steps into JavaScript code:
export const narcissistic = (n: number): boolean =>
[...`${n}`].map(Number).reduce((a, x) => a + x ** ${n}.length, 0) === n;
Revisiting the steps:
- To extract digits:
const digits: number[] = [...`${n}`].map(Number);
- To count the digits:
const exponent: number = ${n}.length;
- To calculate the power:
const power: number = x ** exponent;
- To sum the powers:
const sum: number = digits.reduce((a, x) => a + x ** exponent, 0);
This allows us to streamline the operations into a single cohesive function. Alternatively, for better readability, the following extended version can be used:
export function narcissistic(n: number): boolean {
const digits: number[] = [...`${n}`].map(Number);
const exponent: number = digits.length;
const pow = (x: number) => x ** exponent;
const sum: number = digits.reduce((a, x) => a + pow(x), 0);
return sum === n;
}
The AI's Take on the Problem
I consulted ChatGPT, a leading AI tool, for its solution to this issue:
function isNarcissistic(num: number): boolean {
const numStr = num.toString();
const numDigits = numStr.length;
let sum = 0;
for (const digit of numStr) {
sum += Math.pow(parseInt(digit, 10), numDigits);}
return sum === num;
}
While this solution is functional, it may lack elegance compared to others I've encountered. However, it successfully provided a correct response on the first attempt, which is commendable.
Stay Tuned for More Insights!
Thank you for reading! Be sure to catch my upcoming articles. Sign up for my Medium email list for updates. For more content, visit PlainEnglish.io and subscribe to our free weekly newsletter. You can also connect with us on Twitter, LinkedIn, YouTube, and Discord.
If you're interested in scaling your software startup, check out Circuit.
Chapter 1: The Basics of Narcissistic Numbers
In this chapter, we'll cover the foundational concepts and definitions associated with Narcissistic Numbers, including their significance in number theory.
Section 1.1: Practical Examples
Chapter 2: Implementing the Solution in JavaScript
This video presents a beginner's solution to the Narcissistic Numbers challenge, providing a clear walkthrough of the coding process.
Chapter 3: AI Solutions and Comparisons
In this segment, we explore the coding challenge presented by ChatGPT and analyze its approach to solving the Narcissistic Numbers problem.