🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of jlacar

jlacar's solution

to Armstrong Numbers in the Java Track

Published at May 03 2019 · 1 comment
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

Use gradlew.bat if you're on Windows

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));
    }

}

src/main/java/ArmstrongNumbers.java

import java.util.ArrayList;
import java.util.List;
import java.util.function.IntUnaryOperator;
import java.util.stream.IntStream;

import static java.lang.Math.abs;
import static java.lang.Math.pow;

class ArmstrongNumbers {

	boolean isArmstrongNumber(int numberToCheck) {
		if (numberToCheck < 0) return false;
		return numberToCheck == armstrongSum(numberToCheck);
	}

	private int armstrongSum(int number) {
		String digits = String.valueOf(number);

		return digits.chars()
				.map(Character::getNumericValue)
				.map(eachRaisedTo(digits.length()))
				.sum();
	}

	private IntUnaryOperator eachRaisedTo(int n) {
		return i -> (int) pow(i, n);
	}

	/* ~~~~~ Alternative solutions ~~~~~ */

	/* Equivalent of the above eachRaisedTo() */
	private IntUnaryOperator eachRaisedTo_IntStream_Impl(int n) {
		return i -> IntStream.range(0, n).reduce(1, (raised, notUsed) -> raised * i);
	}

	/* Equivalent of the above eachRaisedTo_IntStream_Impl() */
	private IntUnaryOperator eachRaisedTo_forLoop_Impl(int n) {
		return i -> {
			int raised = 1;
			for (int power = 0; power < n; i++) {
				raised *= i;
			}
			return raised;
		};
	}

	/* If you don't want to use a String */
	private int armstrongSum_Integer(int number) {
		List<Integer> digits = digitsIn(number);
		return digits.stream()
				.mapToInt(eachRaisedTo(digits.size())::applyAsInt)
				.sum();
	}

	private List<Integer> digitsIn(int number) {
		List<Integer> digits = new ArrayList<>();

		if (number == 0) {
			digits.add(0);
		}

		int n = number;
		while (n != 0) {
			digits.add(n % 10);
			n /= 10;
		}

		return digits;
	}
}

src/test/java/ArmstrongNumbersTest.java

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

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class ArmstrongNumbersTest {

    private ArmstrongNumbers armstrongNumbers;

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

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

        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void singleDigitsAreArmstrongNumbers() {
        int input = 5;

        assertThat(armstrongNumbers.isArmstrongNumber(input), is(true));
    }

    @Test
    public void noTwoDigitArmstrongNumbers() {
        int input = 10;

        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void threeDigitNumberIsArmstrongNumber() {
        int input = 153;
        
        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void threeDigitNumberIsNotArmstrongNumber() {
        int input = 100;
        
        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void fourDigitNumberIsArmstrongNumber() {
        int input = 9474;
        
        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void fourDigitNumberIsNotArmstrongNumber() {
        int input = 9475;
        
        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void sevenDigitNumberIsArmstrongNumber() {
        int input = 9926315;
        
        assertTrue(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void sevenDigitNumberIsNotArmstrongNumber() {
        int input = 9926314;
        
        assertFalse(armstrongNumbers.isArmstrongNumber(input));
    }

    @Test
    public void should_be_positive() throws Exception {
        assertThat(armstrongNumbers.isArmstrongNumber(-9926315), is(false));
    }

}

Community comments

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

Note: Implementation that is actually executed is only up to line 27. I have included alternative implementations for comparison. I also added a test for negative numbers, which I'm assuming are not Armstrong numbers.

(edited over 1 year ago)

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?