title: Jumping into c++ (practice on prime factor tree)
body: <p><strong>I got the following exercise:</strong> </p>
  
  <p>Design a program that finds all numbers from 1 to 1000 whose prime factors, when added together, sum up to a prime number (for example, 12 has prime factors of 2, 2, and 3, which sum to 7, which is prime). Implement the code for that algorithm.</p>
  
  <p><strong>My code:</strong></p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;string&gt;
  
  //function prototype
  int PrimeFactor(int number);
  bool isPrime(int PrimeOrNot);
  
  int main() {
      int i;
      for (i = 0; i &lt; 1000; i++)
          PrimeFactor(i);
  }
  
  int PrimeFactor(int number) {
      int i;
      int firstsplit = 0;
      int secondsplit = 0;
      int nextSplit = 0;
      int afternextsplit = 0;
      int sum = 0;
  
      for (i = 2; i &lt; number; i++) //loop that find the number that split it to two number , the purpose is for buiding the factor tree. 
      {
          if (number % i == 0) {
              firstsplit = number / i;
              secondsplit = number / firstsplit;
  
              if (isPrime(firstsplit)) 
                  sum = sum + firstsplit;
  
              if (isPrime(secondsplit)) 
                  sum = sum + secondsplit;
  
              break;
          }
      }
  
      if (isPrime(firstsplit) == false) //check if the splitted number is not already a factor from previous loop.
      {
          while (isPrime(firstsplit) == false) {
              for (i = 2; i &lt; firstsplit; i++) //loop that continue to split the splitted number for the purpose of prime factors.
              {
                  if (firstsplit % i == 0) {
                      nextSplit = firstsplit / i;
                      afternextsplit = firstsplit / nextSplit;
  
                      if (isPrime(nextSplit)) 
                          sum = sum + nextSplit;
  
                      if (isPrime(afternextsplit)) 
                          sum = sum + afternextsplit;
                  }
              }
          }
      }
  }
  
  bool isPrime(int PrimeOrNot) {
      int i;
      for (i = 0; i &lt; PrimeOrNot; i++) {
          if (PrimeOrNot % i == 0) 
              return false;
      }
      return true;
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>I think my program seems to be not so efficient, because there are a lot of variables and I suppose it can be done with less. Can anybody help me a bit with finding another way of doing it by a prime factor tree?</p>
  
  <p><strong>Prime tree example:</strong></p>
  
  <pre><code>    16
     / \
    4    4
   /\   /\
  2  2 2  2
  </code></pre>