- Sum
8 + 81
Check Disarium Number in JavaScript
What you’ll learn
- The precise Disarium rule: digit powers use 1-based positions from the left.
- How to implement the sum with an LSD-first peel and a running position index.
- Integer exponentiation (no
Math.powin the samples), a 1–100 scan, live preview, and edge cases like zero.
Overview
A Disarium number equals the sum of its decimal digits, each raised to the power of its position from the left (most significant digit is position 1). The classic example is 89 = 81 + 92.
Two programs
Single test on 89 and a range sweep 1–100 matching the reference output.
Live preview
Try small positives in the browser with the same sum rule.
Fixes vs reference
countDigits(0) and integer ipow instead of Math.pow.
Prerequisites
Decimal digits, % 10 peel, loops, and nonnegative integer conventions.
console.log,Number.isSafeInteger, andMath.truncfor integer division (noMath.powin the samples on this page).- Optional:
BigIntif you extend to very large powers and sums.
What is a Disarium number?
Write the decimal digits of n as d1d2…dk from left to right. Then n is Disarium if n = d11 + d22 + … + dkk.
This is not the same as Armstrong (narcissistic) numbers, which use a fixed exponent equal to the digit count for every digit.
Formal sum
Let k = ⌊log10 n⌋ + 1 be the number of digits of positive n. Define positions p = 1..k from the left. Disarium means n = ∑p=1k dpp.
8981 + 92 = 8 + 81 = 89.
Intuition
- Sum
1 + 0≠ 10
Takeaway: the rightmost digit gets the largest exponent in the Disarium sum.
Live preview
Positive integers in the JavaScript safe range. Zero and negatives are rejected here.
Algorithm
Goal: decide whether positive n satisfies the Disarium sum.
Count digits k
Treat n = 0 as a special case (definition usually excludes it); otherwise count decimal digits.
Peel from the right
Let pos start at k. While work > 0: add digitpos to sum, decrement pos, divide work by 10.
Compare
Return whether sum equals the original n.
📜 Pseudocode
function isDisarium(n): // n > 0
k ← countDigits(n)
sum ← 0
pos ← k
work ← n
while work > 0:
digit ← work mod 10
sum ← sum + digit^pos
pos ← pos - 1
work ← floor(work / 10)
return sum = nSingle value: 89
Same logic as the reference with ipow for exact integers and countDigits that returns 1 for 0 (only needed if you extend tests).
function ipow(base, exp) {
let r = 1;
for (let i = 0; i < exp; i++) {
r *= base;
}
return r;
}
function countDigits(number) {
if (number === 0) {
return 1;
}
let count = 0;
let n = number;
while (n !== 0) {
count++;
n = Math.trunc(n / 10);
}
return count;
}
function isDisarium(number) {
if (number <= 0) {
return false;
}
const originalNumber = number;
let digitCount = countDigits(number);
let sum = 0;
let num = number;
while (num !== 0) {
const digit = num % 10;
sum += ipow(digit, digitCount);
digitCount--;
num = Math.trunc(num / 10);
}
return sum === originalNumber;
}
const inputNumber = 89;
if (isDisarium(inputNumber)) {
console.log(inputNumber + " is a Disarium number.");
} else {
console.log(inputNumber + " is not a Disarium number.");
}Explanation
For 89, k = 2. First peeled digit is 9 at position 2, then 8 at position 1.
if (number <= 0) return false;Convention. Keeps the sample aligned with positive Disarium numbers.
Disarium numbers from 1 to 100
Same output as the reference: single digits plus 89.
function ipow(base, exp) {
let r = 1;
for (let i = 0; i < exp; i++) {
r *= base;
}
return r;
}
function countDigits(num) {
if (num === 0) {
return 1;
}
let count = 0;
let n = num;
while (n !== 0) {
n = Math.trunc(n / 10);
count++;
}
return count;
}
function isDisarium(num) {
if (num <= 0) {
return false;
}
const originalNum = num;
let digitCount = countDigits(num);
let sum = 0;
while (num !== 0) {
const digit = num % 10;
sum += ipow(digit, digitCount);
num = Math.trunc(num / 10);
digitCount--;
}
return sum === originalNum;
}
console.log("Disarium numbers in the range 1 to 100:");
let line = "";
for (let i = 1; i <= 100; i++) {
if (isDisarium(i)) {
line += i + " ";
}
}
console.log(line.trim());Explanation
Single-digit numbers satisfy d1 = d. The only two-digit Disarium in this range is 89.
Optimization
Precompute powers. Digits are at most 9; you can cache 9p for p ≤ d when scanning many n.
Prune ranges. Simple bounds on the sum can skip impossible lengths, useful for much larger intervals than 100.
Interview: define positions clearly; mention Armstrong contrast; use integer powers.
❓ FAQ
🔄 Input / output examples
Swap inputNumber in Example 1 or widen the loop in Example 2.
| n | Disarium? | Sum check |
|---|---|---|
89 | Yes | 8¹ + 9² = 89 |
135 | Yes | 1¹ + 3² + 5³ = 135 |
10 | No | 1¹ + 0² = 1 |
7 | Yes | single digit |
Edge cases and pitfalls
The reference intro does not spell out that positions are counted from the left; mixing that convention breaks the test.
countDigits(0)
Returning 0 skips the sum loop and can falsely report Disarium unless you special-case.
Math.pow(8, 1)
Might yield 7.9999999… style noise when coerced to int in pathological cases; integers are safer.
Large n
Powers such as 99 still fit in 32-bit signed int, but wider digit counts need BigInt for the sum.
Different rule
An Armstrong number uses the same exponent (digit count) on every digit; do not merge the definitions in interview answers.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isDisarium(n) | O(d · d) naively with ipow per digit of length d | O(1) |
Range [1, N] | O(N · d2) worst case | O(1) |
With cached powers per digit position, the per-n work drops to O(d).
Summary
- Rule: left-to-right position
p(from1) raises thep-th digit top. - Code: peel LSD while a position counter runs down from
k. - Watch-outs:
0digit count,Math.powtyping, Armstrong confusion.
Besides 89, 135 is a classic Disarium example: 11 + 32 + 53 = 1 + 9 + 125 = 135. Every one-digit positive integer 1–9 satisfies the rule because d1 = d.
8 people found this page helpful
