Assuming that there is an upper bound on the largest prime (you said something about the 15th prime which would be 47:
see here[
^], you can easily construct an array containing all the allowed primes. Then starting from the smallest prime (2) towards the largest (47 if my assumption was correct) you iterate over said array and try if the number to be factorized can be divided by the current prime number with a modulo of zero (no remainder).
0. If the number cannot be divided without remainder by the current prime goto 3.
1. If the number can be divided by the current prime without remainder, set the number to be tested to the number divided by the prime and set the exponent of the current prime to one.
2. As long as the division stays without remainder do 1.
3. Choose the next prime from the array and continue with 0.
4. The iteration must stop when the number to factorized becomes one, because then the factorization is done. In case my assumption was correct you can also stop the factorization if you run out of primes. The reduced number will then be the remainder of the factorization under the set limitations.
Interesting side note why one is the termination condition:
All numbers can be expressed by a set of tuples {(n
0,m
0),..., (n
i, m
i) } where the first part m
i of the tuple is the prime number and the second part m
i is the exponent for that prime. The factorized number can thus be expressed as
m
0m0 * ... * n
imi
for all the prime numbers there are. Thankfully we don't have to include all the prime numbers there are (and we couldn't do so either even if we wanted as their number is infinite), because any number raised to the power of zero is one an and thus will not change the product in any way.
Since the number 1 cannot be factorized, the exponents for all possible primes are 0 giving a one for each prime number
0 . The product of all infinite number of primes raised to the power of zero is 1 then again 1.
I hope this wasn't to confusing.
Regards,
— Manfred