Python Program to Print all Twin Primes less than N

Python Program to Print all Twin Primes less than N

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Given a number N the task is to print all twin primes less than the given number in Python.

Twin Primes:

We know that Prime Numbers are those with exactly two Factors. 1 and the number itself are the two Factors.

Twin Primes are pairs of primes that differ by two digits.

(3, 5), (5, 7), (11, 13), and so on are some examples.

In the Number System, there is an unlimited number of Twin Primes.

Examples:

Example1:

Input:

Given number =562

Output:

The twin primes below the number 562 are :
(3,5)  (5,7)  (11,13)  (17,19)  (29,31)  (41,43)  (59,61)  (71,73)  (101,103)  (107,109)  (137,139)  (149,151)  (179,181)  
(191,193)  (197,199)  (227,229)  (239,241)  (269,271)  (281,283)  (311,313)  (347,349)  (419,421)  (431,433)  (461,463)
 (521,523)

Example2:

Input:

Given number =842

Output:

Enter some random number = 842
The twin primes below the number 842 are :
(3,5) (5,7) (11,13) (17,19) (29,31) (41,43) (59,61) (71,73) (101,103) (107,109) (137,139) (149,151) (179,181) (191,193)
 (197,199) (227,229) (239,241) (269,271) (281,283) (311,313) (347,349) (419,421) (431,433) (461,463) (521,523)
 (569,571) (599,601) (617,619) (641,643) (659,661) (809,811) (821,823) (827,829)

Python Program to Print all Twin Primes less than N

Below are the ways to print all Twin primes less than the given number N.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Let us define the function checkPrimeNum(), which accepts an integer as input and returns True if it is prime and False if it is not.
  • Declare the variable count and set it to zero. The count variable is used here to count the number of Factors of a Number. Whenever we find a factor for the given number, we will increment the value of the count.
  • If the count equals 2, the integer is prime, and the function is defined to return True.
  • Else it will return False.
  • Loop from 2 to the given number N using For loop.
  • If both checkPrimeNum(m) and checkPrimeNum(m+2) return True, print m and m+2 where m is the iterator value of the For loop.
  • The Exit of the Program.

Below is the implementation:

# Let us define the function checkPrimeNum(), which accepts an integer as input and
# returns True if it is prime and False if it is not.


def checkPrimeNum(gvnnum):
    # Declare the variable count and set it to zero.
    # The count variable is used here to count the number of Factors of a Number.
    # Whenever we find a factor for the given number,
    # we will increment the value of the count.
    countfactrs = 0
    for m in range(1, gvnnum+1):
        if gvnnum % m == 0:
            countfactrs = countfactrs + 1
    # If the count equals 2, the integer is prime,
    # and the function is defined to return True.
    if countfactrs == 2:
        return True


# Give the number N as static input and store it in a variable.
numbr = 562
print('The twin primes below the number', numbr, 'are :')
# Loop from 2 to the given number N using For loop.
for m in range(2, numbr):
  # If both checkPrimeNum(m) and checkPrimeNum(m+2) return True,
  # print m and m+2 where m is the iterator value of the For loop.
    if (checkPrimeNum(m) == True and checkPrimeNum(m+2) == True):
        print('('+str(m)+','+str(m+2)+')', end='  ')

Output:

The twin primes below the number 562 are :
(3,5)  (5,7)  (11,13)  (17,19)  (29,31)  (41,43)  (59,61)  (71,73)  (101,103)  (107,109)  (137,139)  (149,151)  (179,181)  
(191,193)  (197,199)  (227,229)  (239,241)  (269,271)  (281,283)  (311,313)  (347,349)  (419,421)  (431,433)  (461,463)
 (521,523)

Method #2: Using For Loop (User Input)

Approach:

  • Give the number N as user input using int(input()) and store it in a variable.
  • Let us define the function checkPrimeNum(), which accepts an integer as input and returns True if it is prime and False if it is not.
  • Declare the variable count and set it to zero. The count variable is used here to count the number of Factors of a Number. Whenever we find a factor for the given number, we will increment the value of the count.
  • If the count equals 2, the integer is prime, and the function is defined to return True.
  • Else it will return False.
  • Loop from 2 to the given number N using For loop.
  • If both checkPrimeNum(m) and checkPrimeNum(m+2) return True, print m and m+2 where m is the iterator value of the For loop.
  • The Exit of the Program.

Below is the implementation:

# Let us define the function checkPrimeNum(), which accepts an integer as input and
# returns True if it is prime and False if it is not.


def checkPrimeNum(gvnnum):
    # Declare the variable count and set it to zero.
    # The count variable is used here to count the number of Factors of a Number.
    # Whenever we find a factor for the given number,
    # we will increment the value of the count.
    countfactrs = 0
    for m in range(1, gvnnum+1):
        if gvnnum % m == 0:
            countfactrs = countfactrs + 1
    # If the count equals 2, the integer is prime,
    # and the function is defined to return True.
    if countfactrs == 2:
        return True


# Give the number N as user input using int(input()) and store it in a variable.
numbr = int(input('Enter some random number = '))
print('The twin primes below the number', numbr, 'are :')
# Loop from 2 to the given number N using For loop.
for m in range(2, numbr):
  # If both checkPrimeNum(m) and checkPrimeNum(m+2) return True,
  # print m and m+2 where m is the iterator value of the For loop.
    if (checkPrimeNum(m) == True and checkPrimeNum(m+2) == True):
        print('('+str(m)+','+str(m+2)+')', end='  ')

Output:

Enter some random number = 842
The twin primes below the number 842 are :
(3,5) (5,7) (11,13) (17,19) (29,31) (41,43) (59,61) (71,73) (101,103) (107,109) (137,139) (149,151) (179,181) (191,193)
(197,199) (227,229) (239,241) (269,271) (281,283) (311,313) (347,349) (419,421) (431,433) (461,463) (521,523)
(569,571) (599,601) (617,619) (641,643) (659,661) (809,811) (821,823) (827,829)

Related Programs: