Editor

Python Program to Check a Disarium Number

Beginner · Numbers

A Disarium number is equal to the sum of its digits raised to the power of their positions, counting from 1 on the left. 175 is a Disarium number because 1^1 + 7^2 + 5^3 = 1 + 49 + 125 = 175.

Program

disarium_number.py
num = 175 # or: num = int(input("Enter a number: "))
digits = str(num)
total = 0
# enumerate() gives each digit with its position, counting from 1
for position, digit in enumerate(digits, start=1):
term = int(digit) ** position
print(f"{digit}^{position} = {term}")
total += term
print("Sum:", total)
if total == num:
print(num, "is a Disarium number")
else:
print(num, "is not a Disarium number")
Output
1^1 = 1
7^2 = 49
5^3 = 125
Sum: 175
175 is a Disarium number

How it works

  1. str(num) turns the number into text, so its digits can be read from left to right.
  2. enumerate(digits, start=1) gives each digit together with its position: 1 for the first digit, 2 for the second, and so on.
  3. Each digit is raised to the power of its position and added to total, and the steps are printed as it goes.
  4. If the total equals the number, it is a Disarium number.

Other ways to write it

Working from the last digit

Working from the last digit
num = 89
temp = num
position = len(str(num)) # the last digit sits in the last position
total = 0
while temp > 0:
digit = temp % 10
total += digit ** position
position -= 1
temp //= 10
print(num, "is a Disarium number" if total == num else "is not a Disarium number")
Output
89 is a Disarium number

The % 10 method takes digits from the right, so the position starts at the digit count and goes down by one each time.

Disarium numbers from 1 to 1000

Disarium numbers from 1 to 1000
def is_disarium(n):
return n == sum(int(d) ** i for i, d in enumerate(str(n), start=1))
print("Disarium numbers from 1 to 1000:")
print([n for n in range(1, 1001) if is_disarium(n)])
Output
Disarium numbers from 1 to 1000:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 135, 175, 518, 598]

Besides the one-digit numbers, only 89, 135, 175, 518 and 598 are Disarium numbers up to 1000.

Try it yourself

Set num to 518 and then 598 and check the printed powers by hand.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises