5 Best Ways to check whether product of digits at even places of a number is divisible by k in Python

Rate this post
Checking Product of Digits at Even Places for Divisibility by K in Python

π‘ Problem Formulation: You’re given an integer and you need to determine whether the product of its digits at even places is divisible by a given integer `k`. For example, if the input number is `123456` and `k` is `8`, the product of digits at even places would be `2 * 4 * 6 = 48`, which is divisible by `8`, so the output should be `True`.

Method 1: Using Loop and Modulus Operator

This method involves iterating over each digit of the number, determining whether it’s in an even place, calculating the product of these digits, and checking if the result is divisible by `k`. It’s straightforward and easy to understand.

Here’s an example:

```def is_product_divisible(number, k):
product = 1
number_str = str(number)
for i in range(1, len(number_str), 2):
product *= int(number_str[i])
return product % k == 0

print(is_product_divisible(123456, 8))```

Output: True

This code converts the number to a string for easy digit access. It steps through the string by jumps of two, starting from index 1 (which corresponds to the second digit, thereby accessing even places). It then multiplies these digits and checks divisibility by `k`.

Method 2: List Comprehension and Reduce Function

By utilizing list comprehension and the `reduce` function from `functools`, we can combine the product computation and divisibility check into a more compact form.

Here’s an example:

```from functools import reduce

def is_product_divisible(number, k):
digits = [int(d) for i, d in enumerate(str(number)) if i % 2 != 0]
product = reduce(lambda x, y: x * y, digits)
return product % k == 0

print(is_product_divisible(123456, 8))```

Output: True

The code uses list comprehension to create a list of digits at even places. It then applies the `reduce` function to find their product. Finally, it checks for divisibility by `k` using the modulus operator.

Method 3: Functional Approach with Filter and Reduce

A more functional style using `filter` to select the even-place digits, followed by `reduce` to calculate the product. This method emphasizes readability and functional programming principles.

Here’s an example:

```from functools import reduce

def is_product_divisible(number, k):
digits = list(str(number))
even_digits = filter(lambda i: digits.index(i) % 2 != 0, digits)
product = reduce(lambda x, y: x * y, map(int, even_digits))
return product % k == 0

print(is_product_divisible(123456, 8))```

Output: True

First, it converts the number into a list of digits. It then filters this list to only include digits at even positions. Finally, it computes the product and checks for divisibility by `k`.

Method 4: Using List Slicing

List slicing offers a succinct way to access elements at even indexes. This method simplifies the selection process of the digits on which the product is based.

Here’s an example:

```def is_product_divisible(number, k):
digits = [int(d) for d in str(number)[1::2]]
product = 1
for digit in digits:
product *= digit
return product % k == 0

print(is_product_divisible(123456, 8))```

Output: True

Using string slicing, this code snippet captures every second digit effectively representing even places. It multiplies these digits and checks if the result is divisible by `k`.

Bonus One-Liner Method 5: Using All-In-One Expression with List Comprehension

This concise one-liner bundles list comprehension, product computation, and divisibility check into a single line, showcasing the power and brevity of Python.

Here’s an example:

```def is_product_divisible(number, k):
return reduce(lambda x, y: x * y, [int(str(number)[i]) for i in range(1, len(str(number)), 2)]) % k == 0

print(is_product_divisible(123456, 8))```

Output: True

The one-liner extracts every second digit, calculates their product, and checks divisibility by `k` in a compact and efficient manner.

Summary/Discussion

In this article, we explored five methods to determine if the product of digits at even places of a number is divisible by `k`.

• Method 1: Loop and Modulus Operator. Strengths: It’s clear and easy to understand. Weaknesses: Could be deemed verbose.
• Method 2: List Comprehension and Reduce Function. Strengths: More compact code. Weaknesses: Might be less readable for those unfamiliar with functional programming.
• Method 3: Functional Approach with Filter and Reduce. Strengths: Leverages functional programming paradigms. Weaknesses: Could be performance-intensive due to the use of `filter`.
• Method 4: Using List Slicing. Strengths: Simplified even digit selection process. Weaknesses: May not be as immediately obvious as a loop-based approach.
• Bonus Method 5: All-In-One Expression with List Comprehension. Strengths: Extremely succinct. Weaknesses: Potentially difficult to debug and understand.