Question: I am not able to meet all the conditions of prime factors of a number so that I find the correct sum. 

```
    int sumPrime(number){

         int counter;
         int sum=0;
         for(counter=2;counter<=(number/2);counter++){
           if(number%counter==0){
              sum=counter;
              counter++;
              sum+=counter;

              }
           return sum;  

          }
    }

```

Answer: 
I think the code is self explanatory. It works for `number >= 2`.

```
int sumPrime(int number){
     int factor = 2;
     int sum=0;

    while(1 != number){ //Repeat the loop till number becomes 1.
       if(number%factor==0){ //Check if factor divides the number.
          number /= factor; //If yes, update the number.
          sum+=factor; //Add factor to sum.
          printf("Factor [%d]\n", factor);
          continue;
        }
        factor++; //If the current number is not a factor, check the next number.
    }
    printf("Sum [%d]\n", sum);
    return sum;
}

```

Output for 12:

```
Factor [2]                                                                                                                                      
Factor [2]                                                                                                                                      
Factor [3]                                                                                                                                      
Sum [7] 

```
An efficient algorithm to find the prime factors

**Code:**

```
#include <stdio.h>
#include <math.h>

int sumPrime(int n) {
    int sum = 0;
    printf ("%6d: ", n);

    // find all powers of 2
    while (n%2 == 0) {
        printf("%d+", 2);
        n /= 2;
        sum += 2;
    }

    // checking for factors up to sqrt(n) is sufficient
    for (int i = 3; i <= sqrt(n); i += 2) {
        // find all powers of i
        while (n%i == 0) {
            printf("%d+", i);
            n /= i;
            sum += i;
        }
    }

    // n is prime at this point
    if (n > 2) {
        printf ("%d", n);
        sum += n;
    }

    printf (" = %d\n", sum);
    return sum;
}

void main() {
    sumPrime(226);
    sumPrime(39);
    sumPrime(775);
}

```

**Output:**

```
   226: 2+113 = 115
    39: 3+13 = 16
   775: 5+5+31 = 41

```