Question

User enters two numbers, N1, and N2. Write a program that determine if the numbers are...

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

Homework Answers

Answer #1

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)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
User enters two numbers, N1, and N2. Write a program that determine if the numbers are...
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.
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. 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. For example, the five twin prime pairs are (3, 5),...
C++ [Program-2] Write a program that accepts an indefinite set of numbers until the user enters...
C++ [Program-2] Write a program that accepts an indefinite set of numbers until the user enters “-1”. In other words, the program keeps accepting new values until the user provides a “-1” (your program accepts all values, and discards the “-1”). When done, the program prints back to the user: (i) the sum of all numbers entered (except -1), (ii) the minimum value seen across all numbers (except -1), and (iii) the maximum value across all numbers (except -1).
IN C++ PLEASE. Write a program where the user enters a number and you output an...
IN C++ PLEASE. Write a program where the user enters a number and you output an unfilled square of stars. (DO NOT PROMPT THE USER, just take in a number, The only console output should be the squares). For example the program would start with console input, The user would enters 3, you would output (note 3 is the lowest number your program will be tested with) *** * * *** or, The user enters 5, you would output *****...
Write a program that asks the user to enter a series of numbers separated by commas....
Write a program that asks the user to enter a series of numbers separated by commas. Here is an example of valid input: 7,9,10,2,18,6 The program should calculate and print the sum of all the numbers. Sample Run java NumberSum Enter·numbers·separated·by·commas:1,2,3↵ 6↵
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write a program that asks the user to enter an odd number and prints out a...
Write a program that asks the user to enter an odd number and prints out a triangle pattern. For example, if the user enters 5, the output is note: There is one space between the numbers in each line 1 3 5 1 3 1 If the user enters 9 the output is: 1 3 5 7 9 1 3 5 7 1 3 5 1 3 1 program language: C++
Write a program to find all the numbers divisible by 7 between a given range of...
Write a program to find all the numbers divisible by 7 between a given range of numbers [n1, n2] entered by the user using a FOR loop only. write C program. help
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT