Check Disarium Number in PHP

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
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, 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 expected interview output.

Live preview

Try small positives in the browser with the same sum rule.

Clean integer method

Use intPow helper to keep logic simple and avoid float confusion.

Prerequisites

Decimal digits, % 10 peel, loops, and nonnegative integer conventions.

  • Basic PHP functions, loops, and integer operations.
  • Use intdiv() for clear integer division while peeling digits.

What is a Disarium number?

Write 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 numbers, which use one fixed exponent for all digits.

Disariumexponent = position
898¹ + 9²
1351¹ + 3² + 5³

Formal sum

Let k be number of digits of positive n. Disarium means n = ∑p=1k dpp.

89

81 + 92 = 8 + 81 = 89.

Intuition

89Disarium
Sum
8 + 81
10Not
Sum
1 + 0 ≠ 10

Takeaway: the rightmost digit gets the largest exponent in the Disarium sum.

Live preview

Positive integers in 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

For positive input, count decimal digits.

Peel from the right

Start pos = k; add digitpos while dividing by 10.

Compare

Return true if sum equals original n.

📜 Pseudocode

Pseudocode
function isDisarium(n):  // n > 0
    k = countDigits(n)
    sum = 0
    pos = k
    while n > 0:
        digit = n mod 10
        sum = sum + digit^pos
        pos = pos - 1
        n = floor(n / 10)
    return sum = original
1

Single value: 89

Exact integer-power helper and clear positive-input convention.

php
<?php
function intPow(int $base, int $exp): int {
    $r = 1;
    for ($i = 0; $i < $exp; $i++) $r *= $base;
    return $r;
}
function countDigits(int $number): int {
    if ($number === 0) return 1;
    $count = 0;
    while ($number > 0) {
        $count++;
        $number = intdiv($number, 10);
    }
    return $count;
}
function isDisarium(int $number): bool {
    if ($number <= 0) return false;
    $original = $number;
    $digitCount = countDigits($number);
    $sum = 0;
    while ($number > 0) {
        $digit = $number % 10;
        $sum += intPow($digit, $digitCount);
        $digitCount--;
        $number = intdiv($number, 10);
    }
    return $sum === $original;
}
$inputNumber = 89;
echo isDisarium($inputNumber) ? "{$inputNumber} is a Disarium number.\n" : "{$inputNumber} is not a Disarium number.\n";
?>

Explanation

For 89, first peeled digit is 9 with power 2, then 8 with power 1.

if ($number <= 0) return false;

Convention. This tutorial checks positive Disarium numbers.

2

Disarium numbers from 1 to 100

Expected output: single digits plus 89.

php
<?php
// reuse intPow(), countDigits(), isDisarium()
echo "Disarium numbers in the range 1 to 100:\n";
for ($i = 1; $i <= 100; $i++) {
    if (isDisarium($i)) {
        echo $i . " ";
    }
}
echo "\n";
?>

Explanation

All positive one-digit numbers are Disarium. In two digits under 100, only 89 matches.

Optimization

Precompute powers. Cache digit^position values when scanning larger ranges.

Prune ranges. Basic sum bounds can skip impossible candidates quickly.

Interview: define positions clearly and mention Armstrong contrast.

❓ 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. Decrementing digitCount after each peel assigns the correct exponent.
pow() returns float. A tiny integer intPow helper avoids floating-point confusion in interview-style code.
This page follows the usual interview convention of positive Disarium numbers only, so 0 is treated as not Disarium.
For positive digits 1-9, the sum is d^1 = d, so yes.
O(d^2) in a simple implementation, where d is number of digits. With cached powers, it can be closer to O(d).

🔄 Input / output examples

Change $inputNumber in Example 1 or range limit in Example 2.

nDisarium?Sum check
89Yes8¹ + 9² = 89
135Yes1¹ + 3² + 5³ = 135
10No1¹ + 0² = 1
7Yessingle digit

Edge cases and pitfalls

Most common mistake: mixing left-position definition with right-to-left exponent order.

Definition

Position order

Powers are based on left-to-right positions, not right-to-left.

Zero

n = 0

This page follows positive-integer Disarium convention, so 0 is excluded.

pow()

Float return

Prefer integer helper in beginner/interview code to avoid float-rounding confusion.

Armstrong

Different rule

Armstrong uses same exponent for all digits; Disarium uses position exponent.

⏱️ Time and space complexity

OperationTimeExtra space
isDisarium(n)O(d^2) basicO(1)
Range [1, N]O(N * d^2)O(1)

With cached powers, per-number work can approach O(d).

Summary

  • Rule: left-to-right position p raises the p-th digit to p.
  • Code: peel digits from right while decrementing position counter.
  • Watch-outs: positive-only convention, position order, 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