Algorithm LogbookAlgorithm Logbook

Armstrong Number

Month 1Week 2Day 3

๐Ÿ“‹ Problem Statement

An Armstrong number (also called a Narcissistic number) of N digits is a number where the sum of each digit raised to the power of N equals the number itself. For example, 153 is Armstrong because 1^3 + 5^3 + 3^3 = 153. Check if a given number is an Armstrong number.

๐Ÿ’ก Intuition

This is another application of the digit extraction pattern. First, count how many digits the number has (this determines the power N). Then extract each digit, raise it to the Nth power, and sum them up. If the sum equals the original number, it is an Armstrong number. The key insight is making the power dynamic based on digit count, not hardcoded to 3.

๐Ÿ”ง Approach

1. Count the number of digits using a helper function (repeatedly divide by 10 and count). 2. Store the original number. 3. Extract each digit using % 10, raise it to the power of num_digits, and add to a running sum. 4. Remove the digit with // 10. Repeat until the number is 0. 5. Compare the sum with the original โ€” if equal, it is an Armstrong number.

โšก Complexity Analysis

Time
O(log N) where N is the value of the number โ€” we process each digit once (twice with digit counting)
Space
O(1) โ€” only a few integer variables

โš ๏ธ Common Mistakes

- Hardcoding the power to 3, which only works for 3-digit numbers (fails for 1634, a 4-digit Armstrong number: 1^4 + 6^4 + 3^4 + 4^4 = 1634) - Forgetting to preserve the original number before extracting digits - Not handling 0 correctly as an edge case (0 is technically an Armstrong number: 0^1 = 0) - Negative numbers should return False

๐ŸŽฏ Final Thoughts

Armstrong number checking reinforces the digit extraction loop and adds the concept of dynamic power based on digit count. The helper function for counting digits is a good exercise in code modularity. Some known Armstrong numbers: 0, 1, 153, 370, 371, 407, 1634, 8208, 9474.