5 Best Ways to Check Whether the Sum of Absolute Difference of Adjacent Digits Is Prime in Python

πŸ’‘ Problem Formulation: In this article, we tackle the problem of determining if the sum of absolute differences between adjacent digits of a number is a prime number. An example input could be 2734, and the desired output would be True, assuming the sum (2 + 1 + 1) yields a prime number.

Method 1: Basic Iteration with Prime Check Function

This method involves iterating over the digits of a number to compute the sum of the absolute differences between adjacent digits, and then checking if this sum is prime. A helper function, is_prime, is used to determine the primality.

Here’s an example:

def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def sum_of_diffs_is_prime(number):
    digits = [int(d) for d in str(number)]
    return is_prime(sum(abs(a-b) for a, b in zip(digits, digits[1:])))

# Example
print(sum_of_diffs_is_prime(2734))

Output: False

This code snippet first defines a function is_prime(n) that checks if a number n is a prime number. Then, sum_of_diffs_is_prime(number) calculates the sum of absolute differences of adjacent digits and verifies its primality. In our example, the sum is 4 which is not a prime number.

Method 2: Using List Comprehension and Built-in all Function

List comprehension in Python provides a compact way of processing sequences. This method combines list comprehension and the built-in all() function to determine the primality of the sum efficiently.

Here’s an example:

def is_prime(n):
    return n > 1 and all(n % i for i in range(2, int(n ** 0.5) + 1))

def sum_of_diffs_is_prime(number):
    diffs = [abs(int(a) - int(b)) for a, b in zip(str(number), str(number)[1:])]
    return is_prime(sum(diffs))

# Example
print(sum_of_diffs_is_prime(2734))

Output: False

This example reuses the is_prime function with the all() function for a more concise prime check. Comprehensions are used to create the list of differences, and then the sum is assessed for primality. Again, the sum is 4 and is therefore not prime.

Method 3: Using Recursion and Memoization

Recursion can be a powerful technique when coupled with memoization. This method employs a recursive strategy to check for prime numbers and applies memoization to optimize primality tests.

Here’s an example:

from functools import lru_cache

@lru_cache
def is_prime(n):
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(n ** 0.5) + 1, 2):
        if n % i == 0:
            return False
    return True

def sum_of_diffs_is_prime(number):
    return is_prime(sum(abs(int(a) - int(b)) for a, b in zip(str(number), str(number)[1:])))

# Example
print(sum_of_diffs_is_prime(2734))

Output: False

The is_prime function is decorated with @lru_cache to store results of prime checks and avoid redundant calculations. This is helpful if multiple calls are made with the same inputs. The code demonstrates how a combination of recursive strategies with memoization can efficiently solve the problem.

Method 4: Using itertools and functools

The itertools module in Python allows for elegant and efficient iteration patterns. Combined with functools, this method provides an expressive and functional approach to solve our problem.

Here’s an example:

from itertools import tee
from functools import reduce

def pairwise(iterable):
    "s -> (s[0],s[1]), (s[1],s[2]), (s[2], s[3]), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def is_prime(n):  # As defined in Method 2
    return n > 1 and all(n % i for i in range(2, int(n ** 0.5) + 1))

def sum_of_diffs_is_prime(number):
    diffs = (abs(int(a) - int(b)) for a, b in pairwise(str(number)))
    return is_prime(reduce(lambda x, y: x + y, diffs))

# Example
print(sum_of_diffs_is_prime(2734))

Output: False

By using the pairwise helper function from the itertools recipes, you can generate adjacent pairs of digits and then compute their differences. The sum is then compounded with the help of the functools.reduce function before assessing for primality.

Bonus One-Liner Method 5: Lambda Function with Map and Filter

In this one-liner approach, we use the power of lambda functions alongside map and filter to create a concise and efficient solution to our problem.

Here’s an example:

is_prime = lambda x: x > 1 and all(x % i for i in range(2, int(x ** 0.5) + 1))
sum_of_diffs_is_prime = lambda num: is_prime(sum(map(abs, map(int.__sub__, map(int, str(num)[:-1]), map(int, str(num)[1:])))))

# Example
print(sum_of_diffs_is_prime(2734))

Output: False

This one-liner first maps the subtraction operation to adjacent digits, then applies the absolute function via map to get the positive differences before summing them up. Finally, it checks the sum’s primality with the lambda-defined is_prime function.

Summary/Discussion

  • Method 1: Basic Iteration with Prime Check Function. This is a straightforward, easy-to-understand method. However, it may not be the most efficient for large numbers due to its explicit iteration and prime checking.
  • Method 2: Using List Comprehension and Built-in all Function. It offers an elegant and more Pythonic way of processing the number. This method can be faster due to list comprehensions and the efficient all() function, but it still suffers from potential efficiency issues for large numbers.
  • Method 3: Using Recursion and Memoization. This approach is advantageous when dealing with repetitive checks, as memoization saves on computation time. However, it adds complexity and may incur overhead for the initial setting up of the cache.
  • Method 4: Using itertools and functools. Allows for a more functional programming approach and can be elegant and efficient, especially with large datasets. However, it requires a deeper understanding of Python’s functional tools.
  • Bonus One-Liner Method 5: Lambda Function with Map and Filter. This is a compact and clever solution, but it may be difficult to read and maintain, especially for those not familiar with functional programming concepts.