Skip to main content

Disarium Number in Java: Program, Algorithm and All Disarium Numbers from 1 to 1000

A Disarium number is a positive integer whose digits, raised to their positions from the left, add up to the number itself (175 = 1^1 + 7^2 + 5^3). Definition, a dry run of 175, three Java programs that all run and agree (loop, String, Streams), all 14 Disarium numbers from 1 to 1000, the count up to 10^8, and viva questions with answers.

Quick answer. A Disarium number is a positive integer whose digits, each raised to the power of its position counted from the left, add up to the number itself. For example 175 = 11 + 72 + 53 = 1 + 49 + 125.

All Disarium numbers from 1 to 1000 (14 of them):
1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598

I computed this list by running the code below, not by recalling it: see output/02-list-1-to-1000.txt. Going further, a brute-force run over every int up to 108 finds just 18 in total; the next ones after 598 are 1306, 1676, 2427, 2646798 (output/06-count-to-10-pow-8.txt).

If you are preparing for a college viva or a coding round, this page gives you the definition, a step-by-step dry run, the algorithm, three Java approaches that all run and agree (a digit loop, a String version and a Streams version), the full list, and the answers to the questions examiners actually ask. If you are new to digit programs in general, the number programs hub (reverse, palindrome, Armstrong, perfect, strong) is a good warm-up.

Tested on: JDK 25.0.5 (Temurin, LTS), Maven 3.9.11, JUnit 6.1.3. Every program, number and console line below comes from the companion repository, java-interview-lab / disarium-number, and is regenerated by scripts/run-all.sh.

What is a Disarium number?

A positive integer N is called Disarium when this 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. The only thing to remember is: the power is the position, and positions are counted from the left, starting at 1. Do not confuse it with an Armstrong number, where every digit gets the same power.

position 1position 2position 3 1 7 5 1^1 = 1 7^2 = 49 5^3 = 125 ++ 1 + 49 + 125 = 175 same as the input

The picture shows 175 split into its digits. The leftmost digit gets power 1, the next gets power 2, the last gets power 3, and the three results add back up to 175, which is exactly why 175 qualifies. A number that fails (like 24) simply adds up to something else.

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 ✗

Dry run table: 175, step by step

This is the table to write in an exam answer. Take 175 and go digit by digit from the left:

PositionDigitDigitpositionValueRunning sum
111111
27724950
3553125175

Final sum 175 equals the original number, so 175 is a Disarium number. The program prints the same dry run for several numbers (including failures); the dryRun method is in Disarium.java and the transcript is output/03-dry-run.txt:

175 = 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175  -> Disarium
89 = 8^1 + 9^2 = 8 + 81 = 89  -> Disarium
135 = 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135  -> Disarium
24 = 2^1 + 4^2 = 2 + 16 = 18  -> NOT Disarium (18 != 24)
100 = 1^1 + 0^2 + 0^3 = 1 + 0 + 0 = 1  -> NOT Disarium (1 != 100)

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(log10 N), one iteration per digit.
Space complexity: O(1), only a handful of integer variables.

Why do we start from the right when positions are counted from the left? Because n % 10 peels digits off the right end. The trick is to count the digits first: the rightmost digit sits at position len, and every step to the left reduces the position by one.

Java program: approach 1, the digit loop

This is the program from the original version of this post (kept, with its comments), from isDisariumOriginal in Disarium.java:

    /** The program from the original post, kept as-is (uses Math.pow). */
    public static boolean isDisariumOriginal(int number) {
        int original = number;
        int len = countDigits(number);
        int sum = 0;
        int n = number;
        while (n > 0) {
            int digit = n % 10;
            sum += (int) Math.pow(digit, len);
            len--;
            n /= 10;
        }
        return sum == original;
    }

    public static int countDigits(int n) {
        int count = 0;
        while (n > 0) {
            count++;
            n /= 10;
        }
        return count;
    }

