Avatar of artemkorsakov

artemkorsakov's solution

to Armstrong Numbers in the C# Track

Published at Feb 08 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.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit ArmstrongNumbers.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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.cs

// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;

public class ArmstrongNumbersTest
{
    [Fact]
    public void Single_digit_numbers_are_armstrong_numbers()
    {
        Assert.True(ArmstrongNumbers.IsArmstrongNumber(5));
    }

    [Fact(Skip = "Remove to run test")]
    public void There_are_no_2_digit_armstrong_numbers()
    {
        Assert.False(ArmstrongNumbers.IsArmstrongNumber(10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Three_digit_number_that_is_an_armstrong_number()
    {
        Assert.True(ArmstrongNumbers.IsArmstrongNumber(153));
    }

    [Fact(Skip = "Remove to run test")]
    public void Three_digit_number_that_is_not_an_armstrong_number()
    {
        Assert.False(ArmstrongNumbers.IsArmstrongNumber(100));
    }

    [Fact(Skip = "Remove to run test")]
    public void Four_digit_number_that_is_an_armstrong_number()
    {
        Assert.True(ArmstrongNumbers.IsArmstrongNumber(9474));
    }

    [Fact(Skip = "Remove to run test")]
    public void Four_digit_number_that_is_not_an_armstrong_number()
    {
        Assert.False(ArmstrongNumbers.IsArmstrongNumber(9475));
    }

    [Fact(Skip = "Remove to run test")]
    public void Seven_digit_number_that_is_an_armstrong_number()
    {
        Assert.True(ArmstrongNumbers.IsArmstrongNumber(9926315));
    }

    [Fact(Skip = "Remove to run test")]
    public void Seven_digit_number_that_is_not_an_armstrong_number()
    {
        Assert.False(ArmstrongNumbers.IsArmstrongNumber(9926314));
    }
}
using System;
using System.Linq;

public static class ArmstrongNumbers
{
    public static bool IsArmstrongNumber(int number)
    {
        var nums = number.ToString().ToCharArray();
        return Math.Abs(nums.Sum(n => Math.Pow(char.GetNumericValue(n), nums.Length)) - number) < 0.01;
    }
}

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?