
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Narcissistic Number
In this article, we will learn to check if a number is Narcissistic in JavaScript. A Narcissistic number is a number that equals the sum of its digits, each raised to the power of the total number of digits. We will explore two approaches to solve this problem an iterative method and a concise string manipulation technique.
Narcissistic number
A narcissistic number(also known as an Armstrong number) in a given number base b is a number that is the sum of its digits each raised to the power of the number of digits.
For example ?
153 = 1^3 + 5^3 + 3^3 = 1+125+27 = 153
Similarly,
1 = 1^1 = 1
Different Approaches
Following are the different approaches to check if a number is Narcissistic in JavaScript ?
Using Iterative Approach
This approach breaks down the problem step by step. It first calculates the total number of digits in the number, then processes each digit to compute the sum of their powers. Finally, it compares this sum to the original number to check if it qualifies as a Narcissistic number.
Following are the steps to check if a number is Narcissistic using an Iterative approach ?
-
Count Digits: Use a while loop to divide the number by 10 until it becomes less than 1. Each division increments the count of digits.
-
Sum the Powers: Extract each digit using the modulo operator % and raise it to the power of the digit count. Accumulate the results.
- Compare: Check if the sum equals the original number. If true, the number is Narcissistic.
Calculating the sum of each digit using Math.pow and Math.Floor methods ?
sum += Math.pow(temp % 10, count);temp = Math.floor(temp / 10);
Example
Below is an exampleto check if a number is Narcissistic using an iterative approach ?
const isNarcissistic = (num) => { let m = 1, count = 0; while(num / m > 1){ m *= 10; count++; }; let sum = 0, temp = num; while(temp){ sum += Math.pow(temp % 10, count); temp = Math.floor(temp / 10); }; return sum === num; }; console.log(isNarcissistic(153)); console.log(isNarcissistic(1634)); console.log(isNarcissistic(1433)); console.log(isNarcissistic(342));
Output
true true false false
Time Complexity: O(n), where n is the number of digits in the number. The while loops process each digit.
Space Complexity: O(1), as no additional data structures are used.
Using String Manipulation
This approach takes a more concise and expressive route, focusing on simplifying the process. It directly works with the digits of the number, computes their powers, and accumulates the sum in a streamlined manner.
Following are the steps to check if a number is Narcissistic using string manipulation ?
-
Convert to String: Convert the number into a string and split it into an array of characters representing digits using the String.split() method.
-
Calculate Power: Use the array's length property to determine the number of digits.
-
Sum the Powers: Use the reduce() method to iterate over the digits. Convert each digit back to a number, raise it to the power of the digit count, and accumulate the results.
- Compare: Return true if the sum matches the original number.
Example
Below is an example to check if a number is Narcissistic using string manipulation ?
const isNarcissistic = (num) => { const digits = String(num).split(""); // Convert number to string and split into digits const power = digits.length; // Calculate the number of digits const sum = digits.reduce((acc, digit) => acc + Math.pow(Number(digit), power), 0); return sum === num; // Compare the sum with the original number }; console.log(isNarcissistic(153)); console.log(isNarcissistic(1634)); console.log(isNarcissistic(1433)); console.log(isNarcissistic(342));
Output
true true false false
Time Complexity: O(n), where n is the number of digits. The reduce() method processes each digit.
Space Complexity: O(n), due to the string representation of the number and the split array.
Comparison Table
Aspect | Iterative | String Manipulation |
Readability | Moderate | High |
Performance | Faster (no extra arrays) | Slightly slower (due to string manipulation) |
Space Usage | Lower (constant space) | Higher (temporary arrays) |
Suitability | Better for performance-critical tasks | More concise and expressive |