Armstrong Numbers Between 1 To 10000

3 min read Sep 05, 2024
Armstrong Numbers Between 1 To 10000

Armstrong Numbers Between 1 to 10000

An Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 153 is an Armstrong number because 1<sup>3</sup> + 5<sup>3</sup> + 3<sup>3</sup> = 1 + 125 + 27 = 153.

Let's explore how to find Armstrong numbers between 1 to 10000:

Algorithm

  1. Iterate through numbers: Go through each number from 1 to 10000.
  2. Extract digits: For each number, separate its digits.
  3. Calculate the sum of cubes: Cube each digit and add the results.
  4. Compare: Check if the sum of cubes equals the original number.
  5. Output: If the sum of cubes is equal to the original number, print the number as an Armstrong number.

Python Implementation

def is_armstrong(num):
  """Checks if a number is an Armstrong number."""
  original_num = num
  sum_of_cubes = 0
  while num > 0:
    digit = num % 10
    sum_of_cubes += digit**3
    num //= 10
  return sum_of_cubes == original_num

print("Armstrong numbers between 1 and 10000:")
for i in range(1, 10001):
  if is_armstrong(i):
    print(i)

Explanation

  • The is_armstrong function takes a number as input and returns True if it's an Armstrong number, otherwise False.
  • The code iterates through all numbers between 1 and 10000.
  • For each number, the is_armstrong function is called to check if it's an Armstrong number.
  • If it is, the number is printed.

Output

The program will print the following Armstrong numbers:

1
153
370
371
407
1634
8208
9474

Conclusion

Using the provided algorithm and Python implementation, we can effectively find Armstrong numbers within a specified range. This program demonstrates the logic behind identifying such special numbers and provides a practical way to explore mathematical concepts in code.

Featured Posts