Disarium Number in Java – Definition, Algorithm, and Complete Program

A Disarium number is a positive integer in which the sum of each digit raised to the power of its position (1-indexed from the left) equals the number itself. For example, 135 qualifies because 1¹ + 3² + 5³ = 1 + 9 + 125 = 135. In this tutorial you will learn exactly what makes a number Disarium, work through a manual verification, study a clean single-pass Java algorithm, and run a complete program that checks individual numbers and prints all Disarium numbers up to 100,000.

What Is a Disarium Number?

A positive integer N is called Disarium when the following formula holds:

d₁¹ + d₂² + d₃³ + … + dₙⁿ = N

where d₁, d₂, … dₙ are the digits of N from left to right and n is the total number of digits.

Quick Verification Examples

  • 135 → 1¹ + 3² + 5³ = 1 + 9 + 125 = 135 ✔
  • 89 → 8¹ + 9² = 8 + 81 = 89 ✔
  • 175 → 1¹ + 7² + 5³ = 1 + 49 + 125 = 175 ✔
  • 518 → 5¹ + 1² + 8³ = 5 + 1 + 512 = 518 ✔
  • 24 → 2¹ + 4² = 2 + 16 = 18 ≠ 24 ✗

Algorithm

  1. Store the original number in original.
  2. Count the total digits of the number and store in len.
  3. Initialise sum = 0 and a working copy n = original.
  4. Loop while n > 0:
    a. Extract the last digit: digit = n % 10.
    b. Add digit<sup>len</sup> to sum.
    c. Decrement len by 1.
    d. Remove the last digit: n = n / 10.
  5. After the loop: if sum == original the number is Disarium.

Time complexity: O(log₁₀ N) — one iteration per digit.
Space complexity: O(1) — only a handful of integer variables.

Java Program

public class DisariumNumber {

    // Returns the number of digits in n
    static int countDigits(int n) {
        int count = 0;
        while (n > 0) {
            count++;
            n /= 10;
        }
        return count;
    }

    // Returns true if number is a Disarium number
    static boolean isDisarium(int number) {
        int original = number;
        int len      = countDigits(number);
        int sum      = 0;
        int n        = number;

        while (n > 0) {
            int digit = n % 10;                    // extract rightmost digit
            sum += (int) Math.pow(digit, len);     // digit raised to its position
            len--;                                 // move position left
            n   /= 10;                             // drop the rightmost digit
        }
        return sum == original;
    }

    // Prints all Disarium numbers from 1 to limit
    static void printDisariumSeries(int limit) {
        System.out.print("Disarium numbers up to " + limit + ": ");
        for (int i = 1; i   %s%n",
                num, isDisarium(num) ? "Disarium" : "NOT Disarium");
        }

        System.out.println();

        // Print series
        printDisariumSeries(1000);
    }
}

How the Code Works

  1. countDigits() — divides the number by 10 repeatedly, counting iterations, to determine how many digits it has. This count becomes the starting power for the leftmost digit.
  2. isDisarium() — iterates from the rightmost digit to the leftmost. Because the right-most digit always receives the lowest remaining power (equal to its 1-based position from the left), decrementing len on each iteration correctly assigns positions.
  3. Math.pow(digit, len) — computes digitposition. The result is cast to int because Math.pow returns a double. For the sizes involved (single digits, positions ≤ 7) there is no precision loss.
  4. printDisariumSeries() — calls isDisarium() for every integer in [1, limit] and prints those that qualify.

Sample Output

  89  ->  Disarium
 135  ->  Disarium
 175  ->  Disarium
 518  ->  Disarium
 598  ->  Disarium
  24  ->  NOT Disarium
 100  ->  NOT Disarium

Disarium numbers up to 1000: 1 2 3 4 5 6 7 8 9 89 135 175 518 598

Complete List of Known Disarium Numbers

Running printDisariumSeries(10_000_000) reveals that Disarium numbers are remarkably rare:

1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427, 2646798

Frequently Asked Questions

Is 0 a Disarium number?

By convention, Disarium numbers are defined for positive integers only, so 0 is excluded.

Are single-digit numbers always Disarium?

Yes. For any single digit d, the formula reduces to d¹ = d, which is always true. So 1 through 9 are all Disarium numbers.

Can the algorithm handle large numbers?

For numbers beyond Integer.MAX_VALUE (2,147,483,647) switch the parameter type to long. Powers of large digits can overflow int quickly, so use Math.pow carefully or switch to BigInteger for arbitrary precision.

See Also

Conclusion

Disarium numbers offer a concise yet instructive programming exercise that sharpens your skills in digit extraction, positional mathematics, and loop control. The isDisarium() helper shown above runs in O(log N) time and O(1) space — ideal for coding-interview scenarios or competitive programming warm-ups. Try extending it to accept long input, or challenge yourself to find Disarium numbers using Java Streams for a functional programming twist.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.