English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A pair of (positive) integer numbers are called twin primes if they are both prime numbers and the difference between them is 2, i.e. they are consecutive odd numbers and they are prime numbers. (3, 5), (5, 7) and (11, 13) are three examples of such pair of twin prime numbers.

Write a program to display all the pairs of twin prime numbers which are less than 100.

Caution: You need to write a program to find and list all the twin primes. Your program should be easily extended to other ranges.

2007-12-26 16:27:02 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

It needs to be within the program, and not a function.

Thanks.

2007-12-26 16:33:47 · update #1

3 answers

#include
#include

#define LIMIT 100

int isPrime(int n) {
if (n < 2) return 0;
else if (n % 2 == 0) return n == 2;
else {
int i;
int whenToStop = floor(sqrt(n));

for (i = 3; i <= whenToStop; i += 2)
if (n % i == 0) return 0;

return 1;
}
}

int main() {
int i;

for (i = 3; i < LIMIT; ++i)
if (isPrime(i) && isPrime(i - 2))
printf("%u\t%u\n", i - 2, i);

return 0;
}

2007-12-26 21:30:26 · answer #1 · answered by msaeed33 3 · 0 0

Hi. Write a program that finds all twin primes in your range from an external database. Limit to 100. Should find (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73).

2007-12-26 16:31:07 · answer #2 · answered by Cirric 7 · 0 0

do your homework and assignments yourself buddy.

2007-12-26 17:27:20 · answer #3 · answered by Muhammad Salman 2 · 0 0

fedest.com, questions and answers