Advertisement
sajid161

Class - 26 : Task - 3

Jan 18th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int mx=1e5+123;
  4. bool is_prime[mx];
  5. void prime_gen(int n)
  6. {
  7.     is_prime[2]=1;
  8.     for(int i=3;i<=n;i+=2) is_prime[i]=1;
  9.     for(int i=3;i*i<=n;i+=2)
  10.     {
  11.         if(is_prime[i]==1)
  12.         {
  13.             for(int j=i*i;j<=n;j+=(i+i))
  14.             {
  15.                 is_prime[j]=0;
  16.             }
  17.         }
  18.     }
  19. }
  20. int main()
  21. {
  22.     int lim=1e5;
  23.     prime_gen(lim);
  24.     int n;
  25.     cin>>n;
  26.     if(n==1||n==2) cout<<1<<endl;
  27.     else{
  28.         cout<<2<<endl;
  29.     }
  30.         for(int i=2;i<=n+1;i++)
  31.         {
  32.             if(is_prime[i]==1) cout<<1<<" ";
  33.             else cout<<2<<" ";
  34.         }
  35.  
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement