Simple C++ Help

We had to write a C++ Program to find the first five twin prime pairs of numbers (Twin Primes are primes that are only seperated by two numbers, i.e. 41 and 43).


It's not running :( Can someone point out what I'm doing wrong? I'm using C++ Bloodshed compiler.

Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int count=1;
    int prime=3;
    int fact=2;
    int primeplus=1;
    
    while(count<6){
                   
                   fact=2;
                   
                   while(fact<prime){
                                     if(prime%fact==0){
                                                         prime++;
                                                         
                                                         }
                                     fact++;
                                     }
                   
                   if(primeplus-prime==2){
				   
				   cout<<prime<<" and "<<primeplus<<" are twin primes."<<endl;
				   
				   count++;
				   
				   }
                                          
                   if(prime==fact){
                                     primeplus=prime;}
                                     
                   }
                   
system("pause");
}  

Last edited on
first write a function for checking if a number is prime.
I thought I did. In the first go of the loop, the primeplus-prime if loop results in nothing. The first prime number is assigned to primeplus. After that I subtract the new prime from the primeplus and if the result is two I show them in the output.

I'm getting blank when I run this code. Really confused.

Oh yeah, I made a mistake there. I fixed it, but it's still not working. Here is the revised code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int count=1;
    int prime=3;
    int fact=2;
    int primeplus=3;
    
    while(count<6){
                   
                   fact=2;
                   
                   while(fact<prime){
                                     if(prime%fact==0){
                                                         prime++;
                                                         break;
                                                         
                                                         }
                                     fact++;
                                     }
                   
                   if(primeplus-prime==2){
				   
				   cout<<prime<<" and "<<primeplus<<" are twin primes."<<endl;
				   
				   count++;
				   
				   }
                                          
                   if(prime==fact){
                                     primeplus=prime;}
                                     
                   }
                   
system("PAUSE");
}  


Last edited on
looked at your code and made some remarks. Hope you learn from them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<cmath>
#include <cstdlib>
using namespace std;

int main()
{
    int count=1;
    int prime=3;
    int fact=2;
    int primeplus=3;

    while(count<6)
    {
        fact=2;
        //for first iteration,
        while(fact<prime)  //fac  = 2, prime = 3, condition = true
        {
            if(prime%fact==0)   //3%2 != 0, condition= false, if body does not execute
            {
                prime++;  //failed to increment
                break;
            }
            fact++;  //fac = 3, and while ends coz (fact < prime) is not true
        }//Conclusion: this was useless
        
        if(primeplus-prime==2)  //primeplus = 3, prime = 3, diff = 0, condition false, body does not execute
        {
            cout<<prime<<" and "<<primeplus<<" are twin primes."<<endl;
            count++;

        }
        if(prime==fact) //yes prime == fact as a result from the loop: while(prime < fact)
        {
            primeplus=prime;  //they were already the same
        }//conclusion: (..)

    /* i dont know how you are computing the prime number,
        coz even if all of these were true, primeplus does not change anywhere 
        and will always remain 3, hence, your condition: if(primeplus - pime == 2) will never evaluate to true
    */
    }//count never reaches 6, hence while(count < 6) runs for ever. INFINITE LOOP
    system("PAUSE");
}


This code should do the job for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include <cstdlib>
using namespace std;

bool isPrime(int);

int main()
{
   int nTwinPrimes = 0;
   int a,b,start;

   start = 2;
   while(nTwinPrimes < 6)
   {
       for(int i = start;; i++)
       {
           if(isPrime(i))
           {
                a = i;
                break;
           }

       }
       for(int i = a+1 ;; i++)
       {
           if(isPrime(i))
           {
               b = i;
                break;
           }
       }
       if(b - a == 2)
       {
           cout<<a<<" "<<b<<endl;
            nTwinPrimes++;
       }
    start = b;
   }
   system("pause");
   return 0;
}
bool isPrime(int a)
{
    for(int i = 2; i<= a/2; i++)
    {
        if(a%i == 0)
            return false;
    }
    return true;
}
Thanks for the code, but if I use that it will be plagiarism. We didn't learn about many of those functions in your code yet in our class.

I took your suggestions and changed the code a little bit and am pretty sure it should work as I calculated by hand to see if it works. Please can you tell me what I'm doing wrong this time?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51



#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int count=1;
    int prime=3;
    int fact=2;
    int primeplus=3;
    
    while(count<6){
                   
                   fact=2;
				                      
                   while(fact<prime){
                                     if(prime%fact==0){
                                                         prime++;
                                                         break;
                                                         
                                                         }
                                     
									 fact++;
                                     
                                     }
                   
                                      
                   if(primeplus-prime==2){
				   
				   cout<<prime<<" and "<<primeplus<<" are twin primes."<<endl;
				   
				   count++;
				   
				   }
                                          
                   if(prime==fact){
                                     primeplus=prime;
									 prime++;}
                                     
                   }
                   
system("PAUSE");
}  





Basically I made sure prime changes at the end of the code. So we are not stuck in the situation that was happening in the previous version.
Ok. I was doing the subtraction the wrong way. I fixed it. But I still have a problem. I'm getting the twin primes output twice each.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int count=1;
    int prime=3;
    int fact=2;
    int primeplus=3;
    
    while(count<6){
                   
                   fact=2;
				                      
                   while(fact<prime){
                                     if(prime%fact==0){
                                                         prime++;
                                                         break;
                                                         
                                                         }
                                     
									 fact++;
                                     
                                     }
                   
                                      
                   if(prime-primeplus==2){
				   
				   cout<<prime<<" and "<<primeplus<<" are twin primes."<<endl;
				   
				   count++;
				   
				   }
                                          
                   if(prime==fact){
                                     primeplus=prime;
									 prime++;}
                                     
                   }
                   
system("PAUSE");
}  

something like this ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

bool prime (int number) {
    for ( int i = 2; i < number; i++)
     {  
       if ( number % i == 0 )  
       return false;  //not prime
     }  
    return true; //prime
}

int main () {
  int count=0;
  int ar[10]; //for 5 pair
  for ( int i = 3; count <=10 ; i=i+2 )
    {
	if ( prime( i ) )  
   	  if ( prime( i+2) )  
   	    {
   		  ar[count] = i;
   	  	  ar[count+1] = i+2;
   	  	  count=count+2;   			  		
   	    }
     }
 		
  for (int i: ar)
  cout << i << " ";	
	  
  return 0;
}
Topic archived. No new replies allowed.