- Sum
8 + 81
Check Disarium Number in C#
What you’ll learn
- The exact Disarium rule: powers use positions 1, 2, 3 … counting from the left digit.
- How to peel digits from the right while a counter runs downward so exponents still match.
- Integer powers (cleaner than
Math.Powfor interviews), a 1–100 scan, live preview, and how this differs from Armstrong numbers.
Overview
A Disarium number rebuilds itself from its own digits in a quirky way: raise each digit to the power of its place from the left (first digit uses power 1, second uses 2, and so on), add those powers, and see if you get the original number. The textbook demo is 89 = 81 + 92.
Two programs
One checks 89; the other prints every Disarium between 1 and 100.
Live preview
Watch each digit power appear for small positives.
Polish
IpPow keeps math in int land; clarify positions so you never confuse Disarium with Armstrong.
Prerequisites
Reading decimal digits, % 10, integer division, loops, and basic methods.
using System;,class Program,statichelpers,Console.WriteLine.- Comfort with small powers (digits are at most
9, exponents stay near the digit count).
What is a Disarium number?
Write the digits of n as d1d2…dk from left to right. Then n is Disarium when n = d11 + d22 + … + dkk.
This differs from an Armstrong number, where every digit is raised to the same power—usually the total number of digits.
Formal sum
If k is how many decimal digits n has, label positions 1 through k from the most significant digit. Disarium means n = Σp=1k dpp.
8981 + 92 = 8 + 81 = 89.
Quick comparisons
- Sum
1¹ + 0² = 1
Remember: the digit on the far right uses the biggest exponent (equal to how many digits you have).
Live preview
Positive integers in JavaScript safe range (zero excluded here). Shows each digit power and the final verdict.
Algorithm
Goal: decide whether positive n satisfies the Disarium sum.
Count digits k
Know how many positions you need; treat 0 as one digit only if you deliberately support it.
Peel from the right
Let pos start at k. Repeatedly take digit = n % 10, add digitpos, decrement pos, divide n by 10.
Compare
Return whether the accumulated sum equals the original number.
📜 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 Single value: 89
Matches the original tutorial target. Uses IpPow so everything stays in integer arithmetic (you can swap in (int)Math.Pow(digit, digitCount) once you understand the rounding trade-offs).
using System;
class Program
{
static int IpPow(int baseVal, int exp)
{
int result = 1;
for (int i = 0; i < exp; i++)
{
result *= baseVal;
}
return result;
}
static int CountDigits(int number)
{
if (number == 0)
{
return 1;
}
int count = 0;
int n = number;
while (n != 0)
{
count++;
n /= 10;
}
return count;
}
static bool IsDisarium(int number)
{
if (number <= 0)
{
return false;
}
int originalNumber = number;
int digitCount = CountDigits(number);
int sum = 0;
while (number != 0)
{
int digit = number % 10;
sum += IpPow(digit, digitCount);
digitCount--;
number /= 10;
}
return sum == originalNumber;
}
static void Main()
{
int inputNumber = 89;
if (IsDisarium(inputNumber))
{
Console.WriteLine($"{inputNumber} is a Disarium number.");
}
else
{
Console.WriteLine($"{inputNumber} is not a Disarium number.");
}
}
} Explanation
For 89, k = 2. The code meets 9 first and raises it to 2, then raises 8 to 1.
if (number <= 0) return false;Convention on this page: only positive integers follow the classroom definition.
Disarium numbers from 1 to 100
Same output as the reference listing: every single-digit positive integer plus 89.
using System;
class Program
{
static int IpPow(int baseVal, int exp)
{
int result = 1;
for (int i = 0; i < exp; i++)
{
result *= baseVal;
}
return result;
}
static int CountDigits(int num)
{
if (num == 0)
{
return 1;
}
int count = 0;
int n = num;
while (n != 0)
{
n /= 10;
count++;
}
return count;
}
static bool IsDisarium(int num)
{
if (num <= 0)
{
return false;
}
int originalNum = num;
int digitCount = CountDigits(num);
int sum = 0;
while (num != 0)
{
int digit = num % 10;
sum += IpPow(digit, digitCount);
num /= 10;
digitCount--;
}
return sum == originalNum;
}
static void Main()
{
Console.WriteLine("Disarium numbers in the range 1 to 100:");
for (int i = 1; i <= 100; i++)
{
if (IsDisarium(i))
{
Console.Write($"{i} ");
}
}
Console.WriteLine();
}
} Explanation
Single-digit values automatically pass because d1 = d. Among two-digit numbers up to 100, only 89 joins the club.
Optimization
Power table. Bases are only 0–9; caching bp for many checks avoids recomputation.
Bounding sums. For huge ranges you can prune lengths where even the largest possible digit sum still falls short of n.
Interview: define positions from the left; contrast Armstrong; prefer integer powers.
❓ FAQ
🔄 Input / output examples
Change inputNumber in Example 1 or extend the loop 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
The trap is mixing up which digit gets which exponent—everything hinges on counting positions from the left.
CountDigits(0)
Returning 0 would skip the loop and break logic; return 1 if you ever mean to analyse zero.
Floating doubles
Casting Math.Pow to int usually works for tiny inputs but a multiply loop is predictable everywhere.
Large n
Very long numbers may need long for the running sum because digit powers grow quickly.
Different puzzle
Do not describe Disarium using “raise every digit to the power of how many digits there are”—that is Armstrong.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
IsDisarium(n) | About O(d²) with naive IpPow per digit (d = digit count) | O(1) |
Range [1, N] | Linear in N times that cost per value | O(1) |
Caching powers per exponent lowers the per-n work toward O(d).
Summary
- Rule: left-to-right position
p(starting at1) raises that digit top. - Code: peel from the right while an exponent runs from
kdown to1. - Watch-outs: zero digit-count edge case,
Math.Powcasts, Armstrong confusion.
Besides 89, 135 is a famous Disarium example: 11 + 32 + 53 = 1 + 9 + 125 = 135. Every one-digit positive integer 1–9 works because d1 = d.
8 people found this page helpful
