Sep 21, 2018 at 10:31pm UTC
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 Sep 22, 2018 at 1:03am UTC
Sep 21, 2018 at 11:30pm UTC
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 Sep 21, 2018 at 11:30pm UTC
Sep 21, 2018 at 11:41pm UTC
Im not able to get it to crash, what numbers are you using?
Sep 22, 2018 at 12:11am UTC
4, 6, 8, 12, 14,18, 20, 30
Last edited on Sep 22, 2018 at 12:13am UTC
Sep 22, 2018 at 12:22am UTC
Uh, keskiverto already posted exactly why the program is crashing. How about y’all take a look at that?
Sep 22, 2018 at 1:00am UTC
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 Sep 22, 2018 at 2:50am UTC
Sep 22, 2018 at 3:12am UTC
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.
Sep 22, 2018 at 1:36pm UTC
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 Sep 22, 2018 at 1:38pm UTC
Sep 22, 2018 at 2:58pm UTC
I figured it out keskiverto . Thank you so much!