Twin Prime Number Program in Python

Introduction

In this post, I am going to write and explain a twin prime number program in python. First of all we should know what is twin prime numbers.

Also Read: Python Program to Check Armstrong Number

What is TWIN Prime Number?

A twin prime is a prime number that has a prime gap of two with another prime number. For example, the numbers 3 and 5 are twin primes because they are both prime and the gap between them is 2.

Steps to Find Twin Prime Numbers in Python

To check if a number is a twin prime in Python, we can use a simple function that follows these steps:

  1. Check if the number is prime. If it is not, return False.
  2. Check if the number minus 2 is prime. If it is, return True.
  3. Check if the number plus 2 is prime. If it is, return True.
  4. If none of the above conditions are met, return False.

Here’s what the function might look like:

def is_twin_prime(num):
    # Check if the number is prime
    if is_prime(num) == False:
        return False
    
    # Check if the number minus 2 is prime
    if is_prime(num - 2) == True:
        return True
    
    # Check if the number plus 2 is prime
    if is_prime(num + 2) == True:
        return True
    
    # If none of the above conditions are met, return False
    return False

Note that in this function, we are using another function called is_prime() to check if a number is prime. This function could be implemented using the following steps:

  1. If the number is less than 2, return False.
  2. Iterate over a range of numbers from 2 to the square root of the number + 1.
  3. If the number is evenly divisible by any of the numbers in the range, return False.
  4. If the loop completes without returning False, return True.

Also Read: Python Program to Check If Two Strings are Anagram

Here’s what the is_prime() function might look like:

def is_prime(num):
    # Return False if the number is less than 2
    if num < 2:
        return False
    
    # Iterate over a range of numbers from 2 to the square root of the number + 1
    for i in range(2, int(math.sqrt(num)) + 1):
        # If the number is evenly divisible by any of the numbers in the range, return False
        if num % i == 0:
            return False
    
    # If the loop completes without returning False, return True
    return True

To test the is_twin_prime() function, we can write some test cases:

assert is_twin_prime(3) == True
assert is_twin_prime(5) == True
assert is_twin_prime(7) == True
assert is_twin_prime(11) == True
assert is_twin_prime(17) == True
assert is_twin_prime(41) == True
assert is_twin_prime(71) == True
assert is_twin_prime(101) == True
assert is_twin_prime(107) == False
assert is_twin_prime(137) == False

All of these test cases should return True, indicating that the function is working correctly.

Detailed Explanation

Here is a more detailed explanation of the Python function to check if a number is a twin prime:

First, we start by defining the is_twin_prime() function and passing in a variable called num. The first thing we do is check if the number is prime using the is_prime() function. If the number is not prime, we return False immediately because a twin prime must be prime.

Also Read: Python Program to Delete an Element From a Dictionary

If the number is prime, we then check if the number minus 2 is prime. If it is, we return True because the number and the number minus 2 form a pair of twin primes.

If the number minus 2 is not prime, we then check if the number plus 2 is prime. If it is, we return True for the same reason.

If none of these conditions are met, we return False because the number is not a twin prime.

Here is the complete code again, with some additional comments to explain each part:

Twin Prime Number Program in Python

def is_twin_prime(num):
    # Check if the number is prime
    if is_prime(num) == False:
        return False
    
    # Check if the number minus 2 is prime
    if is_prime(num - 2) == True:
        return True
    
    # Check if the number plus 2 is prime
    if is_prime(num + 2) == True:
        return True
    
    # If none of the above conditions are met, return False
    return False

The is_prime() function works by first checking if the number is less than 2. If it is, we return False because all numbers less than 2 are not prime.

Next, we use a for loop to iterate over a range of numbers from 2 to the square root of the number + 1. For each number in the range, we check if the original number is evenly divisible by it. If it is, we return False because the original number is not prime.

If the loop completes without returning False, we return True because the original number is prime.

Here is the complete code for the is_prime() function:

def is_prime(num):
    # Return False if the number is less than 2
    if num < 2:
        return False
    
    # Iterate over a range of numbers from 2 to the square root of the number + 1
    for i in range(2, int(math.sqrt(num)) + 1):
        # If the number is evenly divisible by any of the numbers in the range, return False
        if num % i == 0:
            return False
    
    # If the loop completes without returning False, return True
    return True

I hope this helps to clarify how the functions work! Let me know if you have any questions.

Leave a Comment