Twin primes

Thread Solved

Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Twin primes

 
0
  #1
Aug 4th, 2008
i am trying to write a program that finds all twin primes between 1 and 100. any suggestions?
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. int main()
  6. {
  7. int n1, n2, y, x;
  8.  
  9. printf("This program list all the twin primes.\n");
  10.  
  11. for (y = 3; y <= 98; y += 2)
  12. {
  13. if (3 % y != 0 && 5 % y != 0 && 7 % y != 0)
  14. {
  15. y = y;
  16. }
  17. for (x = 3; x <= 98; x += 2)
  18. {
  19. if (3 % x != 0 && 5 % x != 0 && 7 % x != 0)
  20. {
  21. x = x;
  22. }
  23. if(y = x + 2)
  24. {
  25. printf("(%d,%d)\n",x, y);
  26. }
  27. }
  28. }
  29. system("pause");
  30. }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Twin primes

 
0
  #2
Aug 4th, 2008
I'm not sure why you have a nested loop in this program. I don't think you need one. I think I would create a function that returns true or false (1 or 0) based on whether a number is prime. Call this function for n and n + 2. If this function returns true (1) for both n and n + 2, then (n, n + 2) are twin primes. Otherwise no. Check out this wikipedia article:

http://en.wikipedia.org/wiki/Twin_prime

You know in advance that some numbers won't be twin pairs by using some of the theorems in the above link, so you won't even have to call the function for those.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Twin primes

 
0
  #3
Aug 4th, 2008
Originally Posted by plike922 View Post
i am trying to write a program that finds all twin primes between 1 and 100. any suggestions?
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. int main()
  6. {
  7. int n1, n2, y, x;
  8.  
  9. printf("This program list all the twin primes.\n");
  10.  
  11. for (y = 3; y <= 98; y += 2)
  12. {
  13. if (3 % y != 0 && 5 % y != 0 && 7 % y != 0)
  14. {
  15. y = y;
  16. }
  17. for (x = 3; x <= 98; x += 2)
  18. {
  19. if (3 % x != 0 && 5 % x != 0 && 7 % x != 0)
  20. {
  21. x = x;
  22. }
  23. if(y = x + 2)
  24. {
  25. printf("(%d,%d)\n",x, y);
  26. }
  27. }
  28. }
  29. system("pause");
  30. }
Lines 13 and 19. Make sure you have the larger number to the left of the % sign and the smaller number to the right of the % sign.

Lines 15 and 21 don't seem to do anything. x already equals x and y already equals y, so you don't need to assign x to equal x or y to equal y.

Line 23. Make sure not to confuse = and ==.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

Re: Twin primes

 
0
  #4
Aug 4th, 2008
It still doesn't work!
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. int main()
  6. {
  7. int n1, n2, y, x;
  8.  
  9. printf("This program list all the twin primes.\n");
  10.  
  11. for (y = 3; y <= 98; y += 2)
  12. {
  13. if (y % 3 != 0 && y % 5 != 0 && y % 7 != 0)
  14. {
  15. n1 = y;
  16. }
  17. for (x = 3; x <= 98; x += 2)
  18. {
  19. if (x % 3 != 0 && x % 5 != 0 && x % 7 != 0)
  20. {
  21. n2 = x;
  22. }
  23. if(n2 == n1 + 2)
  24. {
  25. printf("(%d,%d)\n", n2, n1);
  26. }
  27. }
  28. }
  29. system("pause");
  30. }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Twin primes

 
0
  #5
Aug 4th, 2008
Originally Posted by plike922 View Post
It still doesn't work!
  1. #include <stdio.h>
  2. #include "genlib.h"
  3. #include "simpio.h"
  4.  
  5. int main()
  6. {
  7. int n1, n2, y, x;
  8.  
  9. printf("This program list all the twin primes.\n");
  10.  
  11. for (y = 3; y <= 98; y += 2)
  12. {
  13. if (y % 3 != 0 && y % 5 != 0 && y % 7 != 0)
  14. {
  15. n1 = y;
  16. }
  17. for (x = 3; x <= 98; x += 2)
  18. {
  19. if (x % 3 != 0 && x % 5 != 0 && x % 7 != 0)
  20. {
  21. n2 = x;
  22. }
  23. if(n2 == n1 + 2)
  24. {
  25. printf("(%d,%d)\n", n2, n1);
  26. }
  27. }
  28. }
  29. system("pause");
  30. }

"It still doesn't work" isn't descriptive enough. It does work, to a point, but you are getting repeats. If x and y both flunk the primality test, n1 and n2 are unchanged and are printed again. That's why you're getting repeats. Your prime testing code weeds out 3, 5, and 7, so you are going to miss those pairs. Consider getting rid of the second for loop, as well as n1 and n2. You only need x and y. x does not need a for loop. It can only be one value for a valid twin prime.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: jaikjoel is an unknown quantity at this point 
Solved Threads: 1
jaikjoel jaikjoel is offline Offline
Newbie Poster

Re: Twin primes

 
0
  #6
Aug 4th, 2008
Would u not be missing out on (3,5) and (5,7) as a pair the way your program is structured?
Last edited by jaikjoel; Aug 4th, 2008 at 4:20 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC