Twin Prime Numbers
The Twin Prime numbers are the two Prime numbers having a difference of 2 between both of them. A prime having a prime gap of two is referred to as a twin prime. The twin prime term is used for a pair of twin primes. The twin primes are also known as prime pair or prime twin.
The pair (2, 3) is generally not considered as a pair of twin prime because 2 is the only even prime, and the pair is the only pair of prime numbers that differ from one.
Let’s take some examples of Twin Prime numbers.
- The pair (3, 5) is a pair of Twin Primes numbers because the difference between both the numbers is 2.
- The pair (3, 7) is not a pair of Twin Primes number because the difference of both the numbers is not 2.
- The pair (71, 73) is a pair of Twin Primes numbers because the difference between both the numbers is 2.
These are some of the twin prime pairs:
Note: Each and every pair of twin prime except (3, 5) is of the form (6n – 1, 6n + 1) for natural number.
Steps to check twin prime numbers
In order to check whether the numbers are twin prime or not, we have to follow the following steps:
- Get pair of numbers from the user to check whether it is twin prime or not.
- Check whether both the numbers are prime or not.
- Find the difference between both numbers.
- If both the numbers are prime and the difference of both the number is 2, print “numbers are the twin prime numbers”.
- If not, then print “numbers are not the twin prime numbers.”
Let’s implement the code to check whether the numbers are twin prime numbers or not.
TwinPrimeNumbers.java
Output:
FindAllTwinPrimeNumbers.java
Output: