An Armstrong number has sum of its digits raised to the power of number of digits
equals the number. i.e. An n-digit number equal to the sum of the nth powers of its digits.
The following example deals with very big Armstrong numbers. The 39-digit mammoth number
115132219018763992565095597973971522401 is known to be the
biggest Armstrong number.
/**
* An Armstrong number has sum of digits raised to the power of number of digits
* equals the number
*
* @author Sudhakar KV
*
*/ public class BigArmstrongNumber {
/**
* The largest Armstrong number (in base 10) is the
* 39-digit beast: 115132219018763992565095597973971522401
*/
BigInteger number = new BigInteger("115132219018763992565095597973971522401");
isArmstrong(number);
}
private static int getNumOfDigits(BigInteger number) { return String.valueOf(number).length();
}
private static boolean isArmstrong(BigInteger number) {
BigInteger orgNumber = new BigInteger(number.toString());
BigInteger sum = BigInteger.ZERO; int digit = 0;
BigInteger digitPow = BigInteger.ZERO; int numOfDigits = getNumOfDigits(number);
List<Integer> digits = new ArrayList<Integer>();
List<BigInteger> digitPows = new ArrayList<BigInteger>();
while (number.compareTo(BigInteger.ZERO) > 0) {
digit = number.mod(new BigInteger("10")).intValue();
digitPow = new BigInteger(String.valueOf(digit)).pow(numOfDigits);
sum = sum.add(new BigInteger(String.valueOf(digitPow)));
number = number.divide(new BigInteger("10"));