Check Disarium Number in Java
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 (no floating-point
Math.pow), 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 the reference output.
Live preview
Try small positives in the browser with the same sum rule.
Fixes vs reference
countDigits(0) and integer ipow instead of floating-point power functions.
Prerequisites
Digit extraction with % 10, integer division, and loops in Java.
What is a Disarium number?
Write the decimal digits of n as d1d2...dk from left to right. Then n is Disarium if n = d1^1 + d2^2 + ... + dk^k.
This is not the same as Armstrong (narcissistic) numbers, which use a fixed exponent equal to the digit count for every digit.
Formal sum
For digits d1...dk from left to right, n is Disarium iff n = d1^1 + d2^2 + ... + dk^k.
Intuition and examples
89 = 8^1 + 9^2 works, while 10 = 1^1 + 0^2 = 1 does not.
Live preview
📜 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 = nSingle value: 89
public class Main {
static int ipow(int base, int exp) {
int r = 1;
for (int i = 0; i < exp; i++) {
r *= base;
}
return r;
}
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 boolean 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 += ipow(digit, digitCount);
digitCount--;
number /= 10;
}
return sum == originalNumber;
}
public static void main(String[] args) {
int inputNumber = 89;
if (isDisarium(inputNumber)) {
System.out.println(inputNumber + " is a Disarium number.");
} else {
System.out.println(inputNumber + " is not a Disarium number.");
}
}
}Disarium numbers from 1 to 100
public class Main {
static int ipow(int base, int exp) {
int r = 1;
for (int i = 0; i < exp; i++) {
r *= base;
}
return r;
}
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 boolean 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 += ipow(digit, digitCount);
num /= 10;
digitCount--;
}
return sum == originalNum;
}
public static void main(String[] args) {
System.out.println("Disarium numbers in the range 1 to 100:");
for (int i = 1; i <= 100; i++) {
if (isDisarium(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
}Optimization
Precompute powers for digits 0..9 by position when scanning large ranges.
❓ FAQ
🔄 Input / output examples
89 -> Disarium, 135 -> Disarium, 10 -> not Disarium.
Edge cases
Zero and negatives are excluded by this tutorial's positive-number convention.
⏱️ Time and space complexity
Per number complexity is linear in digit count with constant extra space.
Summary
- Disarium uses positional powers from left to right.
- Java implementation peels digits and tracks exponent index.
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
