Part of the series

Several example codes

~2 min read • Updated Sep 16, 2025

Program Overview

This Python program checks all four-digit numbers (from 1000 to 9999).
It prints those numbers where the sum of the first digit raised to the power of 1 and the fourth digit raised to the power of 4
equals the sum of the second digit squared and the third digit cubed.
In other words:
d1¹ + d4⁴ = d2² + d3³


Python Code:


print("Four-digit numbers where d1^1 + d4^4 = d2^2 + d3^3:")

for num in range(1000, 10000):
    d1 = int(str(num)[0])
    d2 = int(str(num)[1])
    d3 = int(str(num)[2])
    d4 = int(str(num)[3])
    
    left = d1 ** 1 + d4 ** 4
    right = d2 ** 2 + d3 ** 3
    
    if left == right:
        print(num)

Sample Output:


Four-digit numbers where d1^1 + d4^4 = d2^2 + d3^3:
2427
...

Explanation:

Here’s how the program works:
- Each digit is extracted using string indexing
- The first and fourth digits are raised to powers 1 and 4 respectively
- The second and third digits are raised to powers 2 and 3 respectively
- If both sides are equal, the number is printed


Written & researched by Dr. Shahin Siami