| | |
Twin primes
Thread Solved |
i am trying to write a program that finds all twin primes between 1 and 100. any suggestions?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include "genlib.h" #include "simpio.h" int main() { int n1, n2, y, x; printf("This program list all the twin primes.\n"); for (y = 3; y <= 98; y += 2) { if (3 % y != 0 && 5 % y != 0 && 7 % y != 0) { y = y; } for (x = 3; x <= 98; x += 2) { if (3 % x != 0 && 5 % x != 0 && 7 % x != 0) { x = x; } if(y = x + 2) { printf("(%d,%d)\n",x, y); } } } system("pause"); }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
i am trying to write a program that finds all twin primes between 1 and 100. any suggestions?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include "genlib.h" #include "simpio.h" int main() { int n1, n2, y, x; printf("This program list all the twin primes.\n"); for (y = 3; y <= 98; y += 2) { if (3 % y != 0 && 5 % y != 0 && 7 % y != 0) { y = y; } for (x = 3; x <= 98; x += 2) { if (3 % x != 0 && 5 % x != 0 && 7 % x != 0) { x = x; } if(y = x + 2) { printf("(%d,%d)\n",x, y); } } } system("pause"); }
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 ==.
It still doesn't work!
C Syntax (Toggle Plain Text)
#include <stdio.h> #include "genlib.h" #include "simpio.h" int main() { int n1, n2, y, x; printf("This program list all the twin primes.\n"); for (y = 3; y <= 98; y += 2) { if (y % 3 != 0 && y % 5 != 0 && y % 7 != 0) { n1 = y; } for (x = 3; x <= 98; x += 2) { if (x % 3 != 0 && x % 5 != 0 && x % 7 != 0) { n2 = x; } if(n2 == n1 + 2) { printf("(%d,%d)\n", n2, n1); } } } system("pause"); }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
It still doesn't work!
C Syntax (Toggle Plain Text)
#include <stdio.h> #include "genlib.h" #include "simpio.h" int main() { int n1, n2, y, x; printf("This program list all the twin primes.\n"); for (y = 3; y <= 98; y += 2) { if (y % 3 != 0 && y % 5 != 0 && y % 7 != 0) { n1 = y; } for (x = 3; x <= 98; x += 2) { if (x % 3 != 0 && x % 5 != 0 && x % 7 != 0) { n2 = x; } if(n2 == n1 + 2) { printf("(%d,%d)\n", n2, n1); } } } system("pause"); }
"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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Wats the problem in this Prog???
- Next Thread: help with windows.h..
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






