Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Armstrong Numbers in the Java Track

Published at May 18 2020 · 0 comments
Instructions
Test suite
Solution

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

  • 9 is an Armstrong number, because 9 = 9^1 = 9
  • 10 is not an Armstrong number, because 10 != 1^2 + 0^2 = 1
  • 153 is an Armstrong number, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
  • 154 is not an Armstrong number, because: 154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190

Write some code to determine whether a number is an Armstrong number.

Tips

For more help on how to solve this exercise, please refer to the tutorial provided as part of the hello world exercise: TUTORIAL.md

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Wikipedia https://en.wikipedia.org/wiki/Narcissistic_number

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ArmstrongNumbersTest.java

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

public class ArmstrongNumbersTest {

    private ArmstrongNumbers armstrongNumbers;

    @Before
    public void setup() {
        armstrongNumbers = new ArmstrongNumbers();
    }

    @Test
    public void zeroIsArmstrongNumber() {
        int input = 0;

        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void singleDigitsAreArmstrongNumbers() {
        int input = 5;

        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void noTwoDigitArmstrongNumbers() {
        int input = 10;

        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void threeDigitNumberIsArmstrongNumber() {
        int input = 153;
        
        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void threeDigitNumberIsNotArmstrongNumber() {
        int input = 100;
        
        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void fourDigitNumberIsArmstrongNumber() {
        int input = 9474;
        
        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void fourDigitNumberIsNotArmstrongNumber() {
        int input = 9475;
        
        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void sevenDigitNumberIsArmstrongNumber() {
        int input = 9926315;
        
        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Ignore("Remove to run test")
    @Test
    public void sevenDigitNumberIsNotArmstrongNumber() {
        int input = 9926314;
        
        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

class ArmstrongNumbers {

    boolean isArmstrongNumber(int numberToCheck) {
        List<Integer> digits = getDigits(numberToCheck);
        return numberToCheck == sumOfDigitsRaisedToNumberOfDigits(digits);
    }

    private static double sumOfDigitsRaisedToNumberOfDigits(List<Integer> digits) {
        return digits.stream().map(digit -> {
            return Math.pow(digit, digits.size());
        }).collect(Collectors.summingDouble(d -> d));
    }

    /**
     * Split an integer into its individual digits
     * NOTE: digits order is maintained - i.e. Least significant digit is at index[0]
     * @param num positive integer
     * @return array of digits
     */
    private static List<Integer> getDigits(int num) {
        if (num < 0) { return new ArrayList<>(0); }
        List<Integer> digits = new ArrayList<Integer>();
        collectDigits(num, digits);
        Collections.reverse(digits);
        return digits;
    }

    private static void collectDigits(int num, List<Integer> digits) {
        if(num / 10 > 0) {
            collectDigits(num / 10, digits);
        }
        digits.add(num % 10);
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?