Go Program to Check Armstrong Number
sum += int(math.Pow(float64(digit), float64(numDigits))) inside a loop.Examples
How to Think About It
Algorithm
Code
package main import ( "fmt" "math" ) func main() { number := 153 numDigits := int(math.Log10(float64(number))) + 1 sum := 0 n := number for n > 0 { digit := n % 10 sum += int(math.Pow(float64(digit), float64(numDigits))) n /= 10 } if sum == number { fmt.Printf("%d is an Armstrong number\n", number) } else { fmt.Printf("%d is not an Armstrong number\n", number) } }
Dry Run
Let's trace the number 153 through the code to check if it is an Armstrong number.
Calculate number of digits
number = 153, numDigits = 3 (because 153 has 3 digits)
Initialize sum and start loop
sum = 0, n = 153
First digit calculation
digit = 153 % 10 = 3, sum += 3^3 = 27, sum = 27, n = 153 / 10 = 15
Second digit calculation
digit = 15 % 10 = 5, sum += 5^3 = 125, sum = 152, n = 15 / 10 = 1
Third digit calculation
digit = 1 % 10 = 1, sum += 1^3 = 1, sum = 153, n = 1 / 10 = 0
Compare sum with original number
sum = 153, number = 153, they are equal
| Digit | Digit^numDigits | Sum after addition |
|---|---|---|
| 3 | 27 | 27 |
| 5 | 125 | 152 |
| 1 | 1 | 153 |
Why This Works
Step 1: Count digits
We use math.Log10 to find how many digits the number has, which is needed to know the power to raise each digit.
Step 2: Sum of powers
Each digit is raised to the power of the digit count using math.Pow, then added to a running sum.
Step 3: Compare sum and number
If the sum equals the original number, it means the number is an Armstrong number.
Alternative Approaches
package main import ( "fmt" "math" "strconv" ) func main() { number := 9474 numStr := strconv.Itoa(number) numDigits := len(numStr) sum := 0 for _, ch := range numStr { digit := int(ch - '0') sum += int(math.Pow(float64(digit), float64(numDigits))) } if sum == number { fmt.Printf("%d is an Armstrong number\n", number) } else { fmt.Printf("%d is not an Armstrong number\n", number) } }
package main import ( "fmt" "math" ) func powerSum(n, digits int) int { if n == 0 { return 0 } digit := n % 10 return int(math.Pow(float64(digit), float64(digits))) + powerSum(n/10, digits) } func main() { number := 153 numDigits := int(math.Log10(float64(number))) + 1 sum := powerSum(number, numDigits) if sum == number { fmt.Printf("%d is an Armstrong number\n", number) } else { fmt.Printf("%d is not an Armstrong number\n", number) } }
Complexity: O(d) time, O(1) space
Time Complexity
The program loops through each digit once, so the time depends linearly on the number of digits, O(d).
Space Complexity
Only a few variables are used regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
The numeric method using modulo and division is fastest and uses less memory than string conversion or recursion.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Modulo and division | O(d) | O(1) | Fastest and memory efficient |
| String conversion | O(d) | O(d) | Easier to read, uses more memory |
| Recursive method | O(d) | O(d) | Elegant but less efficient for large inputs |
math.Log10 to find the number of digits quickly instead of converting to string.