title: Find a better algorithm for prime factors of a number
body: <p>I was given an assignement to make a program that that takes a positive int and displays its prime factors.</p>
  
  <p>I observed that when giving numbers bigger than 500000, my program takes a lot of time to find the prime factors.</p>
  
  <p>In the end, i failed, because, when given the integer 776621, the program took longer than 10 seconds to count the results.
  I would like to find out how to optimize my code, so that it would give results faster.
  Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int ft_isprime(int num)
  {
      int i;
      int j;
  
      i = 0;
      j = 2;
      if (num &lt;= 1 &amp;&amp; num &gt;= 0)
          return (0);
      if (num &gt;= 2 &amp;&amp; num &lt;= 3)
          return (1);
      while (j &lt; num)
      {
          if (num % j == 0)
              return (0);
          j++;
      }
      return (1);
  }
  int main(int argc, char **argv)
  {
      int num;
      int divisor;
      int prime_divisors[30];
      int i;
  
      i = 0;
      num = 0;
      if (argc == 2)
          num = atoi(argv[1]);
      divisor = num;
      if (argc != 2)
      {
          printf("\n");
          return (0);
      }
      if (num == 1)
      {
          printf("1\n");
          return (0);
      }
      if (ft_isprime(num))
      {
          printf("%d\n", num);
          return (0);
      }
      while (num &gt; 0 &amp;&amp; divisor &gt; 0)
      {
          if (ft_isprime(divisor))
          {
              if (num % divisor == 0)
              {
                  num = num / divisor;
                  prime_divisors[i] = divisor;
                  i++;
                  continue;
              }
          }
          divisor--;
      }
      while (i &gt; 0)
      {
          if (i == 1)
              printf("%d", prime_divisors[i-1]);
          else
              printf("%d*", prime_divisors[i-1]);
          i--;
      }
      printf("\n");
      return (0);
  }
  </code></pre>
  
  <p>Example of output:</p>
  
  <pre><code>$&gt; ./fprime 225225 | cat -e
  3*3*5*5*7*11*13$
  $&gt; ./fprime 8333325 | cat -e
  3*3*5*5*7*11*13*37$
  $&gt; ./fprime 9539 | cat -e
  9539$
  $&gt; ./fprime 804577 | cat -e
  804577$
  $&gt; ./fprime 42 | cat -e
  2*3*7$
  $&gt; ./fprime 1 | cat -e
  1$
  $&gt; ./fprime | cat -e
  $
  $&gt; ./fprime 42 21 | cat -e
  $
  </code></pre>