Question
Using C only PLease ! Mathematical Background: Twin Primes are pairs of prime numbers that are almost adjacent; they're separated by a single number, like
Using C only PLease !
Mathematical Background:
Twin Primes are pairs of prime numbers that are almost adjacent; they're separated by a single number, like this:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
In other words, they're distributed like this in the number line:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
(Prime numbers are in bold above; twin primes are in color.)
It is not currently known by mathematicians how many twin primes there are. The largest known twin prime currently is 2,996,863,034,895 21290000 1, which would need about a hundred pages to write out. It is conjectured that there exist infinitely many twin primes (the "twin prime conjecture") but people have been trying to prove it for hundreds of years.
Assignment:
You already know how to write a C program to find prime numbers. This time, adapt that program into a function that tests whether a number is prime, and returns the Boolean value true or false.
Your function prototype should look something like this:
bool isPrime (int n);
Use it like this:
if (isPrime(42)) { printf ("42 is prime "); } else { printf ("42 is not a prime number "); }
The above code fragment should, of course, say "42 is not a prime number".
Rules:
Write a program that asks for an integer limit and then prints out a list of all the twin primes up to that limit.
Your program should never print a number larger than the limit. (ie if the limit is 6, don't print out the pair 5,7)
(2, 3) is not a twin primebecause reasonsso don't print that one.
Example Output:
Choose a limit (greater than 2): 20 All the twin primes less than or equal to 20 are: (3, 5) (5, 7) (11, 13) (17, 19)
Choose a limit (greater than 2): 6 All the twin primes less than or equal to 10 are: (3, 5)
Choose a limit (greater than 2): 1000 All the twin primes less than or equal to 1000 are: (3, 5) (5, 7) (11, 13) (17, 19) (29, 31) (41, 43) (59, 61) (71, 73) (101, 103) (107, 109) (137, 139) (149, 151) (179, 181) (191, 193) (197, 199) (227, 229) (239, 241) (269, 271) (281, 283) (311, 313) (347, 349) (419, 421) (431, 433) (461, 463) (521, 523) (569, 571) (599, 601) (617, 619) (641, 643) (659, 661) (809, 811) (821, 823) (827, 829) (857, 859) (881, 883)
Hints:
Start by writing a function isPrime() that simply always returns true.
Next, make the function work right. Copy your prime number finding code from a previous lab and modify it here to return true if n is prime and false otherwise.
Write a short main() program to test your function on 1, 2, 3, 4, 5, 6, 7, ...
Then write a program that counts up from 3 and considers every pair of numbers (n, n+2) and tests to see if both are prime. If so, you've found one! (Print it out.)
(2, 3) is not a twin prime because mathematicians say so. For this reason, you only need look at odd numbers.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started