The old version of this page had its main method cut off mid-line (the stored code stopped at for (int i = 1; i), so the program could not be copied and run. The repository now has the complete runnable Main class; its check mode runs the same seven sample checks and the series printer for 1 to 1000. Here is the real output, from output/01-check-and-series.txt:

  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

The code that produced the series line is upTo:

    public static List<Integer> upTo(int limit) {
        return IntStream.rangeClosed(1, limit).filter(Disarium::isDisariumLoop).boxed().toList();
    }

That is source from Disarium.java, called by Main.java.

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 rightmost digit.
  2. isDisarium(): iterates from the rightmost digit to the leftmost. The rightmost digit always has the highest position (equal to the digit count), so decrementing len on each iteration assigns the correct position to each digit.
  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 up to 7) there is no precision loss; the test in the repo compares this version with an exact integer version for every number up to 1,000,000 and finds no difference.
  4. printDisariumSeries() / upTo(): calls the check for every integer in [1, limit] and keeps those that qualify.
Correction to the old text. The earlier intro said the program prints all Disarium numbers “up to 100,000”, but its main asked for 1000. The program here prints up to 1000, and the 108 run is a separate mode (count).

Approach 2: use a String

If you find n % 10 confusing, convert the number to text. Now the digits are already in left-to-right order, so the position is simply index + 1 and there is no digit counting at all. This is often the version students find easiest to explain in a viva. From Disarium.java:

    /** Approach 2: String of digits, left to right, position = index + 1. */
    public static boolean isDisariumString(int number) {
        if (number <= 0) return false;
        String s = Integer.toString(number);
        long sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += intPow(s.charAt(i) - '0', i + 1);
        }
        return sum == number;
    }

    static long intPow(int base, int exp) {
        long r = 1;
        for (int i = 0; i < exp; i++) r *= base;
        return r;
    }

intPow multiplies in a loop instead of calling Math.pow, so no double and no cast are involved, and the sum is a long.

Approach 3: Java Streams

The same idea as a one-expression pipeline. IntStream.range(0, s.length()) gives the indexes; each index becomes digit^(index+1); sum() adds them up. From Disarium.java:

    /** Approach 3: Streams over the characters of the String. */
    public static boolean isDisariumStream(int number) {
        if (number <= 0) return false;
        String s = Integer.toString(number);
        long sum = IntStream.range(0, s.length())
                .mapToLong(i -> intPow(s.charAt(i) - '0', i + 1))
                .sum();
        return sum == number;
    }

For the idea behind mapToLong and friends, see Java Streams API: The Complete Reference Guide. Streams are shorter but not faster here; in an interview, say that the loop is the one you would write first.

Do all the approaches agree?

I did not want to trust any of them on faith, so Main approaches compares the original Math.pow version, the integer loop, the String version, the stream version and a log10 variant for every number from 1 to 1,000,000. Output from output/04-approaches-agree.txt:

compared original, loop, String, stream, log10 for 1..1000000: disagreements=0
Going deeper: counting digits with log10 instead of a loop

You will see this in other tutorials: (int) Math.log10(n) + 1 gives the number of digits of a positive integer, so no counting loop is needed. It is a one-liner, but it goes through floating point, so it deserves a check at the dangerous spots: numbers right next to powers of ten (9, 10, 99, 100, and so on). From Disarium.java:

    /** Digit count via log10 instead of a loop (used by the maths variant). */
    public static int digitsLog10(int n) {
        return (int) Math.log10(n) + 1;
    }

    /** Approach 1b: same as the loop, but the digit count comes from log10. */
    public static boolean isDisariumLog10(int number) {
        if (number <= 0) return false;
        int len = digitsLog10(number);
        long sum = 0;
        for (int n = number; n > 0; n /= 10, len--) {
            sum += intPow(n % 10, len);
        }
        return sum == number;
    }

I compared digitsLog10 with the loop-based countDigits for every value within two of each power of ten up to 109, and the output (output/05-log10-digit-count.txt) was:

log10 vs loop digit count around every power of ten: mismatches=0

So within the int range it works, but this only covers the values I tested; the digit-counting loop needs no such argument, which is why the main version uses it. Never call Math.log10(0) or on a negative number: you get negative infinity or NaN, which is why every method here rejects number <= 0 first.

Going deeper: why Math.pow is fine here, and why I still use long

Math.pow works on double. Every power here is at most 910 (about 3.5 billion), well below the 253 limit where a double stops being able to represent every integer, so casting back to int is exact for the numbers tested. The integer versions use a long sum as a precaution so a large input cannot overflow silently; they are also the right base if you later switch the parameter to long.

