- Sum
8 + 81
Check Disarium Number in PHP
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.
Formal sum
Let k be number of digits of positive n. 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 JavaScript safe range. Zero and negatives are rejected here.
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
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 = originalSingle value: 89
Exact integer-power helper and clear positive-input convention.
<?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.
Disarium numbers from 1 to 100
Expected output: single digits plus 89.
<?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
🔄 Input / output examples
Change $inputNumber in Example 1 or range limit 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
Most common mistake: mixing left-position definition with right-to-left exponent order.
Position order
Powers are based on left-to-right positions, not right-to-left.
n = 0
This page follows positive-integer Disarium convention, so 0 is excluded.
Float return
Prefer integer helper in beginner/interview code to avoid float-rounding confusion.
Different rule
Armstrong uses same exponent for all digits; Disarium uses position exponent.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isDisarium(n) | O(d^2) basic | O(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
praises thep-th digit top. - Code: peel digits from right while decrementing position counter.
- Watch-outs: positive-only convention, position order, 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
