Exploring Python Techniques to Identify Disarium Numbers

Rate this post

π‘ Problem Formulation: A Disarium Number is a number in which the sum of its digits powered with their respective positions is equal to the number itself. For instance, 175 is a Disarium number as 11 + 72 + 53 = 175. This article will explore multiple Python programming methods to check if a given number is a Disarium number.

Method 1: Using Loop and Position Power Calculation

This method calculates the positional power sum through iteration. Each digit is raised to its positional power (starting from 1), and the sum is accumulated. If the sum matches the original number, it is a Disarium number.

Here’s an example:

```def is_disarium(number):
sum_of_digits = 0
for i, digit in enumerate(str(number), start=1):
sum_of_digits += int(digit) ** i
return sum_of_digits == number

print(is_disarium(175))```

Output: `True`

This code snippet defines a function called `is_disarium()` which checks if the input `number` is a Disarium number. It uses a loop with `enumerate()` to get each digit and its position, raises the digit to the power of its position, sums these values, and compares the sum to the original number.

Method 2: Using List Comprehension

This method employs list comprehension for a more compact syntax. It calculates the powered sum inline within a list and uses the `sum()` function to evaluate if the original number is a Disarium number.

Here’s an example:

```def is_disarium(number):
return number == sum(int(d) ** i for i, d in enumerate(str(number), 1))

print(is_disarium(89))```

Output: `True`

In this code example, `is_disarium()` function conducts a list comprehension that raises each digit to its positional power, analogous to Method 1, but in a more compact form, demonstrating Python’s expressive capabilities.

Method 3: Using Map and Lambda Function

This technique utilizes `map()` and a `lambda` function to apply the positional power to each digit. `Map()` inherently iterates over the digits, applying the `lambda` function to produce a similar result to Method 2 but with a different approach.

Here’s an example:

```def is_disarium(number):
digits = str(number)
return number == sum(map(lambda x, y: int(y) ** (x + 1), range(len(digits)), digits))

print(is_disarium(135))```

Output: `True`

The snippet defines `is_disarium()`, using `map()` to raise each digit to its respective positional power using a `lambda` function. The `range(len(digits))` is used to get the position for each digit, offset by 1 since positions should start from 1.

Method 4: Utilizing Recursive Function

This recursive function approach calculates the positional power sum by calling itself with the next digit and the remaining part of the number until it covers all the digits. It’s an elegant form, showcasing the power of recursion.

Here’s an example:

```def disarium_helper(number, position):
if number == 0:
return 0
else:
return (number % 10) ** position + disarium_helper(number // 10, position - 1)

def is_disarium(number):
return number == disarium_helper(number, len(str(number)))

print(is_disarium(80))```

Output: `False`

This code snippet uses a helper function `disarium_helper()` which applies the recursive logic to calculate the sum of positional powers. The `is_disarium()` function then makes use of this helper to check if the sum equals the original number.

Bonus One-Liner Method 5: Using Functional Programming with `reduce()`

This method cleverly combines `reduce()` from `functools` and a generator expression to compactly and efficiently calculate the positional power sum and verify the Disarium condition.

Here’s an example:

```from functools import reduce

is_disarium = lambda number: number == reduce(lambda s, d, idx: s + pow(int(d), idx), str(number), 0, enumerate(str(number), 1))

print(is_disarium(89))```

Output: `True`

This one-liner defines an `is_disarium` lambda function that combines `reduce()` with positional power calculation. The generator expression creates tuples of digits with their positions, and `reduce()` is used to accumulate the sum.

Summary/Discussion

• Method 1: Loop and Position Power Calculation. Simple to understand and debug. Slightly verbose.
• Method 2: List Comprehension. Compact and Pythonic. May be less readable for those new to Pythonβs list comprehensions.
• Method 3: Map and Lambda Function. A functional approach. Can be less intuitive due to the `lambda` and `map()` functions.
• Method 4: Recursive Function. Elegant and expressive. However, it may not be as performance-efficient for large input sizes.
• Bonus Method 5: Functional Programming with `reduce()`. Extremely concise. High efficiency in functional contexts but can sacrifice readability for some.