Given an integer n. we need to print all twin prime number pairs between 1 to n. A Twin prime are those numbers which are prime and having a difference of two ( 2 ) between the two prime numbers. In other words, a twin prime is a prime that has a prime gap of two.
Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime pair. Usually, the pair (2, 3) is not considered to be a pair of twin primes. Since 2 is the only even prime, this pair is the only pair of prime numbers that differ by one; thus twin primes are as closely spaced as possible for any other two primes.
The first few twin prime pairs are :
(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103), (107, 109), (137, 139), …etc.
FACT : There are 409 Twin primes below 10, 000.
Input : n = 15 Output : (3, 5), (5, 7), (11, 13) Input : 25 Output :(3, 5), (5, 7), (11, 13), (17, 19)
Using Sieve of Eratosthenes find the list of all primes smaller than or equal to n and then iterate list again till n and just checks the ith number and heck its (i+2)th number if both are prime then print both the numbers else proceed to next number to find Twin prime.
(3, 5)(5, 7)(11, 13)(17, 19)
- Twin Prime Numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of prime numbers without odd prime digits
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Numbers less than N which are product of exactly two distinct prime numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Prime numbers after prime P with sum S
- Prime Numbers
- Almost Prime Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar