Sunday, July 26, 2009

C Programming Question?? Help!?

I need help writing a program in C language that finds twin primes... A twin prime is a prime number that differs from another prime number by two....this is how the program should work.








Please enter how many prime numbers you want the program to find: _





*** then the user enters for example 3.


the program writes:





2 is prime but not a twin prime.


3 is prime but not a twin prime.


4 is not prime.


5 is prime and is a twin prime in the prime pair 3, 5.


6 is not prime.


7 is prime and is a twin prime in the prime pair 5, 7.


8 is not prime.


9 is not prime.


10 is not prime.


11 is prime but not a twin prime.


12 is not prime.


13 is prime and is a twin prime in the prime pair 11, 13.





so the program found and wrote 3 twin primes.








PLEASE HELP!! THANK YOU.

C Programming Question?? Help!?
It is very easy, use a for loop


for(i=2;i%26lt;n;i++)


if(number%i == 0)


/* number is not prime */


..


}
Reply:First find the primes. Try using the method used in the sieve of Aristophanes. You can modify that method to fit your needs. Then find the twins.





The research and programming is left as an exercise to the reader.
Reply:lol ECS30, UCD?


If you know exactly what I am talking about in the above question then I can give a few intentionally vague hints (to protect my own self). If that first question didn't make any sense then disregard my entire post.





1.) You should have a book . . . the program found on page 162 will be helpful. Specifically the function in the first part of it tests whether or not numbers are prime. If you do not know what a function is then read from the beginning of the chapter until that page.


2.) By modifying the prime.c found on the webpage (not the one in the book) and adding in the function you have essentially all that you need. Just add another "if" statement (to the body where the results for prime numbers are) that tests whether (prime number - 2) is prime using the function. (So if prime number - 2 is prime, you know that the number is a twin prime, if not then the number is just prime)


You also need to add variables to count: the number of integers you went through, the number of primes, and the number of twin primes.


3.)I found this post when I was stuck earlier. Spent time a lot of time trying to do it with arrays according to one of the responses, you don't need any arrays at all.





Sorry but that is as much as I feel safe discussing since I don't know how much we're allowed to help each other. It took me literally 12 hours to do because I missed a few lectures and haven't been reading, but hopefully you are better prepared and will finish on time.
Reply:The best help I can offer is the suggestion to pay attention in class and do the reading. This is an easy problem, but I'm not going to do the entire assignment for you.





You need an algorithm to determine if a number is a prime, and a strategy for keeping track of the primes. You could keep an array of primes you've found so far and use that to find twins, or you could ask for every value N that is prime, if N-2 is prime. That would be easier, unless your assignment requires you to do it a certain way.





Other than that the rest is easy. You have probably covered all this in class. Good luck.
Reply:lawl


No comments:

Post a Comment