Going deeper: why are Disarium numbers so rare?

A d-digit number is at least 10d-1, while the biggest possible digit-power sum is 91 + 92 + … + 9d. The sum can only reach the number while it grows fast enough, and 10d eventually outpaces it, so very large Disarium numbers cannot exist. That is a bound argument, not something I tested; what I did run is the brute force below, which stops at 108.

Sample output: all Disarium numbers from 1 to 1000

The series printed by the program (from output/02-list-1-to-1000.txt):

count=14
1 2 3 4 5 6 7 8 9 89 135 175 518 598

Notice that 1 to 9 are there for free, and then there are only five more below 1000: 89, 135, 175, 518 and 598. Between 599 and 1000 there are none. Each of those five can be checked by hand with the dry run above.

Complete list of known Disarium numbers

The earlier version of this post said the list up to 10,000,000 is remarkably short. That was right, and now it is backed by a run. Main count checks every int up to 108 and prints the running total at each power of ten (this takes about two minutes on a shared 2-vCPU sandbox; your timing will differ). From output/06-count-to-10-pow-8.txt:

up to 10^1: 9
up to 10^2: 10
up to 10^3: 14
up to 10^4: 17
up to 10^5: 17
up to 10^6: 17
up to 10^7: 18
up to 10^8: 18
found at or above 1000: 1306 1676 2427 2646798

1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598, 1306, 1676, 2427, 2646798. That is 18 numbers up to 108, and 17 up to 106. The jump from 2427 to 2646798 is the reason a naive search of the first few million numbers looks like it has stopped finding anything.

Tests

The repository pins all of this with DisariumTest.java: the 14-number list, agreement of all approaches up to 200,000, the two dry-run strings, rejection of 0 and negative inputs, and two large cases. Result, from output/07-test-results.txt:

PASS DisariumTest.allApproachesAgreeUpTo200000
PASS DisariumTest.dryRunOf175
PASS DisariumTest.dryRunOf24
PASS DisariumTest.zeroAndNegativesAreNotDisarium
PASS DisariumTest.biggerKnownOnes
PASS DisariumTest.listUpTo1000
6 tests, 0 failures

One honest note from writing this: my first draft of the large-number test asserted that 12157692 was Disarium, because I half-remembered it from somewhere. The test failed, and the brute-force run confirms it is not. Only 2646798 is in the list after 2427.

Frequently asked questions

What is a Disarium number?

A positive integer whose digits, each raised to the power of its position counted from the left, add up to the number itself. 175 is one: 11 + 72 + 53 = 1 + 49 + 125 = 175.

How many Disarium numbers are there between 1 and 1000?

Fourteen. The program in this post counts them: 1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518 and 598.

Is 0 a Disarium number?

By convention Disarium numbers are positive integers, so 0 is excluded. The code in this post returns false for 0 and for negative numbers.

Are single-digit numbers always Disarium?

Yes. For a single digit d the formula reduces to d1 = d, which is always true, so 1 to 9 are all Disarium numbers.

Is a Disarium number the same as an Armstrong number?

No. In an Armstrong number every digit is raised to the same power, the digit count (153 = 13 + 53 + 33). In a Disarium number the power is the digit’s position, so it grows from left to right.

Can the program handle large numbers?

The code here takes an int and sums in a long. For values beyond Integer.MAX_VALUE (2,147,483,647) change the parameter type to long. For arbitrary size use BigInteger. I brute-forced every int up to 108 and found 18 Disarium numbers in total.

See also

Fixed links. The old “See Also” list pointed to separate Armstrong, Perfect and Neon posts, but those URLs return a 404 on this site. The Armstrong, perfect and strong number programs live together in the number programs hub linked above, so I linked that instead.

Conclusion

Disarium numbers are a compact exercise in digit extraction, positional maths and loop control. The loop version runs in O(log N) time and O(1) space, which is what an examiner wants to hear; the String and Streams versions say the same thing in different words, and the repository shows that all of them agree. For practice, try changing the parameter to long, or write the same check as a Predicate<Integer>.

Should you memorise the list? No. Memorise the definition and the 175 dry run; the list up to 1000 is short enough to rebuild in an exam by testing 3-digit candidates, and a program that prints it is worth more than the list itself.

No Comments yet!

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