title: prime factorization using sieve method
body: <p>What's wrong with my code ?
  <br>I am trying  to find the prime factor, but it didn't work after getting input it crash's.</p>
  
  <p>What should I do now ? I am using separate method to generate the primes after generating prime i just call the prime function in main. </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstdio&gt;
  #include&lt;math.h&gt;
  #define SIZE 1000000
  using namespace std;
  long p[SIZE],input;
  long List[SIZE];  // saves the List
  long listSize;   // saves the size of List
  
  void prime(void)
  {
         long i,j;
         p[0]=1;
         p[1]=1;
         for(i=2;i&lt;=sqrt(SIZE);i++)  // prime generate part
              if(p[i]==0)
                  for(j=2;j*i&lt;=SIZE;j++)
                      p[i*j]=1;
  }
  
  void primeFactorize( long n )
  {
      listSize = 0;   // Initially the List is empty, we denote that size = 0
      long sqrtN = long( sqrt(n) ); // find the sqrt of the number
      for( long i = 0; p[i] &lt;= sqrtN; i++ ) { // we check up to the sqrt
          if( n % p[i] == 0 ) { // n is multiple of prime[i]
              // So, we continue dividing n by prime[i] as long as possible
              while( n % p[i] == 0 ) {
                  n /= p[i]; // we have divided n by prime[i]
                  List[listSize] = p[i]; // added the ith prime in the list
                  listSize++; // added a prime, so, size should be increased
              }
              // we can add some optimization by updating sqrtN here, since n
              // is decreased. think why it's important and how it can be added
          }
      }
      if( n &gt; 1 )
      {
          // n is greater than 1, so we are sure that this n is a prime
          List[listSize] = n; // added n (the prime) in the list
          listSize++; // increased the size of the list
      }
  }
  int main()
  {
      prime();    
      while(scanf_s("%ld",&amp;input),input)
      {
            if(input==1)
                  printf("1 = 1\n");
              else if(input==-1)
                  printf("-1 = -1 x 1\n");
              else
              {
                  primeFactorize( input );
  
              }
      for( long i = 0; i &lt; listSize; i++ ) // traverse the List array
                  printf("%d ", List[i]);
  
      }
      return 0;
  }
  </code></pre>