Introduction
In this blog post, we will explore a Python program that identifies and prints all Disarium numbers within the range of 1 to 100. Disarium numbers are those whose sum of each digit, each raised to the power of its respective position, equals the number itself.
Python Program
def calculate_disarium_sum(number):
num_str = str(number)
disarium_sum = 0
for i in range(1, len(num_str) + 1):
digit = int(num_str[i - 1])
disarium_sum += digit ** i
return disarium_sum
def is_disarium(number):
return number == calculate_disarium_sum(number)
def find_disarium_numbers(start, end):
disarium_numbers = []
for num in range(start, end + 1):
if is_disarium(num):
disarium_numbers.append(num)
return disarium_numbers
# Example Usage
start_range = 1
end_range = 100
disarium_result = find_disarium_numbers(start_range, end_range)
print(f"The Disarium numbers between {start_range} and {end_range} are: {disarium_result}")
Program Explanation
The program defines a function
calculate_disarium_sum
to calculate the sum of digits each raised to the power of their respective position.Another function
is_disarium
checks if a given number is a Disarium number by comparing it with the result obtained fromcalculate_disarium_sum
.The main function
find_disarium_numbers
iterates through the specified range and identifies Disarium numbers by calling theis_disarium
function.The Disarium numbers are stored in a list and returned.
The example usage demonstrates finding Disarium numbers between 1 and 100 and printing the result.
Example Output
For the specified range of 1 to 100, the output will be:
The Disarium numbers between 1 and 100 are: [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]
Conclusion
This Python program effectively identifies and prints Disarium numbers within a specified range. The code demonstrates a clear implementation of the Disarium number concept, utilizing functions to enhance modularity.
Readers can experiment with different ranges and explore additional functionalities, such as finding Disarium numbers with a specific number of digits. Understanding and modifying this program will provide insights into the properties of Disarium numbers. Happy coding!