- Sum
8 + 81
Check Disarium Number in Python
What you’ll learn
- The Disarium rule in simple terms: position-based powers from left to right.
- How to implement it using easy digit peeling with
% 10and// 10. - Single check, range scan, live preview, and common mistakes.
Overview
A positive number is Disarium if each digit raised to its left-to-right position adds back to the same number. Example: 89 = 8^1 + 9^2.
Two programs
Single-number check and 1-100 scan.
Live preview
Try numbers instantly in browser.
Common confusion
Different from Armstrong numbers.
Prerequisites
Loops, modulo, integer division, and basic functions in Python.
- Use
% 10to read the last digit and// 10to remove it. - Use
powor**for integer exponentiation.
What is a Disarium number?
If digits are d1 d2 ... dk from left, check whether d1^1 + d2^2 + ... + dk^k equals the number.
Example: 135 = 1^1 + 3^2 + 5^3 so it is Disarium.
Formal sum
For positive n with k digits, Disarium means n = sum(d_i ^ i) where position i starts from 1 at the leftmost digit.
898^1 + 9^2 = 8 + 81 = 89
Intuition
- Sum
1 + 0 = 1
Takeaway: the rightmost digit always gets the highest exponent.
Live preview
Positive integers only, within JavaScript safe range.
Algorithm
Goal: check if positive n equals its position-power digit sum.
Count digits
Set exponent to total digit count.
Peel and add
Take last digit, raise to current exponent, add to sum, decrease exponent.
Compare sum
If sum equals original number, it is Disarium.
📜 Pseudocode
function is_disarium(n): // n > 0
k = number_of_digits(n)
sum = 0
pos = k
while n > 0:
digit = n mod 10
sum += digit ^ pos
pos -= 1
n = floor(n / 10)
return sum == original_n Single value check (89)
Direct and interview-friendly implementation for one input value.
def is_disarium(number: int) -> bool:
if number <= 0:
return False
original = number
digits = len(str(number))
total = 0
while number > 0:
digit = number % 10
total += digit ** digits
digits -= 1
number //= 10
return total == original
n = 89
if is_disarium(n):
print(f"{n} is a Disarium number.")
else:
print(f"{n} is not a Disarium number.") Explanation
The first peeled digit is the rightmost one, so we start exponent from full digit count and decrease each step.
Disarium numbers from 1 to 100
Prints all Disarium numbers in this beginner range.
def is_disarium(num: int) -> bool:
if num <= 0:
return False
original = num
digits = len(str(num))
total = 0
while num > 0:
digit = num % 10
total += digit ** digits
digits -= 1
num //= 10
return total == original
print("Disarium numbers in the range 1 to 100:")
for i in range(1, 101):
if is_disarium(i):
print(i, end=" ") Explanation
All one-digit positive numbers are Disarium. In two digits up to 100, only 89 matches.
Optimization
Cache powers. Precompute digit powers for each position when scanning large ranges.
Early pruning. Use rough upper bounds to skip impossible ranges.
Interview: clearly state left-position powers and Armstrong difference.
❓ FAQ
🔄 Input / output examples
Change input values in the first example or range limits in the second one.
| n | Disarium? | Check |
|---|---|---|
89 | Yes | 8^1 + 9^2 = 89 |
135 | Yes | 1^1 + 3^2 + 5^3 = 135 |
10 | No | 1^1 + 0^2 = 1 |
7 | Yes | 7^1 = 7 |
Edge cases and pitfalls
Most lists use positive numbers only; be explicit about zero and negatives.
n = 0
Commonly excluded by definition; this page checks positive numbers only.
Left-to-right count
Do not use right-to-left positions directly without adjusting exponent order.
Different formula
Armstrong uses fixed exponent equal to digit count for all digits.
Power growth
Use caching or careful bounds if scanning very large intervals.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single check (basic) | O(d^2) | O(1) |
| Single check (cached powers) | O(d) | O(d) |
Range [1..N] | About O(N * d^2) | O(1) |
Summary
- Rule: digit powers depend on left position.
- Code: peel from right, decrease exponent each step.
- Remember: Disarium is different from Armstrong.
Besides 89, 135 is a classic Disarium number because 11 + 32 + 53 = 135. Every one-digit positive number 1 to 9 is Disarium.
8 people found this page helpful
