5 Best Ways to Check if a Given Number is an Emirp Number in Python

Rate this post

πŸ’‘ Problem Formulation: An emirp number is a number that is prime both forwards and backwards. Unlike palindromes, the reversed number must be different. For instance, 13 is an emirp number because both 13 and 31 are prime. This article will provide methods to determine if a given number is an emirp number in Python.

Method 1: Basic Function With Prime Check

This method involves creating a function that first checks if a given number is prime. Then, it confirms whether the reversed number is also prime, ensuring both are different. The function specification is straightforward: it returns True if the number is emirp, and False otherwise.

Here’s an example:

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

def is_emirp(num):
    rev_num = int(str(num)[::-1])
    return num != rev_num and is_prime(num) and is_prime(rev_num)

print(is_emirp(13))

Output: True

This code introduces two functions: is_prime() checks for primality of a number, and is_emirp() checks for the emirp property by reversing the digits of the number and testing for primality of both.

Method 2: Using List Comprehensions

Python’s list comprehension is a concise way to perform operations on lists. In this method, a single list comprehension checks for non-divisible numbers up to the square root of given number. This makes the primality test more direct and shorter.

Here’s an example:

def is_emirp(num):
    is_prime = lambda n: all(n%i != 0 for i in range(2, int(n**0.5)+1)) and n>1
    rev_num = int(str(num)[::-1])
    return num != rev_num and is_prime(num) and is_prime(rev_num)

print(is_emirp(37))

Output: True

This code uses a lambda function to concisely express the prime checking logic within the is_emirp() function, which then evaluates the emirp criteria as before.

Method 3: Caching Prime Results

Caching results for prime checks can significantly improve performance when checking multiple numbers. In this method, a helper dictionary stores previously calculated prime numbers to avoid redundant calculations.

Here’s an example:

prime_cache = {}

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

def is_emirp(num):
    rev_num = int(str(num)[::-1])
    return num != rev_num and is_prime(num) and is_prime(rev_num)

print(is_emirp(17))

Output: True

By integrating caching into the prime-checking function, is_emirp() can now quickly determine if a number is emirp, especially if some prime validations have already been computed.

Method 4: Utilizing a Sieve Algorithm

The Sieve of Eratosthenes is a highly efficient way to generate a list of primes up to a certain number. This information can then be used to check emirp numbers within that range quickly.

Here’s an example:

def sieve(n):
    is_prime = [False, False] + [True] * (n-1)
    for p in range(2, int(n**0.5) + 1):
        if is_prime[p]:
            for i in range(p*p, n+1, p):
                is_prime[i] = False
    return is_prime
 
def is_emirp(num, primes):
    rev_num = int(str(num)[::-1])
    return num != rev_num and primes[num] and primes[rev_num]

n = 100
primes = sieve(n)
print(is_emirp(31, primes))

Output: True

This code first generates a list of prime status for numbers up to n using the sieve algorithm. Then the is_emirp() function checks the emirp condition using this precomputed list.

Bonus One-Liner Method 5: Using All-in-One Expression

For those who prefer concise code, this one-line method combines checking for prime, reversing the number, and confirming the emirp condition.

Here’s an example:

is_emirp = lambda n: n > 1 and all(n % i for i in range(2, int(n**0.5) + 1)) and n != int(str(n)[::-1]) and all(int(str(n)[::-1]) % i for i in range(2, int(int(str(n)[::-1])**0.5) + 1))

print(is_emirp(71))

Output: True

This one-liner defines an is_emirp lambda function that encompasses prime checking for both the original and reversed number, as well as the check to ensure they are not the same.

Summary/Discussion

  • Method 1: Basic Function With Prime Check. Straightforward and easy to understand. Not optimal for checking a large range of numbers.
  • Method 2: Using List Comprehensions. Cleaner syntax than Method 1. Still not the best for a large dataset due to lack of caching.
  • Method 3: Caching Prime Results. More efficient through caching, making it better suited for programs checking many numbers.
  • Method 4: Utilizing a Sieve Algorithm. Best for a known range of numbers, very efficient for checking multiple emirp numbers within that range.
  • One-Liner Method 5: Using All-in-One Expression. Extremely concise, but can be hard to read and understand at a glance. Best for one-off uses or small scripts.