I want to store primary numbers in an array up-to n=100000 with an efficient algorithm.I am using the basic method to store prime numbers but it is taking more time.
       void primeArray(){
        int primes[100000],flag=0,k=2; 
        primes[0]=2;
        primes[1]=3;
        for(int i=5;i<n;i=i+2){
                for(int j=2;j<i/2;j++){
                    if(i%j==0){
                    flag=1;
                    break;
                   }
                }
            if(flag==0){
                primes[k]=i;
                k++;
            }
            flag=0;
       }   
     }
 
     
     
     
     
     
    