[Python] python[Python] python
🕰️
Factors
Week 12, 2026
//Paste your solution here if you want to share it publiclydef order_by_factors_optimized(limit=100000, output_file=None):
factor_counts = [0] * (limit + 1)
for i in range(1, limit + 1):
for j in range(i, limit + 1, i):
factor_counts[j] += 1
numbers = list(range(1, limit + 1))
numbers.sort(key=lambda x: (-factor_counts[x], x))
file_handle = open(output_file, 'w') if output_file else None
current_count = None
for number in numbers:
count = factor_counts[number]
if current_count != count:
if current_count is not None:
line = "\n"
print(line, end='')
if file_handle:
file_handle.write(line)
current_count = count
line = f"{number} : {count}"
print(line)
if file_handle:
file_handle.write(line + "\n")
if file_handle:
file_handle.close()
order_by_factors_optimized(100000, output_file='output.txt')