All SolutionsAll Solutions

🕰️

Factors

Week 12, 2026

Build dictionary and print the result | greenya | Odin Solutions

package main import "core:fmt" main :: proc () { // [num of factors] => {list of numbers}; [0] is not used factors := make([dynamic][dynamic] int) for n in 1..=100_000 { // 1 has one factor, any other has two factors (1 and itself) num_of_factors := n == 1 ? 1 : 2 for j in 2..=n/2 do if n%j == 0 do num_of_factors += 1 if num_of_factors >= len(factors) do resize(&factors, num_of_factors + 1) append(&factors[num_of_factors], n) } #reverse for list, num_of_factors in factors { if len(list) == 0 do continue for n in list do fmt.println(n, ":", num_of_factors) fmt.println() } }

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")

python | Peiran D | Python Solutions

//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')