Hi everybody, I need a little help and advice with this code snippet.

I want the function to take an integer, return a pointer to an array of ints on the heap. This array will contain all of the factors.

Please point out where I could optimize or beautify my code.

Anyway, here it is:

-- PlacidCode:const int MaxFactors = 30; int* Factorize(int Number) { int* Factors = new int[MaxFactors]; int* pFactor; int possibleFactor = 3; int counter = 0; int startNumber = Number; if(Number == 1) { pFactor = new int(1); Factors[counter] = *pFactor; delete pFactor; pFactor = 0; counter++; } for(counter; counter < MaxFactors; ) { if(Number % 2 == 0) { pFactor = new int(2); Factors[counter] = *pFactor; delete pFactor; pFactor = 0; Number /= 2; counter++; } else break; } for(counter; counter < MaxFactors && possibleFactor <= Number; ) { if(Number % possibleFactor == 0) { while(Number % possibleFactor == 0) { pFactor = new int(possibleFactor); Factors[counter] = *pFactor; delete pFactor; pFactor = 0; Number /= possibleFactor; counter++; } } else possibleFactor += 2; } if(Factors[0] == startNumber) { // Primenumber, no factors if(Factors[0] == 2) { pFactor = new int(2); Factors[0] = *pFactor; delete pFactor; pFactor = 0; } else if(Factors[0] == 1) { pFactor = new int(1); Factors[0] = *pFactor; delete pFactor; pFactor = 0; } else return Factors; } // end if(Factors[0] == startNumber) return Factors; } // end Factorize(int Number)