PHP Disarium Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Disarium Number?

A positive integer is a Disarium number when the sum of its digits, each raised to the power of its left-to-right position (starting at 1), equals the number itself.

Remember
Positions:  1  2  3  …
             ↓  ↓  ↓
Example:    1  3  5
  1¹ + 3² + 5³ = 1 + 9 + 125 = 135  ← Disarium
nCalculationDisarium?
8981 + 92 = 8 + 81 = 89Yes
13511 + 32 + 53 = 135Yes
89 vs 9091 + 02 = 990 → No

How to Check a Disarium Number in PHP

Four clear steps — then pick a right-to-left peel or a left-to-right digit string.

  1. Require a positive integer ($n > 0).
  2. Count the digits (or convert to a string).
  3. Raise each digit to its position power and add the results.
  4. Compare the sum with the original $n.
MethodIdeaTimeBest for
Right-to-left peelStart power at digit count; decrease while peelingO(d²)Interviews without strings
Digit stringIndex $i maps to power $i + 1O(d²)Clear left-to-right reasoning

Pseudocode

Pseudocode
function IsDisarium(n):
    if n <= 0: return false

    digits = countDigits(n)
    sum = 0
    temp = n
    power = digits

    while temp > 0:
        digit = temp mod 10
        sum = sum + digit ^ power
        power = power - 1
        temp = temp / 10

    return sum == n

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
PositionLeftmost digit has power 1
vs ArmstrongArmstrong uses one shared exponent (digit count)
One digit1–9 are all Disarium
Classic checks89, 135
Power helperPrefer integer multiply over pow()
Zero / negativesTreat as not Disarium in this tutorial

Live Preview

Enter a positive integer to see each digitposition term and the final check.

Positive integers only. Tap a chip or type a value.

Result
n = 135
1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135
135 is a Disarium number

Worked Walkthrough — n = 135

Three digits → powers 1, 2, and 3 from left to right.

Calculation
Digits: 1, 3, 5   (count = 3)

1¹ = 1
3² = 9
5³ = 125

Sum = 1 + 9 + 125 = 135
135 == 135 → Disarium

Right-to-left peel (same powers):
  peel 5 with power 3 → 125
  peel 3 with power 2 → 9
  peel 1 with power 1 → 1

PHP Programs

Start with the integer peel so you can explain every power without strings. Then use a digit string when left-to-right indexing feels clearer.

Example 1 — Right-to-Left Peel

Count digits, then peel from the right while decreasing the position power.

PHP
<?php
function intPow(int $base, int $exp): int
{
    $result = 1;
    for ($i = 0; $i < $exp; $i++) {
        $result *= $base;
    }
    return $result;
}

function countDigits(int $n): int
{
    $count = 0;
    while ($n > 0) {
        $count++;
        $n = intdiv($n, 10);
    }
    return $count;
}

function isDisarium(int $n): bool
{
    if ($n <= 0) {
        return false;
    }

    $power = countDigits($n);
    $sum = 0;
    $temp = $n;

    while ($temp > 0) {
        $digit = $temp % 10;
        $sum += intPow($digit, $power);
        $power--;
        $temp = intdiv($temp, 10);
    }

    return $sum === $n;
}

$number = 135;
if (isDisarium($number)) {
    echo $number . " is Disarium\n";
} else {
    echo $number . " is not Disarium\n";
}

How It Works

  1. intPow multiplies in a loop so powers stay exact integers (no pow() floats).
  2. countDigits finds how many positions exist — that becomes the starting power.
  3. Reject non-positive inputs; this page defines Disarium for positive integers only.
  4. Each $temp % 10 peels the next rightmost digit; $power-- keeps positions correct.
  5. Compare $sum === $n. For 135: 5³ + 3² + 1¹ = 125 + 9 + 1 = 135.

Example 2 — Digit String

Convert to a string so index $i maps directly to power $i + 1.

PHP
<?php
function intPow(int $base, int $exp): int
{
    $result = 1;
    for ($i = 0; $i < $exp; $i++) {
        $result *= $base;
    }
    return $result;
}

function isDisarium(int $n): bool
{
    if ($n <= 0) {
        return false;
    }

    $s = (string) $n;
    $sum = 0;
    $len = strlen($s);

    for ($i = 0; $i < $len; $i++) {
        $digit = (int) $s[$i];
        $power = $i + 1;
        $sum += intPow($digit, $power);
    }

    return $sum === $n;
}

echo (isDisarium(89) ? "true" : "false") . "\n";
echo (isDisarium(90) ? "true" : "false") . "\n";
echo (isDisarium(135) ? "true" : "false") . "\n";

How It Works

  1. (string) $n writes digits in left-to-right order.
  2. Index 0 is position 1, so power is always $i + 1.
  3. (int) $s[$i] converts a digit character to an integer.
  4. Same intPow helper keeps results exact.
  5. Answers match Example 1; pick the style that is easier to explain in the interview.

Edge Cases & Pitfalls

Check these before calling the solution done.

1–9

Single digits

All of 1 through 9 pass because d¹ = d.

Powers

Not all cubes

Each digit gets its own position power — unlike Armstrong.

pow()

Prefer intPow

pow() returns float; keep integer multiply for exactness.

≤ 0

Not Disarium here

This tutorial requires a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Right-to-left peelO(d²)O(1)
Digit stringO(d²)O(d) for the string

Here d is the digit count. Each power costs up to O(d) multiplies, and there are d digits.

Key Takeaways

  • Definition: digitposition sum equals n (positions from the left).
  • Peel trick: start power at digit count and decrease while peeling right.
  • vs Armstrong: Disarium uses different powers per digit.
  • Test set: 7 → yes; 89 → yes; 135 → yes; 90 → no.

One line: raise each digit to its left-to-right position and compare the sum with n.

Frequently Asked Questions

A positive integer n is Disarium if the sum of its digits, each raised to the power of its left-to-right position (starting at 1), equals n. Example: 89 = 8¹ + 9².
Armstrong raises every digit to the same power (the digit count). Disarium raises each digit to its own position index from the left.
n % 10 and intdiv(n, 10) are simple. Start the exponent at the digit count and decrease it so powers still match left-to-right positions.
pow() returns float. A tiny integer intPow helper avoids floating-point confusion in interview-style code.
Yes for 1 through 9, because d¹ = d. Zero is usually excluded when the definition requires a positive integer.
For d digits, a straightforward check with an integer power helper is O(d²) time and O(1) extra space for the peel method.

Next: Even Number

Check whether a number is divisible by 2.

Even number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

18 people found this page helpful