## 1. Overview

In this quick tutorial, we’ll learn what Armstrong numbers are and how to check and find them by creating a Java program.

## 2. Introduction to the Problem

First, let’s understand what an Armstrong number is.

Given a positive integer i of n digits, the integer i is an Armstrong number if the sum of the n-th powers of their digits is equal to i. Armstrong numbers form the OEIS sequence A005188.

A few examples may help us understand Armstrong numbers quickly:

• 1pow(1,1) = 1 -> 1 is an Armstrong number.
• 123: pow(1, 3) + pow(2, 3) + pow(3, 3) = 1 + 8 +27 = 36 != 123 -> 123 is not an Armstrong number.
• 1634: pow(1, 4) + pow(6, 4) + pow(3, 4) + pow(4, 4) = 1 + 1296 + 81 + 256 = 1643 -> 1634 is an Armstrong number.

So, we want to have a Java program to conveniently check if a given number is an Armstrong number. Further, we’d like to produce an OEIS sequence A005188 less than a given limit.

For simplicity, we’ll use unit test assertions to verify whether our methods work as expected.

## 3. The Idea to Solve the Problem

Now that we understand Armstrong numbers let’s look into the problem and consider the idea to solve it.

First, generating an OEIS sequence A005188 with a limit can be translated into going from 0 to the given limit and finding out all Armstrong numbers. If we’ve a method to check if an integer is an Armstrong number, it’s easy to filter out non-Armstrong numbers from the integer range and get the desired sequence.

Thus, the primary problem is to create the Armstrong number check method. A straightforward idea to do the check is a two-step approach:

• step 1 – break the given integer into a digit list, for instance, 12345 -> [1, 2, 3, 4, 5]
• step 2 – for each digit in the list, calculate pow(digit, list.size()), then sum the results, and finally compare the sum to the initially given integer

Next, let’s convert the idea into Java code.

## 4. Creating Armstrong Number Methods

As we’ve discussed, let’s first convert the given integer to a digit list:

``````static List<Integer> digitsInList(int n) {
List<Integer> list = new ArrayList<>();
while (n > 0) {
n = n / 10;
}
return list;
}
``````

As the code above shows, we extract digits from n in a while loop. In each step, we take one digit through n % 10, then shrink the number by n = n / 10.

Alternatively, we can convert the number into a string and use the split() method to get a digit-in-string list. Then, finally, we can convert each digit back to an integer again. Here, we haven’t taken this approach.

Now that we’ve created the check method, we can move to step 2: pow() calculation and sum:

``````static boolean isArmstrong(int n) {
if (n < 0) {
return false;
}
List<Integer> digitsList = digitsInList(n);
int len = digitsList.size();
int sum = digitsList.stream()
.mapToInt(d -> (int) Math.pow(d, len))
.sum();
return n == sum;
}
``````

As we can see in the isArmstrong() check method, we’ve used Java Stream‘s mapToInt() method to turn each digit into the result after the pow() calculation and then sum the results in the list.

Finally, we compared the sum to the initial integer to determine if the number is an Armstrong number.

It’s worth mentioning that we can alternatively combine the mapToInt() and the sum() method calls into one reduce() call:

``````int sum = digits.stream()
.reduce(0, (subtotal, digit) -> subtotal + (int) Math.pow(digit, len));``````

Next, let’s create a method to generate the OEIS sequence A005188 up to a limit:

``````static List<Integer> getA005188Sequence(int limit) {
if (limit < 0) {
throw new IllegalArgumentException("The limit cannot be a negative number.");
}
return IntStream.range(0, limit)
.boxed()
.filter(ArmstrongNumberUtil::isArmstrong)
.collect(Collectors.toList());
}
``````

As we can see in the code above, we’ve used Stream API again to filter Armstrong numbers and generate the sequence.

## 5. Testing

Now, let’s create some tests to verify if our methods work as expected. First, let’s start with some test data:

``````static final Map<Integer, Boolean> ARMSTRONG_MAP = ImmutableMap.of(
0, true,
1, true,
2, true,
153, true,
370, true,
407, true,
42, false,
777, false,
12345, false);
``````

Now, let’s pass each number in the Map above to our check method and see if returns the expected result:

``````ARMSTRONG_MAP.forEach((number, result) -> assertEquals(result, ArmstrongNumberUtil.isArmstrong(number)));
``````

If we run the test, it passes. So, the check method does the job correctly.

Next, let’s prepare two expected sequences and test if getA005188Sequence() works as expected too:

``````List<Integer> A005188_SEQ_1K = ImmutableList.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407);
List<Integer> A005188_SEQ_10K = ImmutableList.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474);

assertEquals(A005188_SEQ_1K, ArmstrongNumberUtil.getA005188Sequence(1000));
assertEquals(A005188_SEQ_10K, ArmstrongNumberUtil.getA005188Sequence(10000));
``````

Our test passes if we give it a run.

## 6. Conclusion

In this article, we’ve discussed what an Armstrong number is. Further, we’ve created methods to check if an integer is an Armstrong number and generate OEIS sequence A005188 up to a given limit.

As usual, all code snippets presented here are available over on GitHub.

### Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE