Javatpoint Logo
Javatpoint Logo

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.

  1. The pair (3, 5) is a pair of Twin Primes numbers because the difference between both the numbers is 2.
  2. The pair (3, 7) is not a pair of Twin Primes number because the difference of both the numbers is not 2.
  3. The pair (71, 73) is a pair of Twin Primes numbers because the difference between both the numbers is 2.
Twin Prime Numbers

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:

  1. Get pair of numbers from the user to check whether it is twin prime or not.
  2. Check whether both the numbers are prime or not.
  3. Find the difference between both numbers.
  4. If both the numbers are prime and the difference of both the number is 2, print "numbers are the twin prime numbers".
  5. 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:

Twin Prime Numbers

FindAllTwinPrimeNumbers.java

Output:

Twin Prime Numbers




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA