A number matches the sum-of-digit-cubes condition if its value equals the sum of each decimal digit raised to the third power. For example, 371 = 3³ + 7³ + 1³. This fixed-cube problem is related to, but different from, the general Armstrong-number rule.
Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.
View on GitHubn = 371
total = sum(int(digit) ** 3 for digit in str(n))
print("Sum of cubes:", total)
print("Matches:", total == n)
Output:
Sum of cubes: 371
Matches: True
For three-digit numbers, this is also the Armstrong-number condition.
This follows the source tutorial using pow(digit, 3) and a string input.
n = input("Enter a number: ").strip()
if n.isdecimal():
my_sum = 0
for i in range(len(n)):
my_sum += pow(int(n[i]), 3)
print("Sum of cubes:", my_sum)
print("Digits match:", my_sum == int(n))
else:
print("Use nonnegative whole numbers")
Output:
Sum of cubes: 371
Digits match: True
The original code searches from 10 to 99999 and finds 153, 370, 371 and 407.
matches = []
for n in range(10, 100000):
total = sum(int(d) ** 3 for d in str(n))
if total == n:
matches.append(n)
print(matches)
print("Smallest:", min(matches))
print("Largest:", max(matches))
Output:
[153, 370, 371, 407]
Smallest: 153
Largest: 407
For the fixed cube rule, the digits 0 and 1 match because 0³ = 0 and 1³ = 1. Other single digits do not.
print([n for n in range(10) if n == n ** 3])
Output:
[0, 1]
Separate the calculation from the matching test.
def cube_sum(n):
return sum(int(d) ** 3 for d in str(n))
for n in (153, 370, 371, 407, 1634):
print(n, cube_sum(n), n == cube_sum(n))
Output:
153 153 True
370 370 True
371 371 True
407 407 True
1634 308 False
Armstrong numbers raise digits to the number of digits, not always to 3.
n = 1634
digits = str(n)
fixed_cube = sum(int(d) ** 3 for d in digits)
armstrong_power = sum(int(d) ** len(digits) for d in digits)
print("Fixed cube:", fixed_cube)
print("Armstrong:", armstrong_power)
Output:
Fixed cube: 308
Armstrong: 1634
input() returns a string. Convert and validate the value before doing arithmetic.Change the range and explain why 1634 is Armstrong but not equal to the sum of digit cubes. Use the companion practice notebook for runnable examples and self-checks.
Yes. Each equals the sum of the cubes of its digits.
No. It is a four-digit Armstrong number using fourth powers.
Check that the list is nonempty before calling min() or max().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.