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
1^1 = 1 7^2 = 49 5^3 = 125 Sum: 175 175 is a Disarium number
How it works
str(num)turns the number into text, so its digits can be read from left to right.enumerate(digits, start=1)gives each digit together with its position: 1 for the first digit, 2 for the second, and so on.- Each digit is raised to the power of its position and added to
total, and the steps are printed as it goes. - If the total equals the number, it is a Disarium number.
Other ways to write it
Working from the last digit
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: [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.
For a practical file: draw its flowchart or write its algorithm in steps.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises