In this article, we cover how to generate twin primes through a program in Python.

Introduction

We have already covered how to Generate Prime numbers in Python. A prime number has only two factors i.e. 1 and itself. When we generate a list of prime numbers we realize that there are two consecutive prime numbers which differ by 2.

So, the pair of consecutive prime numbers which differ by 2 are called twin primes. Let’s understand it with the help of an example. Below is the list of prime numbers to 100.

`[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]`

Here:

(3, 5 )
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)

are twin primes.

Generate twin primes through a Python program

```ran = int(input("Enter a range: "))
p_numbers = []
if ran >= 2:
p_numbers.append(2)
for i in range(3,ran+1):
for j in range(2,i):
if i==j:
continue
elif i % j == 0:
break
else:
p_numbers.append(i)

print(f"\nList of prime numbers till range: {ran}\n{p_numbers}\n")

x = []
for i in range(1, len(p_numbers)):
if (p_numbers[i] - p_numbers[i-1]) == 2:
x.append(p_numbers[i-1])
x.append(p_numbers[i])

print(f"List of twin prime numbers till range: {len(x)}\n{x}\n")

for j in range(0,len(x),2):
print(f"({x[j]},{x[j+1]})\n")```

There are multiple ways to approach the twin primes problem. We chose to continue from where we left off in the article: Generate Prime numbers in Python. So, we already had prime numbers in the list p_numbers. All we have to do now is to find those prime numbers from the list that differ by 2.

Therefore, we used a for loop and checked and stored all those prime numbers in the list p_numbers which differ by 2 in the list x. And, then printed out those twin primes through another for loop.

Testing the code

It prompts us to enter a range, let’s say we take 100. Then it would show a list of all prime numbers to 100. And, then number of twin primes till 100. And, a list of twin prime numbers and pairs. So, this is how it worked out for us:

```Enter a range: 100

List of prime numbers till range: 100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

List of twin prime numbers till range: 16
[3, 5, 5, 7, 11, 13, 17, 19, 29, 31, 41, 43, 59, 61, 71, 73]

(3,5)

(5,7)

(11,13)

(17,19)

(29,31)

(41,43)

(59,61)

(71,73)
```

Conclusion

In this article, we covered how to generate twin primes through a Python program. Twin primes are two consecutive prime numbers that differ by 2. We already had generated a list of prime numbers in the previous article. So, we continued with the same program and checked for the condition where the difference between two consecutive prime numbers was 2.

Read more about Twin prime:

https://en.wikipedia.org/wiki/Twin_prime

Quiz Time

Question I. Which of the following is a twin prime?

A. (2,3)

B. (41,43)

C. (71,83)

D. (31,43)

Question II. How many pairs of twin primes are there between 1 and 200? Hint: If you don’t want to count, then count the number of times for j in range(0,len(x),2): loop runs.

A. 15

B. 18

C. 35

D. 30

Answer I. B

Answer II. A