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. If you just list all of the twin prime numbers, you will get 0 credit.

thank you.

I did my best,but the result is far from what should appear. I'd appreciate it if any body can correct my mistake.

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int i,j,n;
	for (i=3;i<=100;i++)
	{
	     for (j=2;j<=sqrt((double)i)+1;j++);
		 {
			 n=i%j;
			 if (!n==0)
			 {
				 printf("%d,%d\n",i,i);
			 }
		 }
	}
}

---------------------------------------------------------------------------------
I did my best,but the result is far from what should appear. I'd appreciate it if any body can correct my mistake.

You need to first be able to generate prime numbers. When you are able to do that, you
can create some variables to hold the previous and next prime numbers. If their difference is 2, then you got a twin pair. try to code that idea.

since this seems to be for school i will not give you the awnser. but i have quickly coded somthing that you can look at. this is how to find a prime number. once you can do that it should be farly easy to figure out how to find primes 2 numbers apart.

#include <iostream>
#include <math.h>

using namespace std;

int main () {
	int testnumber = 3; //declares the number that will be tested on. this is the first prime
	int findingprime = testnumber - 1; // the number that is used to divid by testnumber to check if there is a remander

	while (testnumber < 100) { //loops while primes are under 100
		if (testnumber%findingprime == 0) { // check for to see if there is no remander and if there is its not a prime number and adds 2 to the testnumber to start testing the next number
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else { //else if there is a remander it takes one of the findingprime veriable and reruns the loop
			findingprime = findingprime - 1;
		}
		while (findingprime == 1) { //if it goes through all the looping and finding prime is equal to 1 then that means testnumber is a prime
			cout << testnumber << "\n"; //print the prime to the screen
			testnumber = testnumber + 2; //moves onto next number
			findingprime = testnumber - 1; // sets findingprime to one lest then testnumber
		}
	}
	cin.get(); // waits for user to press enter to stop the programe.
}

Good Luck

since this seems to be for school i will not give you the awnser. but i have quickly coded somthing that you can look at. this is how to find a prime number. once you can do that it should be farly easy to figure out how to find primes 2 numbers apart.

#include <iostream>
#include <math.h>

using namespace std;

int main () {
	int testnumber = 3; //declares the number that will be tested on. this is the first prime
	int findingprime = testnumber - 1; // the number that is used to divid by testnumber to check if there is a remander

	while (testnumber < 100) { //loops while primes are under 100
		if (testnumber%findingprime == 0) { // check for to see if there is no remander and if there is its not a prime number and adds 2 to the testnumber to start testing the next number
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else { //else if there is a remander it takes one of the findingprime veriable and reruns the loop
			findingprime = findingprime - 1;
		}
		while (findingprime == 1) { //if it goes through all the looping and finding prime is equal to 1 then that means testnumber is a prime
			cout << testnumber << "\n"; //print the prime to the screen
			testnumber = testnumber + 2; //moves onto next number
			findingprime = testnumber - 1; // sets findingprime to one lest then testnumber
		}
	}
	cin.get(); // waits for user to press enter to stop the programe.
}

Good Luck

A somewhat more efficient algorithm for finding primes:
edit: I should note that in addition to being more efficient, this function will store the result in a vector instead of printing it to the screen. This way you can use it for other things, such as solving the problem you were given.

#include <vector>
#include <cmath>

using namespace std;

void findPrimes(vector<int> &primes, const int max_bound)
{
    primes.push_back(2);

    for(int i=3;i<=max_bound;i+=2)
    {
        bool isPrime=true;
        for(vector<int>::const_iterator j=primes.begin();*j<=sqrt(i);j++)
            if(i%*j==0)
            {
                isPrime=false;
                break;
            }
        if(isPrime)
            primes.push_back(i);
    }
    return;
}

Edited 6 Years Ago by Allophyl: n/a

Thanks to cgcgames and Allophyl,

it seems like I have correctly finished the problem,see below:

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int testnumber,findingprime;
	testnumber=3;
	findingprime=testnumber-1;
	while (testnumber < 100)
	{
		if (testnumber%findingprime == 0)
		{
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else {
			findingprime = findingprime - 1;
		}
		while (findingprime == 1)
		{
			if((testnumber+2)%3!=0)
				{
					printf("(%d,%d)\n",testnumber,testnumber+2);
			    }
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
	}
}

The output seems correct, and displays all the twin prime numbers. I would appreciate it if anybody can spot some small errors in it.

Edited 6 Years Ago by qqwushi12345: n/a

Thanks to cgcgames and Allophyl,

it seems like I have correctly finished the problem,see below:

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int testnumber,findingprime;
	testnumber=3;
	findingprime=testnumber-1;
	while (testnumber < 100)
	{
		if (testnumber%findingprime == 0)
		{
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else {
			findingprime = findingprime - 1;
		}
		while (findingprime == 1)
		{
			if((testnumber+2)%3!=0)
				{
					printf("(%d,%d)\n",testnumber,testnumber+2);
			    }
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
	}
}

The output seems correct, and displays all the twin prime numbers. I would appreciate it if anybody can spot some small errors in it.

You're only testing to see if the next odd number following a prime number is divisible by 3, not whether or not it too is actually prime. So you end up with a lot of pairs which aren't twin primes, such as (23,25). 25=5*5 is clearly not a prime number.

I would suggest storing all of the prime numbers in a container such as a vector or an array, and then once you have done so, compare each prime number with the one following it to see if the difference between them is 2. If it is, then you can print that pair.

I should also mention that that is a rather inefficient algorithm for determining prime numbers, as it checks *every* number between 2 and itself minus 1 to see if any of them are divisible. You only really need to check the prime numbers from 2 up to the square root of the number (which, if you're making a list of all of the prime numbers anyway, is rather easy to do).

Edited 6 Years Ago by Allophyl: n/a

Re:Allophyl:

I am just an amateur on this so I don't think I can make big changes on this, I tried my best changing it based on what you said:

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int testnumber,findingprime;
	testnumber=3;
	findingprime=testnumber-1;
	while (testnumber < 100)
	{
		if (testnumber%findingprime == 0)
		{
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else {
			findingprime = findingprime - 1;
		}
		while (findingprime == 1)
		{
			if (testnumber-testnumber==2)
			{
				printf("(%d,%d)",testnumber,testnumber+2);
			}
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
	}
}

Could you point out anyting on this?

Well, the code in this program will produce no output, as testnumber-testnumber==2 will always be false (subtracting a number from itself will always result in 0.

You can however easily solve this without storing the numbers in an array or vector:

Try creating a variable in which to store the last prime number you found. Initialize it to the value 2. When you find a prime number, you will compare it against this value, print the two numbers if the difference between them is 2, then set the variable to the new prime number you just found.

I tried eliminating the hom+2 ones that are nonprime:

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int testnumber,findingprime,hom,j;
	float lim;
	testnumber=3;
	findingprime=testnumber-1;
	while (testnumber < 100)
	{
		if (testnumber%findingprime == 0)
		{
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else {
			findingprime = findingprime - 1;
		}
		while (findingprime == 1)
		{
			hom=testnumber;
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
			lim=sqrt((double)hom)+1;
		    for(j=2;j<=lim;j++)
			{
				if((hom+2)%j!=0)
				{
					printf("(%d,%d)\n",hom,hom+2);
				}
			}
		}
	}   
}

Edited 6 Years Ago by qqwushi12345: n/a

I tried eliminating the hom+2 ones that are nonprime:

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int testnumber,findingprime,hom,j;
	float lim;
	testnumber=3;
	findingprime=testnumber-1;
	while (testnumber < 100)
	{
		if (testnumber%findingprime == 0)
		{
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else {
			findingprime = findingprime - 1;
		}
		while (findingprime == 1)
		{
			hom=testnumber;
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
			lim=sqrt((double)hom)+1;
		    for(j=2;j<=lim;j++)
			{
				if((hom+2)%j!=0)
				{
					printf("(%d,%d)\n",hom,hom+2);
				}
			}
		}
	}   
}

No, all this will do is print hom and hom+2 for every single number that isn't a divisor of hom. Just follow the instructions I gave in my last post, and it'll work fine.

Allophyl, You ARE MY SAVOR. I think I did it. It's almost 12.

#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int testnumber,findingprime,hom,j;
	float lim;
	testnumber=3;
	findingprime=testnumber-1;
	hom=2;
	while (testnumber < 100)
	{
		if (testnumber%findingprime == 0)
		{
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else {
			findingprime = findingprime - 1;
		}
		while (findingprime == 1)
		{
			if (testnumber-hom==2)
			{
				printf("(%d,%d)\n",hom,testnumber);
			}
			hom=testnumber;
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
	}   
}

like whaty Allophyl said :). i was just trying to keep it simple hehe. um but you havent stored the other prime in testing number. what your doing is minusing the same number from eachother becuase you store a diffrent prime in testing number everytime. what i would do is somthing like this.

#include <iostream>
#include <math.h>

using namespace std;

int main () {
	int testnumber = 3; //declares the number that will be tested on. this is the first prime
	int findingprime = testnumber - 1; // the number that is used to divid by testnumber to check if there is a remander
	int lastprimeholder = 3; //holdes the last prime so you can compare it

	while (testnumber < 100) { //loops while primes are under 100
		if (testnumber%findingprime == 0) { // check for to see if there is no remander and if there is its not a prime number and adds 2 to the testnumber to start testing the next number
			testnumber = testnumber + 2;
			findingprime = testnumber - 1;
		}
		else { //else if there is a remander it takes one of the findingprime veriable and reruns the loop
			findingprime = findingprime - 1;
		}
		while (findingprime == 1) { //if it goes through all the looping and finding prime is equal to 1 then that means testnumber is a prime
			if (testnumber-lastprimeholder == 2) {
				cout << lastprimeholder << " " << testnumber << "\n"; //print the pair to the screen
			}
				lastprimeholder = testnumber; //make sure you make the code run this before it moves onto the next prime or you will store the wronge value
				testnumber = testnumber + 2; //moves onto next number
				findingprime = testnumber - 1; // sets findingprime to one lest then testnumber
		}
	}
	cin.get(); // waits for user to press enter to stop the programe.
}

Edited 6 Years Ago by cgcgames: n/a

This article has been dead for over six months. Start a new discussion instead.