Check Number Disarium in Java
Problem
A Disarium number is a number where each digit raised to the power of its position (counted from the left, starting at 1) sums back up to the number itself.
Given a number, determine whether it is a Disarium number.
Java Program
public class DisariumNumberCheck {
public static void main(String[] args) {
int n = 135;
String digits = String.valueOf(n);
long sum = 0;
for (int i = 0; i < digits.length(); i++) {
int digit = digits.charAt(i) - '0';
int position = i + 1;
sum += Math.round(Math.pow(digit, position)); // guards against floating-point drift
}
System.out.println(n + " is a Disarium number: " + (sum == n));
}
}Output
Core Logic
Converting the number to a string exposes both its digit count and each digit's left-to-right position, which is exactly what raising each digit to its own position needs.
- 1
String.valueOf(n)gives access to each digit character and its index. - 2The loop runs
ifrom0to the string's length, treatingi + 1as that digit's 1-indexed position. - 3Each digit is raised to the power of its position with
Math.pow(), rounded to the nearest whole number, and added intosum. - 4Once every digit has been processed,
sumis compared against the originaln.
135: 1^1 = 1, 3^2 = 9, and 5^3 = 125 — summing to 135, which matches the original number.Key Point: Math.round() matters here — Math.pow() returns a double, and its floating-point result can drift slightly away from the exact integer value it's meant to represent.
Why: The loop computes one power per digit, and Math.pow()'s own cost doesn't grow with how large n itself is.
Key Concepts
Approach 2: Java 8
import java.util.stream.IntStream;
public class DisariumNumberCheckStream {
public static void main(String[] args) {
int n = 135;
String digits = String.valueOf(n);
// Streams the index range so each digit's position stays available inside the mapping step
long sum = IntStream.range(0, digits.length())
.mapToLong(i -> Math.round(Math.pow(digits.charAt(i) - '0', i + 1)))
.sum();
System.out.println(n + " is a Disarium number: " + (sum == n));
}
}
Output
Core Logic
Streaming the digit string's indices, instead of looping over them by hand, lets each index double as both the digit's position and the lookup into the string.
- 1
String.valueOf(n)gives the digit string, same as the manual version. - 2
IntStream.range(0, digits.length())produces every valid index into that string. - 3
.mapToLong(i -> Math.round(Math.pow(digits.charAt(i) - '0', i + 1)))reads the digit at indexi, raises it to the power of its 1-indexed position, and rounds away floating-point drift, all inside the mapping step. - 4
.sum()totals every mapped power into the final sum.
135, the index stream maps to 1^1 = 1, 3^2 = 9, and 5^3 = 125, summing to 135.Key Point: Streaming the index range, rather than the characters directly, is what keeps each digit's position available inside the lambda — a plain chars() stream would lose track of where each digit sits.
Why: The stream computes one power per digit just like the loop, generating indices on demand without extra storage.