Sum of Cubes of Digits in Python: Find Matching Numbers

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.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

n = 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.

Original digit-by-digit loop

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

Find all matching multi-digit numbers

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

One-digit values also match

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]

Reusable cube-sum function

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

Cube sum versus Armstrong numbers

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

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

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.

Frequently asked questions

Are 153 and 371 matching numbers?

Yes. Each equals the sum of the cubes of its digits.

Is 1634 a matching digit-cube number?

No. It is a four-digit Armstrong number using fourth powers.

What if the range has no matches?

Check that the list is nonempty before calling min() or max().

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer