SIGABRT error cause for this program

What is the casuse of SIGABRT error in this program
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>

using namespace std;

bitset<100000> mybits;
/*
  Function print_primes(int x, int y) - prints all the prime nos. between range [x,y].
*/

void print_primes(int a, int b, vector<int> primes)
{
     if (b <= primes[primes.size() - 1]) {
             // numbers are already precalculated just print them.
             for (int i = 0; i < primes.size(); i++) {
                 if (primes[i] >= a && primes[i] <= b)
                    cout << primes[i] << endl;
             }
             
             return;
     }
     
     // use segmented sieve.
     mybits.reset();
     int limit = (int) sqrt(b);
     int temp;
     
     for (int i = a; i <= b; i++) {
         if (mybits[i] == 1) goto end;
         
         for (int j = 0; primes[j] <= limit; j++) {
                  if (i % primes[j] == 0) {
                       mybits.set(i - a);
                       temp = i - a + primes[j];
                       while (1) {
                                 if (temp > b - a) goto end;
                                 mybits.set(temp);
                                 temp = temp + primes[j];
                       }
                  }
         }
         end:
         ;
     }
     
     for (int i = 0; i <= b - a; i++) 
         if (!mybits.test(i)) cout << i + a << endl;

}
int main()
{
    /* Calculate all the primes upto sqrt(10^9) i.e, 31623 using precomputation */
    vector<int> primes;
    primes.push_back(2);
    primes.push_back(3);
    int n, a, b;
    
    for (int i = 5; i < 31623; i = i + 2) {
        for (int j = 2; j <= sqrt(i); j++) {
            if (i % j == 0) {
                  goto end;
            }
        }
        primes.push_back(i);
        end:
        ;
    }
    
    cin >> n;
    
    for (int i = 0; i < n; i++) {
        cin >> a;
        cin >> b;
        
        print_primes(a, b, primes);
        cout << endl;
    }
    return 0;
}   
    


Sample Input
1
500000 600000
terminate called after throwing an instance of 'std::out_of_range'
what():  bitset::set
39: mybits.set(temp);

temp = 100000
Last edited on
If I'm correct it means that I am trying to set a bit position that is out of range right!!!
yes.
Topic archived. No new replies allowed.