Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In the name of ALLAH
- #include<bits/stdc++.h>
- using namespace std;
- #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- #define endl '\n'
- const int mx = 1e6+123;
- bool is_prime[mx];
- vector<int> prime;
- void primegen ( int n )
- {
- for ( int i = 3; i <= n; i += 2 ) is_prime[i] = 1;
- int sq = sqrt ( n );
- for ( int i = 3; i <= sq; i += 2 ) {
- if ( is_prime[i] == 1 ) {
- for ( int j = i*i; j <= n; j += ( i + i ) ) {
- is_prime[j] = 0;
- }
- }
- }
- is_prime[2]=0;
- prime.push_back(2);
- for ( int i = 3; i <= n; i += 2 ) {
- if ( is_prime[i] == 1 ) prime.push_back ( i );
- }
- }
- int main()
- {
- //Prime Generation: complexity 0(n) almost
- primegen(100);
- for(auto u: prime)cout << u << " ";
- cout << endl;//2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement