title: C factoring a number into prime factors
body: <p>I wrote a program that factors numbers into its prime factors, then stores them in a vector, and lastly asks whether to verify the result by multiplying them out.</p>
  
  <p>It works this way: ask for a number (<code>num</code> in the code), and divide it by 2 and up.</p>
  
  <p>If it finds a number (the <code>divisor</code> in the code) whose modulo (when <code>num</code> mod <code>divisor</code>) is zero, store that divisor into a vector, and reduce <code>num</code> by dividing it by the <code>divisor</code> and store it into <code>temp</code>, and reset the divisor to 1 (and the last statement in the <code>while</code> loop will increment it to 2. If no such number is found, <code>divisor</code> increases until it is greater or equal to <code>num</code>. This process continues until <code>divisor</code> is greater than <code>num</code>.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  int main() {
  
      //num=the number of interest
      //divisor=the number dividing the number of interest each time
      unsigned long  divisor=2, num, temp ; //num=13699293826d
      char c;
  
      vector&lt;unsigned long&gt; divisors;
      cout&lt;&lt;"Enter a number: "&lt;&lt;endl;
      cin&gt;&gt;num;
  
      //temp stores the number that is reduced each time
      temp=num;
  
      while(divisor&lt;=num)
      {
          if(temp%divisor==0)
          {
               temp=temp/divisor;
               divisors.push_back(divisor);
               cout&lt;&lt;"one "&lt;&lt;divisor&lt;&lt;endl;
               cout&lt;&lt;"the number of interest is now"&lt;&lt;temp&lt;&lt;endl;
               divisor=1;
          }
          if(divisor==temp&amp;&amp;temp!=1)
          {
              cout&lt;&lt;"two " &lt;&lt; divisor&lt;&lt;endl;
              divisors.push_back(divisor);
          }
  
          divisor++;
      }
  
      if(divisors[0]==num)
      {
          cout&lt;&lt;"The number: "&lt;&lt;num&lt;&lt;" is prime. ";
      }
      else
      {
          cout&lt;&lt;"Its proper divisors are: ";
          for(unsigned int count=0; count&lt;divisors.size(); count++ )
          {
              cout&lt;&lt;divisors[count]&lt;&lt;"\t";
          }
      }
  
      cout&lt;&lt;"Print out the multiplication? Press 'Y' or 'N'."&lt;&lt;endl;
      cin&gt;&gt;c;
  
      if(c=='Y'||c=='y')
      {
          for(unsigned int count=0; count&lt;divisors.size(); count++)
          {
              temp*=divisors[count];
              cout&lt;&lt;temp&lt;&lt;"\t";
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>I have printed some debug <code>cout</code> statements.</p>
  
  <p>The problem I have with it is this: when the number is sufficiently large, the debug 
  statement "the number of interest is now", has the number 1 following it.
  Then, the program crashes.</p>
  
  <p>What is wrong with the code?</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>And yes, I am running it on 64 bits.</p>
  
  <p>A sample program output:</p>
  
  <pre><code>    Enter a number: 
      13699293826
      one 3
      the number of interest is now: 1431655765
      one 5
      the number of interest is now: 286331153
      one 17
      the number of interest is now: 16843009
      one 257
      the number of interest is now: 65537
      one 65537
      the number of interest is now: 1
  </code></pre>
  
  <p>And then the program crashes.</p>
  
  <p>I also noticed that the first "prime factor" of 3 is incorrect, because 13699293826 divided by 3 is 4562761275.333333333333333.....</p>
  
  <p>edit # 2------------------------------------------</p>
  
  <pre><code>    temp 65537, divisor 62287
  
      ..............omitted output
  
      temp 65537, divisor 65530
      temp 65537, divisor 65531
      temp 65537, divisor 65532
      temp 65537, divisor 65533
      temp 65537, divisor 65534
      temp 65537, divisor 65535
      temp 65537, divisor 65536
      temp 65537, divisor 65537
      one 65537
      the number of interest is now: 1
      Its proper divisors are: 3  5   17  257 65537   Print out the                 multiplication? Press 'Y' or 'N'.
  </code></pre>
  
  <p>And then the program stops responding, and it does not work when I press "y" and enter.</p>
  
  <p>Also, the number multiplied are not correct; the result is 4294967295... after googling, it says it is "the highest number you can get using 32 bits (BInary digiTS)". But on my PC, it says the operating system is 64 bit.</p>