i am trying to write a program that finds all twin primes between 1 and 100. any suggestions?

#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");
}

## All 5 Replies

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:

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.

i am trying to write a program that finds all twin primes between 1 and 100. any suggestions?

#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 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 ==.

It still doesn't work!

#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!

#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.

Would u not be missing out on (3,5) and (5,7) as a pair the way your program is structured?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.