Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check Disarium number - JavaScript
A Disarium number is a number where the sum of its digits raised to their respective positions equals the original number.
Definition
For a number with digits xy...z, it's a Disarium number if:
xy...z = x^1 + y^2 + ... + z^n
Where n is the total number of digits in the number.
Example
Let's check if 175 is a Disarium number:
175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175
Since the sum equals the original number, 175 is a Disarium number.
JavaScript Implementation
const num = 175;
const isDisarium = num => {
const res = String(num)
.split("")
.reduce((acc, val, ind) => {
acc += Math.pow(+val, ind+1);
return acc;
}, 0);
return res === num;
};
console.log(isDisarium(num));
console.log(isDisarium(32));
console.log(isDisarium(4334));
true false false
How It Works
The function converts the number to a string, splits it into individual digits, and uses reduce to calculate the sum of each digit raised to its position (starting from 1). Finally, it compares this sum with the original number.
Alternative Approach
function checkDisarium(num) {
let digits = num.toString().split('');
let sum = 0;
for (let i = 0; i
true
true
true
Conclusion
Disarium numbers are identified by checking if the sum of digits raised to their positions equals the original number. Both functional and iterative approaches work effectively for this calculation.
