Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print all Disarium numbers between 1 to 100
A Disarium number is a number where the sum of its digits raised to the power of their respective positions equals the original number itself. For example, 89 is a Disarium number because 81 + 92 = 8 + 81 = 89.
To find all Disarium numbers between 1 and 100, we need to calculate the length of each number and check if the sum of digits raised to their positional powers equals the original number.
Complete Program
def length_calculation(my_val):
len_val = 0
while(my_val != 0):
len_val = len_val + 1
my_val = my_val//10
return len_val
def digit_sum(my_num):
remaining = sum_val = 0
len_fun = length_calculation(my_num)
while(my_num > 0):
remaining = my_num%10
sum_val = sum_val + (remaining**len_fun)
my_num = my_num//10
len_fun = len_fun - 1
return sum_val
ini_result = 0
print("The disarium numbers between 1 and 100 are:")
for i in range(1, 101):
ini_result = digit_sum(i)
if(ini_result == i):
print(i)
The disarium numbers between 1 and 100 are: 1 2 3 4 5 6 7 8 9 89
How It Works
The program uses two helper functions:
- length_calculation() ? Counts the number of digits in a given number
- digit_sum() ? Calculates the sum of digits raised to their respective positional powers
Step-by-Step Process
For each number from 1 to 100:
- Calculate the total number of digits
- Extract each digit from right to left
- Raise each digit to the power of its position (starting from the leftmost position)
- Sum all these powered values
- Compare with the original number
Example Verification
Let's verify why 89 is a Disarium number ?
number = 89
print(f"Checking {number}:")
print(f"8^1 + 9^2 = {8**1} + {9**2} = {8**1 + 9**2}")
print(f"Is {number} a Disarium number? {8**1 + 9**2 == number}")
Checking 89: 8^1 + 9^2 = 8 + 81 = 89 Is 89 a Disarium number? True
Conclusion
Disarium numbers between 1 and 100 are mostly single digits (1-9) and one two-digit number (89). The algorithm systematically checks each number by calculating the sum of digits raised to their positional powers.
