I agree with jsmith recursion is not needed.
the algorithm seems to be:
1. Check that the number generated is or is not prime. If it is, the problem is solved. The number has no factors. e.g. if n=151 n is prime - no factors.
2. Try to divide 2 (the 1st and only even prime) into the number as many times as possible without leaving a remainder any time. Divide n by 2 each time to progressively halve the value of n each time. i.e. (n/=2;) Each time you succeeed, print 2. e.g. if n = 144 you would get 2 2 2 2 . You could use a while(n>1) loop to controll this. If the original n is odd, 2 will not be divided into n in this 1st while loop and n will remain unchanged.
3. Pass on to a similar while loop and progressively divide the next prime number into the reduced n. Divide n by the current array prime (in this case 3) each time there is no remainder (as with 2 above)This could be handled by a for loop iterating through the array of primes.
With n = 144 you would get 3 3. So factors of 144 would be 2 2 2 2 3 3 which when multiplied together would produce 144 as a check.
You need to generate each succeeding prime with a prime function or alternatively load an array with all the prime numbers between your desired bounds and compare n with each prime factor until the two equal
e.g. if the number was 366 the routine would return 2 3 61.
Probably the easiest way to control this would be with a simple for loop.
If the chosen bounds were 0 --> 1000 , since there are 168 primes in this range the fore loop inside the while loop would look like:
1 2 3 4
|
for(i=3;i<168;i++) //to iterate through the array of primes.
{if (array[i]==n)
cout<<n<<" ";
break;}
|
e.g. if n was 999 the factors would be 3 3 3 37.
if n was 998 the factors would be 2 499
4. Multiply the factors together to check that their product = n.