Avatar of artemkorsakov

artemkorsakov's solution

to Armstrong Numbers in the Java Track

Published at Jan 29 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

Java 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

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

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 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.Arrays;

class ArmstrongNumbers {

    boolean isArmstrongNumber(int numberToCheck) {
        String[] nums = String.valueOf(numberToCheck).split("");
        return (numberToCheck > 0) && (Arrays.stream(nums).mapToInt(n -> (int) Math.pow(Integer.parseInt(n), nums.length)).sum() == numberToCheck);
    }
    
}

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?