User enters two numbers, N1, and N2. Write a program that determine if the numbers are twin primes. Definition: Twin primes are two prime numbers separated by 2. For example, 3 and 5 are both prime numbers and separated by 2. Another example is 17, 19. matlab
Twin Prime Number :-
For Example-
Input: n= 15
Output: (3, 5), (5, 7), (11, 13)
Input: 25
Output: (3, 5), (5, 7), (11, 13), (17, 19)
Program By using Python:-
def printTwinPrime(n):
# Create a boolean array "prime[0..n]"
# and initialize all entries it as
# true. A value in prime[i] will
# finally be false if i is Not a prime,
# else true.
prime = [True for i in range(n + 2)]
p = 2
while (p * p <= n + 1):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, n + 2, p):
prime[i] = False
p += 1
# check twin prime numbers
# display the twin prime numbers
for p in range(2, n-1):
if prime[p] and prime[p + 2]:
print("(",p,",", (p + 2), ")" ,end='')
# driver program
if _name=='main_':
# static input
n = 25
printTwinPrime(n)
Output: (3, 5), (5, 7), (11, 13), (17, 19)
Get Answers For Free
Most questions answered within 1 hours.