Sieve of Eratosthenes Twin Prime Numbers

Hello, I'm trying to print the number of twin prime pairs from the prime numbers that are calculated. For certain numbers, the code works as I want it to but for other numbers(3,5,6,7,8,30,etc.) the program crashes. Can anyone tell me why its crashing?

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
int main()
{
	int userInput, max = 0, count = 0, total = 0, diff = 0, num = 0;
	cout << "Enter a number: ";
	cin >> userInput;
	vector<bool> sieve(userInput + 1, true);
	sieve[0] = false;
	sieve[1] = false;

	int x = sqrt(userInput);
	for (int i = 2; i <= x; i++) {
		if (sieve[i]) {
			for (int j = i * i; j <= userInput; j += i) {
				sieve[j] = false;
			}

		}

	}
	for (int i = 0; i <= userInput; i++) {
		if (sieve[i]) {
			cout << i << " ";
			max = i;

			count++;

		}
		if (sieve[i] < max) {
			diff = max - sieve[i];
			total = diff / count;

		}

		if (sieve[i] && sieve[i + 2]) {
			num++;

		}
	}
	
	cout << endl;
	cout << "Number of Primes: "<< count << endl;
	cout << "Number of Twin Primes: " << num << endl;
	cout << "Max is: " << max << endl;
	cout << "Average is: " << total;
	cout << endl << endl << endl;
}
Last edited on
1
2
3
4
5
vector<bool> sieve(userInput + 1, true);

for (int i = 0; i <= userInput; i++) {

  if (sieve[i] && sieve[i + 2]) {

The userInput is the last valid index of the sieve.
What does happen, if you do access sieve[userInput+2]?
Well I've changed the code around a bit and now it crashes at even numbers.
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
int main()
{
	int userInput, max = 0, count = 0, total = 0, diff = 0, num = 0;
	cout << "Enter a number: ";
	cin >> userInput;
	vector<bool> sieve(userInput + 1, true);
	sieve[0] = false;
	sieve[1] = false;

	int x = sqrt(userInput);
	for (int i = 2; i <= x; i++) {
		if (sieve[i]) {
			for (int j = i * i; j <= userInput; j += i) {
				sieve[j] = false;
			}

		}

	}
	for (int i = 0; i <= userInput; i++) {
		if (sieve[i]) {
			cout << i << " ";
			max = i;

			count++;

		}
		if (sieve[i] < max) {
			diff = max - sieve[i];
			total = diff / count;

		}
	

			
	}
	for (int i = 0; i < userInput; i++) {
		if (sieve[i] && sieve[i + 2]) {
			num++;
		}
	}

	
	cout << endl;
	cout << "Number of Primes: "<< count << endl;
	cout << "Number of Twin Primes: " << num << endl;
	cout << "Max is: " << max << endl;
	cout << "Average is: " << total;
	cout << endl << endl << endl;
}
Last edited on
Im not able to get it to crash, what numbers are you using?
4, 6, 8, 12, 14,18, 20, 30
Last edited on
Uh, keskiverto already posted exactly why the program is crashing. How about y’all take a look at that?
Duthomhas I get what he's saying I just don't know how to fix it. I tried adding a for loop using just the < symbol so it doesn't include the last number but it still crashes.
Last edited on
If you don't know how to fix it then you don't really get what he is saying. Google around "out of bounds errors" for some good reading. Understanding this stuff is basic to using arrays.

Once you really get what is going wrong, then you will see how to fix it. Remember, you control the indices into the array with the loop.
Yes, we do see that you have now:
1
2
vector<bool> sieve( N + 1 );
for ( int i = 0; i < N; i++ ) {

Lets say that K=N+1:
1
2
vector<bool> sieve( K );
for ( int i = 0; i < K-1; i++ ) {

On last iteration you thus access
sieve[i] && sieve[i + 2], where i==(K-1)-1
In other words:
1
2
3
sieve[K-2] && sieve[K - 2 + 2]
// ==
sieve[K-2] && sieve[K]

The sieve has only K elements, and thus its last valid index, K-1, is still smaller than the K that you do use.

How would you get both i and i+2 stay in [0..userInput] in your loop?
Last edited on
I figured it out keskiverto. Thank you so much!
Topic archived. No new replies allowed.