Python SolutionsPython Solutions

🕰️

Factors

Week 12, 2026

All Solutions

Python - Sieve | BMC | Python Solutions

def count_factors_sieve(limit): factors = [0] * (limit + 1) for i in range(1, limit + 1): for j in range(i, limit + 1, i): factors[j] += 1 return factors limit = 100000 factor_counts = count_factors_sieve(limit) numbers_data = [(n, factor_counts[n]) for n in range(1, limit + 1)] numbers_data.sort(key=lambda x: (-x[1], x[0])) print("Writing to ans.txt...") with open("ans.txt", "w") as f: current_count = None group_count = 0 max_count = numbers_data[0][1] for num, count in numbers_data: if current_count is not None and count != current_count: f.write("\n") group_count += 1 f.write(f"{num} : {count}\n") current_count = count print("Process Completed")