Check whether given three numbers are adjacent primes in Python

Adjacent primes are consecutive prime numbers with no other prime numbers between them. For example, 5, 7, and 11 are adjacent primes because 7 is the next prime after 5, and 11 is the next prime after 7.

So, if the input is like nums = [5, 7, 11], then the output will be True since these are three consecutive primes.

Algorithm

To solve this problem, we will follow these steps:

  • Check if all three numbers are prime
  • Verify that the second number is the next prime after the first
  • Verify that the third number is the next prime after the second
  • Return True if all conditions are met, False otherwise

Implementation

Let's implement the solution step by step ?

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

def get_next_prime(num):
    next_prime = num + 1
    while not is_prime(next_prime):
        next_prime += 1
    return next_prime

def check_adjacent_primes(x, y, z):
    # Check if all three numbers are prime
    if not (is_prime(x) and is_prime(y) and is_prime(z)):
        return False
    
    # Check if y is the next prime after x
    if get_next_prime(x) != y:
        return False
    
    # Check if z is the next prime after y
    if get_next_prime(y) != z:
        return False
    
    return True

# Test with the example
nums = [5, 7, 11]
result = check_adjacent_primes(*nums)
print(f"Are {nums} adjacent primes? {result}")

The output of the above code is ?

Are [5, 7, 11] adjacent primes? True

Testing with Different Examples

Let's test our function with multiple examples to verify its correctness ?

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

def get_next_prime(num):
    next_prime = num + 1
    while not is_prime(next_prime):
        next_prime += 1
    return next_prime

def check_adjacent_primes(x, y, z):
    if not (is_prime(x) and is_prime(y) and is_prime(z)):
        return False
    
    if get_next_prime(x) != y:
        return False
    
    if get_next_prime(y) != z:
        return False
    
    return True

# Test cases
test_cases = [
    [5, 7, 11],    # Adjacent primes
    [3, 5, 7],     # Adjacent primes
    [2, 3, 5],     # Adjacent primes
    [5, 11, 13],   # Not adjacent (missing 7)
    [4, 6, 8],     # Not primes
    [3, 7, 11]     # Missing 5 between 3 and 7
]

for nums in test_cases:
    result = check_adjacent_primes(*nums)
    print(f"{nums}: {result}")
[5, 7, 11]: True
[3, 5, 7]: True
[2, 3, 5]: True
[5, 11, 13]: False
[4, 6, 8]: False
[3, 7, 11]: False

How It Works

The solution consists of three helper functions:

  • is_prime(num): Efficiently checks if a number is prime by testing divisibility up to its square root
  • get_next_prime(num): Finds the next prime number after the given number
  • check_adjacent_primes(x, y, z): Validates that all three numbers are primes and consecutive

Conclusion

This solution efficiently checks if three numbers are adjacent primes by verifying each number is prime and that they are consecutive in the prime sequence. The optimized prime checking function ensures good performance for reasonable input sizes.

Updated on: 2026-03-25T14:35:56+05:30

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements