Find all ways to write a number as multiply of numbers (different from 1).
For example:
12 = 12, 12 = 2 * 6, 12 = 3 * 4, 12 = 2 * 2 * 3
I have no idea how to this problem and I have spend days reading about factors, sieves and similar topics
Find all ways to write a number as multiply of numbers (different from 1).
For example:
12 = 12, 12 = 2 * 6, 12 = 3 * 4, 12 = 2 * 2 * 3
I have no idea how to this problem and I have spend days reading about factors, sieves and similar topics
A simplistic recursive approach:
def get_equations(n):
equations = set()
quotient, remainder = divmod(n ** 0.5, 1)
for divisor in range(n - 1, int(quotient if remainder else quotient - 1), -1):
quotient, remainder = divmod(n, divisor)
if remainder == 0:
equations.add(tuple(sorted([divisor, quotient])))
for sub_equation in get_equations(divisor):
equations.add(tuple(sorted([*sub_equation, quotient])))
return equations
print(get_equations(12))
print(get_equations(25))
print(get_equations(81))
OUTPUT
> python3 test.py
{(2, 2, 3), (2, 6), (3, 4)}
{(5, 5)}
{(3, 3, 9), (3, 3, 3, 3), (3, 27), (9, 9)}
>
Instead of explicitly going after the prime divisors first, like all the suggestions in the cited duplicate, I just let it all play out. Not optimal (should avoid duplication rather than use sets to eliminate it) but simple enough to write and doesn't require days of research.
I didn't include your '12 = 12' result as it's really '12 = 12 * 1' and you said 'different from 1'.