Twin Prime Conjecture in Python
The twin prime conjecture is a famous unsolved problem in number theory that deals with the distribution of twin prime numbers. Twin primes are pairs of prime numbers that differ by 2, such as (3, 5), (5, 7), (11, 13), and so on. The twin prime conjecture states that there are an infinite number of twin prime pairs.
# Program to check if a number is prime or not
num = 3
prime = []
# To take input from the user
# num = int(input("Enter a number: "))
while num != 10000:
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
num = num + 1
break
else:
prime.append(num)
num = num + 1
# Check for 1, but this program starts at 3 so no need for this.
else:
num = num + 1
end = False
while end == False:
# get first number in list
first = prime[0]
# Iterate through the list of prime numbers
for x in prime:
if x - first == 2:
print(first, " and ", x, " are twin primes!")
else:
continue
prime.pop(0)
# if the list of prime numbers is empty, exit while loop
if not prime:
end = True