← All problems
Problem 485
Maximum Number of Divisors — S(10^8, 10^5) via τ sieve + most-recent window max.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log log n)
Space complexity O(n)O(n)
Approach Flow solution Sieve-based divisor sums
Verdict Suboptimal
Flow source
# Project Euler 485
# Maximum Number of Divisors — S(10^8, 10^5) via τ sieve + most-recent window max.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let LIMIT: i64 = 100000000
let BLOCK: i64 = 100000
let tau: ptr<i16> = calloc(LIMIT + 1, 2)
if tau == null { return 1 }
# O(n log n) divisor count sieve — correct for all n
let mut i: i64 = 1
while i <= LIMIT {
let mut j: i64 = i
while j <= LIMIT {
tau[j] = ((tau[j] as i64) + 1) as i16
j = j + i
}
i = i + 1
}
# most_recent[d] = latest index with tau == d; mr_len-1 is current window max
let CAP: i64 = 1024
let most_recent: ptr<i32> = calloc(CAP, 4)
if most_recent == null { return 1 }
let mut mr_len: i64 = 0
i = 0
while i < BLOCK {
let current: i64 = tau[i] as i64
if current >= mr_len {
let mut t: i64 = mr_len
while t <= current {
most_recent[t] = 0
t = t + 1
}
mr_len = current + 1
}
most_recent[current] = i as i32
i = i + 1
}
let mut result: i64 = 0
i = BLOCK
while i <= LIMIT {
let too_far: i64 = i - BLOCK
while mr_len > 0 {
if (most_recent[mr_len - 1] as i64) > too_far { break }
mr_len = mr_len - 1
}
let current2: i64 = tau[i] as i64
if current2 >= mr_len {
let mut t2: i64 = mr_len
while t2 <= current2 {
most_recent[t2] = 0
t2 = t2 + 1
}
mr_len = current2 + 1
}
most_recent[current2] = i as i32
result = result + (mr_len - 1)
i = i + 1
}
printf("%lld\n", result)
free(most_recent)
free(tau)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
int32_t main(void);
int32_t main(void) {
int64_t LIMIT = 100000000;
int64_t BLOCK = 100000;
int16_t* tau = (int16_t*)(calloc((LIMIT + 1), 2));
if (tau == NULL) {
return 1;
}
int64_t i = 1;
while (i <= LIMIT) {
int64_t j = i;
while (j <= LIMIT) {
tau[j] = ((int16_t)((((int64_t)(tau[j])) + 1)));
j = (j + i);
}
i = (i + 1);
}
int64_t CAP = 1024;
int32_t* most_recent = (int32_t*)(calloc(CAP, 4));
if (most_recent == NULL) {
return 1;
}
int64_t mr_len = 0;
i = 0;
while (i < BLOCK) {
int64_t current = ((int64_t)(tau[i]));
if (current >= mr_len) {
int64_t t = mr_len;
while (t <= current) {
most_recent[t] = 0;
t = (t + 1);
}
mr_len = (current + 1);
}
most_recent[current] = ((int32_t)(i));
i = (i + 1);
}
int64_t result = 0;
i = BLOCK;
while (i <= LIMIT) {
int64_t too_far = (i - BLOCK);
while (mr_len > 0) {
if (((int64_t)(most_recent[(mr_len - 1)])) > too_far) {
break;
}
mr_len = (mr_len - 1);
}
int64_t current2 = ((int64_t)(tau[i]));
if (current2 >= mr_len) {
int64_t t2 = mr_len;
while (t2 <= current2) {
most_recent[t2] = 0;
t2 = (t2 + 1);
}
mr_len = (current2 + 1);
}
most_recent[current2] = ((int32_t)(i));
result = (result + (mr_len - 1));
i = (i + 1);
}
printf("%lld\n", result);
free(most_recent);
free(tau);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @main() -> i32 {
%0 = arith.constant 100000000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 100000 : i32
%3 = arith.extsi %2 : i32 to i64
%5 = arith.constant 1 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.addi %1, %7 : i64
%8 = arith.constant 2 : i32
%9 = arith.extsi %8 : i32 to i64
%4 = func.call @calloc(%6, %9) : (i64, i64) -> !llvm.ptr
%10 = llvm.mlir.zero : !llvm.ptr
%11 = llvm.icmp "eq" %4, %10 : !llvm.ptr
cf.cond_br %11, ^bb0, ^bb1
^bb0:
%12 = arith.constant 1 : i32
func.return %12 : i32
^bb1:
cf.br ^bb2
^bb2:
%13 = arith.constant 1 : i32
%14 = arith.extsi %13 : i32 to i64
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i64 : (i64) -> !llvm.ptr
llvm.store %14, %16 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%17 = llvm.load %16 : !llvm.ptr -> i64
%18 = arith.cmpi sle, %17, %1 : i64
cf.cond_br %18, ^bb4, ^bb5
^bb4:
%19 = llvm.load %16 : !llvm.ptr -> i64
%20 = llvm.mlir.constant(1 : i64) : i64
%21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
llvm.store %19, %21 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%22 = llvm.load %21 : !llvm.ptr -> i64
%23 = arith.cmpi sle, %22, %1 : i64
cf.cond_br %23, ^bb7, ^bb8
^bb7:
%25 = llvm.load %21 : !llvm.ptr -> i64
%26 = llvm.getelementptr %4[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i16
%24 = llvm.load %26 : !llvm.ptr -> i16
%27 = arith.extsi %24 : i16 to i64
%28 = arith.constant 1 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.addi %27, %30 : i64
%31 = arith.trunci %29 : i64 to i16
%32 = llvm.load %21 : !llvm.ptr -> i64
%33 = llvm.getelementptr %4[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i16
llvm.store %31, %33 : i16, !llvm.ptr
%34 = llvm.load %21 : !llvm.ptr -> i64
%35 = llvm.load %16 : !llvm.ptr -> i64
%36 = arith.addi %34, %35 : i64
llvm.store %36, %21 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%37 = llvm.load %16 : !llvm.ptr -> i64
%38 = arith.constant 1 : i32
%40 = arith.extsi %38 : i32 to i64
%39 = arith.addi %37, %40 : i64
llvm.store %39, %16 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%41 = arith.constant 1024 : i32
%42 = arith.extsi %41 : i32 to i64
%44 = arith.constant 4 : i32
%45 = arith.extsi %44 : i32 to i64
%43 = func.call @calloc(%42, %45) : (i64, i64) -> !llvm.ptr
%46 = llvm.mlir.zero : !llvm.ptr
%47 = llvm.icmp "eq" %43, %46 : !llvm.ptr
cf.cond_br %47, ^bb9, ^bb10
^bb9:
%48 = arith.constant 1 : i32
func.return %48 : i32
^bb10:
cf.br ^bb11
^bb11:
%49 = arith.constant 0 : i32
%50 = arith.extsi %49 : i32 to i64
%51 = llvm.mlir.constant(1 : i64) : i64
%52 = llvm.alloca %51 x i64 : (i64) -> !llvm.ptr
llvm.store %50, %52 : i64, !llvm.ptr
%53 = arith.constant 0 : i32
%54 = arith.extsi %53 : i32 to i64
llvm.store %54, %16 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%55 = llvm.load %16 : !llvm.ptr -> i64
%56 = arith.cmpi slt, %55, %3 : i64
cf.cond_br %56, ^bb13, ^bb14
^bb13:
%58 = llvm.load %16 : !llvm.ptr -> i64
%59 = llvm.getelementptr %4[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i16
%57 = llvm.load %59 : !llvm.ptr -> i16
%60 = arith.extsi %57 : i16 to i64
%61 = llvm.load %52 : !llvm.ptr -> i64
%62 = arith.cmpi sge, %60, %61 : i64
cf.cond_br %62, ^bb15, ^bb16
^bb15:
%63 = llvm.load %52 : !llvm.ptr -> i64
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
llvm.store %63, %65 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%66 = llvm.load %65 : !llvm.ptr -> i64
%67 = arith.cmpi sle, %66, %60 : i64
cf.cond_br %67, ^bb19, ^bb20
^bb19:
%68 = arith.constant 0 : i32
%69 = llvm.load %65 : !llvm.ptr -> i64
%70 = llvm.getelementptr %43[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %68, %70 : i32, !llvm.ptr
%71 = llvm.load %65 : !llvm.ptr -> i64
%72 = arith.constant 1 : i32
%74 = arith.extsi %72 : i32 to i64
%73 = arith.addi %71, %74 : i64
llvm.store %73, %65 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%75 = arith.constant 1 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.addi %60, %77 : i64
llvm.store %76, %52 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%78 = llvm.load %16 : !llvm.ptr -> i64
%79 = arith.trunci %78 : i64 to i32
%80 = llvm.getelementptr %43[%60] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %79, %80 : i32, !llvm.ptr
%81 = llvm.load %16 : !llvm.ptr -> i64
%82 = arith.constant 1 : i32
%84 = arith.extsi %82 : i32 to i64
%83 = arith.addi %81, %84 : i64
llvm.store %83, %16 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%85 = arith.constant 0 : i32
%86 = arith.extsi %85 : i32 to i64
%87 = llvm.mlir.constant(1 : i64) : i64
%88 = llvm.alloca %87 x i64 : (i64) -> !llvm.ptr
llvm.store %86, %88 : i64, !llvm.ptr
llvm.store %3, %16 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%89 = llvm.load %16 : !llvm.ptr -> i64
%90 = arith.cmpi sle, %89, %1 : i64
cf.cond_br %90, ^bb22, ^bb23
^bb22:
%91 = llvm.load %16 : !llvm.ptr -> i64
%92 = arith.subi %91, %3 : i64
cf.br ^bb24
^bb24:
%93 = llvm.load %52 : !llvm.ptr -> i64
%94 = arith.constant 0 : i32
%96 = arith.extsi %94 : i32 to i64
%95 = arith.cmpi sgt, %93, %96 : i64
cf.cond_br %95, ^bb25, ^bb26
^bb25:
%98 = llvm.load %52 : !llvm.ptr -> i64
%99 = arith.constant 1 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.subi %98, %101 : i64
%102 = llvm.getelementptr %43[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%97 = llvm.load %102 : !llvm.ptr -> i32
%103 = arith.extsi %97 : i32 to i64
%104 = arith.cmpi sgt, %103, %92 : i64
cf.cond_br %104, ^bb27, ^bb28
^bb27:
cf.br ^bb26
^bb28:
cf.br ^bb29
^bb29:
%105 = llvm.load %52 : !llvm.ptr -> i64
%106 = arith.constant 1 : i32
%108 = arith.extsi %106 : i32 to i64
%107 = arith.subi %105, %108 : i64
llvm.store %107, %52 : i64, !llvm.ptr
cf.br ^bb24
^bb26:
%110 = llvm.load %16 : !llvm.ptr -> i64
%111 = llvm.getelementptr %4[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i16
%109 = llvm.load %111 : !llvm.ptr -> i16
%112 = arith.extsi %109 : i16 to i64
%113 = llvm.load %52 : !llvm.ptr -> i64
%114 = arith.cmpi sge, %112, %113 : i64
cf.cond_br %114, ^bb30, ^bb31
^bb30:
%115 = llvm.load %52 : !llvm.ptr -> i64
%116 = llvm.mlir.constant(1 : i64) : i64
%117 = llvm.alloca %116 x i64 : (i64) -> !llvm.ptr
llvm.store %115, %117 : i64, !llvm.ptr
cf.br ^bb33
^bb33:
%118 = llvm.load %117 : !llvm.ptr -> i64
%119 = arith.cmpi sle, %118, %112 : i64
cf.cond_br %119, ^bb34, ^bb35
^bb34:
%120 = arith.constant 0 : i32
%121 = llvm.load %117 : !llvm.ptr -> i64
%122 = llvm.getelementptr %43[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %120, %122 : i32, !llvm.ptr
%123 = llvm.load %117 : !llvm.ptr -> i64
%124 = arith.constant 1 : i32
%126 = arith.extsi %124 : i32 to i64
%125 = arith.addi %123, %126 : i64
llvm.store %125, %117 : i64, !llvm.ptr
cf.br ^bb33
^bb35:
%127 = arith.constant 1 : i32
%129 = arith.extsi %127 : i32 to i64
%128 = arith.addi %112, %129 : i64
llvm.store %128, %52 : i64, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
%130 = llvm.load %16 : !llvm.ptr -> i64
%131 = arith.trunci %130 : i64 to i32
%132 = llvm.getelementptr %43[%112] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %131, %132 : i32, !llvm.ptr
%133 = llvm.load %88 : !llvm.ptr -> i64
%134 = llvm.load %52 : !llvm.ptr -> i64
%135 = arith.constant 1 : i32
%137 = arith.extsi %135 : i32 to i64
%136 = arith.subi %134, %137 : i64
%138 = arith.addi %133, %136 : i64
llvm.store %138, %88 : i64, !llvm.ptr
%139 = llvm.load %16 : !llvm.ptr -> i64
%140 = arith.constant 1 : i32
%142 = arith.extsi %140 : i32 to i64
%141 = arith.addi %139, %142 : i64
llvm.store %141, %16 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%143 = llvm.mlir.addressof @str_0 : !llvm.ptr
%144 = llvm.load %88 : !llvm.ptr -> i64
%145 = llvm.call @printf(%143, %144) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%43) : (!llvm.ptr) -> ()
func.call @free(%4) : (!llvm.ptr) -> ()
%148 = arith.constant 0 : i32
func.return %148 : i32
}
}