Twin primes program

This program prints out the total number of up to 100,000. What I'm trying to figure out how to do is to print out the number of twin primes up to 100,000. Is it as simple as adding an if statement "if(primesList[j] - i == 2)"?

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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
  int primesCount = 0;
  int i, j;
  bool isPrime;
  int primesList[20000];


  for(i = 2; i < 100000; ++i){

     isPrime = true;

     for(j = 0; j < primesCount; ++j){
if(primesList[j] > sqrt(i))
   break;
        if(i % primesList[j] == 0){
   isPrime = false;
   break;
}
     }

     if(isPrime) {
primesList[primesCount] = i;
        ++primesCount;
     }
  }
  cout << "Number of primes < 100,000 is " << primesCount << endl;
}
}
Is it as simple as adding an if statement "if(primesList[j] - i == 2)"?

Is i always the last prime found?
Is i always the last prime found?


Yes, I believe that 'i' is the always the last prime found
But j is not always a valid index into primesList. On the other hand you know that if primesCount is greater than one, the last two elements of primesList are the last two primes found. Taking the difference of those will tell you if they're twin primes.
On the other hand you know that if primesCount is greater than one, the last two elements of primesList are the last two primes found. Taking the difference of those will tell you if they're twin primes.


Then what would represent the last two elements in the code? How would I write that?
Topic archived. No new replies allowed.