- #106
rgrig
- 34
- 1
prime factorizations
Now, that is a nice problem..
So, you are given a number [tex]N[/tex] and you want to produce all tuples [tex](p_1, \ldots, p_k)[/tex] such that forall i the number p_i is prime and [tex]p_1 \ldots p_k = N[/tex]. Well, take all prime numbers [tex]p_1[/tex] such that [tex]N' = N / p_1[/tex] is an integer and complete the tuple with all solutions for [tex]N'[/tex]. The case when [tex]N[/tex] is prime is trivial.
You need one extra twist to ensure uniqueness.
ek said:So how do I make all 30 prime factorizations happen? I've been staring at this code for some time now and I can't figure it out.
Now, that is a nice problem..
So, you are given a number [tex]N[/tex] and you want to produce all tuples [tex](p_1, \ldots, p_k)[/tex] such that forall i the number p_i is prime and [tex]p_1 \ldots p_k = N[/tex]. Well, take all prime numbers [tex]p_1[/tex] such that [tex]N' = N / p_1[/tex] is an integer and complete the tuple with all solutions for [tex]N'[/tex]. The case when [tex]N[/tex] is prime is trivial.
You need one extra twist to ensure uniqueness.
Code:
factoring(min_prime, number)
if number = 1 return {()} /* one empty factoring */
sol = {} /* empty set */
for i = min_prime to number, where i is prime and a divisor of number
append to sol the result of prepending i to each factoring(i, number / i)
return sol