A Disarium number is a number such that the sum of its digits powered with its corresponding position is equal to the number itself. For example, the number 89 is a Disarium number because 8^1 + 9^2 = 89.
Here is a Python program that uses a for loop and the pow()
function to check if a given number is a Disarium number:
def is_disarium(n):
num = n
digits = [int(d) for d in str(n)]
length = len(digits)
sum = 0
for i in range(length):
sum += pow(digits[i], i+1)
return sum == num
# Example usage
num = 89
print(is_disarium(num))
The function is_disarium(n)
takes an integer as an argument, it converts the integer to a list of its digits using list comprehension and the int(d)
function. It then calculates the sum of the digits powered with their corresponding position using a for loop. It compares the sum with the original number and returns a boolean value indicating if the number is a Disarium number or not.
The example usage calls the is_disarium()
function passing the number 89 as an argument, which returns True because 8^1 + 9^2 = 89.
You could also use the map()
function and the sum()
function instead of a for loop to calculate the sum of digits powered with their corresponding position.