Check Disarium Number in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code examples + Try it
Digit sums

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.pow in 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, and Math.trunc for integer division (no Math.pow in the samples on this page).
  • Optional: BigInt if 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.

Disarium exponent = position
89 8¹+9²
135 1¹+3²+5³

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.

89

81 + 92 = 8 + 81 = 89.

Intuition

89 Disarium
Sum
8 + 81
10 Not
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.

Shows the digit-power sum and whether it matches n.

Live result
Press “Check” to evaluate.

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

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 = n
1

Single 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).

JavaScript
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.");
}
Try it Yourself

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.

2

Disarium numbers from 1 to 100

Same output as the reference: single digits plus 89.

JavaScript
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());
Try it Yourself

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

A positive integer n is Disarium if the sum of its decimal digits, each raised to the power of its 1-based position counting from the left (most significant digit has position 1), equals n. Example: 89 = 8^1 + 9^2.
The code peels digits from the right with % 10, but the rightmost digit has the largest position index (units are the last position). Decrementing digitCount after each peel assigns the correct exponent.
Math.pow returns a double; small integer cases are usually exact, but an integer ipow helper avoids dependency and any rounding surprises in interview-style code.
countDigits must treat 0 as having one digit for a consistent loop; otherwise digitCount becomes 0 and the Disarium sum is vacuously 0, which blurs the usual definition focused on positive integers.
For positive digits 1–9, the sum is d^1 = d, so yes. Zero is usually excluded from the definition in lists like OEIS A032799.
O(d) for d decimal digits per test, with exponents bounded by d so each ipow is O(exponent) at worst; d is small for 32-bit ints.

🔄 Input / output examples

Swap inputNumber in Example 1 or widen the loop in Example 2.

nDisarium?Sum check
89Yes8¹ + 9² = 89
135Yes1¹ + 3² + 5³ = 135
10No1¹ + 0² = 1
7Yessingle 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.

Zero

countDigits(0)

Returning 0 skips the sum loop and can falsely report Disarium unless you special-case.

pow

Math.pow(8, 1)

Might yield 7.9999999… style noise when coerced to int in pathological cases; integers are safer.

Overflow

Large n

Powers such as 99 still fit in 32-bit signed int, but wider digit counts need BigInt for the sum.

Armstrong

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

OperationTimeExtra space
isDisarium(n)O(d · d) naively with ipow per digit of length dO(1)
Range [1, N]O(N · d2) worst caseO(1)

With cached powers per digit position, the per-n work drops to O(d).

Summary

  • Rule: left-to-right position p (from 1) raises the p-th digit to p.
  • Code: peel LSD while a position counter runs down from k.
  • Watch-outs: 0 digit count, Math.pow typing, Armstrong confusion.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful