Monday, July 27, 2009

Please help me with this C programming assignment quick?

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.

Please help me with this C programming assignment quick?
#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;





#define LIMIT 100





int isPrime(int n) {


if (n %26lt; 2) return 0;


else if (n % 2 == 0) return n == 2;


else {


int i;


int whenToStop = floor(sqrt(n));





for (i = 3; i %26lt;= whenToStop; i += 2)


if (n % i == 0) return 0;





return 1;


}


}





int main() {


int i;





for (i = 3; i %26lt; LIMIT; ++i)


if (isPrime(i) %26amp;%26amp; isPrime(i - 2))


printf("%u\t%u\n", i - 2, i);





return 0;


}
Reply: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).
Reply:do your homework and assignments yourself buddy.


No comments:

Post a Comment