Code Challenge – Armstrong Number

C

Narcissistic Number (or Armstrong Number) is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).

For example, take 153 (3 digits), which is narcissistic:

    1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

and 1652 (4 digits), which isn’t:

    1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938

The Challenge:

Your code must return true or false (not ‘true’ and ‘false’) depending upon whether the given number is a Narcissistic number in base 10.

This may be True and False in your language, e.g. PHP.

Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function.

Solution:

What is Armstrong Number?

An Armstrong number (also known as a narcissistic number) is a positive integer that equals the sum of its own digits, where each digit is raised to the power of the total number of digits in the number. This definition typically applies to numbers in base 10 (decimal system).

Key Characteristics:

  • The “power” used in the calculation is equal to the number of digits in the number.
  • If the sum of the digits raised to that power equals the original number, it’s an Armstrong number.

Examples:

  1. 153 (3 digits):
    • Calculation: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
    • 153 = 153, so it’s an Armstrong number.
  2. 370 (3 digits):
    • Calculation: 3³ + 7³ + 0³ = 27 + 343 + 0 = 370
    • 370 = 370, so it’s an Armstrong number.
  3. 407 (3 digits):
    • Calculation: 4³ + 0³ + 7³ = 64 + 0 + 343 = 407
    • 407 = 407, so it’s an Armstrong number.
  4. 1652 (4 digits):
    • Calculation: 1⁴ + 6⁴ + 5⁴ + 2⁴ = 1 + 1296 + 625 + 16 = 1938
    • 1938 ≠ 1652, so it’s not an Armstrong number.
  5. 1 (1 digit):
    • Calculation: 1¹ = 1
    • 1 = 1, so it’s an Armstrong number.

Single-Digit Armstrong Numbers:

All single-digit numbers (1 through 9) are technically Armstrong numbers because any single digit raised to the power of 1 (number of digits) equals itself.

Interesting Facts:

  • There are only a finite number of Armstrong numbers in base 10.
  • Some well-known Armstrong numbers include: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407.
  • After 407, the next Armstrong numbers are 1634, 8208, and 9474 (4-digit numbers).

def narcissistic(number):
    # Convert number to string to get digits and length
    num_str = str(number)
    num_digits = len(num_str)
    
    # Calculate sum of each digit raised to power of total digits
    total = sum(int(digit) ** num_digits for digit in num_str)
    
    # Return True if sum equals original number, False otherwise
    return total == number

# Test cases
test_cases = [
    1,    # True (1^1 = 1)
    153,  # True (1^3 + 5^3 + 3^3 = 153)
    1652, # False (1^4 + 6^4 + 5^4 + 2^4 = 1938)
    407,  # True (4^3 + 0^3 + 7^3 = 64 + 0 + 343 = 407)
    370,  # True (3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370)
    123   # False (1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36)
]

# Run tests
for test in test_cases:
    print(f"{test} --> {narcissistic(test)}")

How it works:

  1. Converts the number to a string to easily:
    • Get the number of digits with len()
    • Iterate through each digit
  2. Uses a generator expression with sum() to:
    • Convert each digit back to integer with int(digit)
    • Raise it to the power of total digits with ** num_digits
    • Sum all results
  3. Compares the calculated sum with the original number
  4. Returns boolean True or False

Output:

1 --> True 153 --> True 1652 --> False 407 --> True 370 --> True 123 --> False

Alternative solution with explicit loop:

def narcissistic(number): num_str = str(number) num_digits = len(num_str) total = 0 for digit in num_str: total += int(digit) ** num_digits return total == number

Key features:

  • Returns boolean values (True/False, not strings)
  • Works with any positive integer
  • Correctly calculates powers based on number of digits
  • No error checking needed per problem constraints
  • Handles single-digit and multi-digit numbers

The first solution is more concise using Python’s sum() and generator expression, while the second solution might be more readable for beginners showing the accumulation explicitly. Both give identical results.

For example with 153:

  • 3 digits total
  • 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
  • 153 equals 153, so returns True

For 1652:

  • 4 digits total
  • 1⁴ + 6⁴ + 5⁴ + 2⁴ = 1 + 1296 + 625 + 16 = 1938
  • 1938 ≠ 1652, so returns False

About the author

taffwarezz

Add comment