C++

No description

fork
loading
Files
  • main.cpp
  • example.txt
main.cpp
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
//by rohit j
#include <bits/stdc++.h>
using std::cin;
using std::ifstream;
using std::vector;
using std::cout;
using std::ios_base;

template<class t>
void sieve(t q, t n ,std::ofstream& myfile){
  vector<bool> a(n,1);
  t m = sqrt(n); 
  a[0] = false;
  a[1] = false;
  for(t i = 2 ; i <= m ; i++ ){
    if(a[i]){
      for(t j = i*i ; j <= m ; j+=i ){
        a[j] = false;
      }
    }
  }
  vector<t> primes;
  for(t i = 2 ; i <= m ; i++ ){
      if(a[i]){ primes.emplace_back(i);}
  }
 
  t temp;
  for (t &x : primes) {
        temp = q/x;
        temp*=x;
        for(t y = temp ; y <=n ; y+=x)
            a[y] = false;
        
  }
  for(t i = 0 ; i < primes.size() ;++i){
    a[primes[i]] = true;
  }

  for(t i =q ; i <= n ; i++ ){
    if(a[i]) {myfile << i << "\n";}
  }
}


int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 

  std::ofstream myfile;
  myfile.open("example.txt");
  unsigned long long int q = 2, n = 10;
  sieve(q,n , myfile);
 
  return 0;
}