← All problems
Problem 129
Least n > 10^6 with A(n) > 10^6 (repunit divisibility).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(sqrt(n))
Space complexity O(1)O(1)
Approach Flow solution Repunit divisibility
Verdict Suboptimal
Flow source
# Project Euler 129
# Least n > 10^6 with A(n) > 10^6 (repunit divisibility).
function A(n: i64) -> i64 {
if n % 2 == 0 || n % 5 == 0 { return 0 }
let mut k: i64 = 1
let mut r: i64 = 1
while r != 0 {
r = (r * 10 + 1) % n
k = k + 1
}
return k
}
function main() -> i32 {
let limit: i64 = 1000000
let mut n: i64 = limit
while A(n) <= limit {
n = n + 1
}
printf("%lld\n", n)
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; }
int64_t A_i64(int64_t n);
int32_t main(void);
int64_t A_i64(int64_t n) {
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (5)) == 0)) {
return 0;
}
int64_t k = 1;
int64_t r = 1;
while (r != 0) {
r = FLOW_CHECKED_MOD((((r * 10) + 1)), (n));
k = (k + 1);
}
return k;
}
int32_t main(void) {
int64_t limit = 1000000;
int64_t n = limit;
while (A_i64(n) <= limit) {
n = (n + 1);
}
printf("%lld\n", n);
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 @A(%arg0: i64) -> i64 {
%0 = arith.constant 2 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.remsi %arg0, %2 : i64
%3 = arith.constant 0 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.cmpi eq, %1, %5 : i64
%6 = scf.if %4 -> (i1) {
%7 = arith.constant true
scf.yield %7 : i1
} else {
%8 = arith.constant 5 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.remsi %arg0, %10 : i64
%11 = arith.constant 0 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.cmpi eq, %9, %13 : i64
scf.yield %12 : i1
}
cf.cond_br %6, ^bb0, ^bb1
^bb0:
%14 = arith.constant 0 : i32
%15 = arith.extsi %14 : i32 to i64
func.return %15 : i64
^bb1:
cf.br ^bb2
^bb2:
%16 = arith.constant 1 : i32
%17 = arith.extsi %16 : i32 to i64
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i64, !llvm.ptr
%20 = arith.constant 1 : i32
%21 = arith.extsi %20 : i32 to i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%24 = llvm.load %23 : !llvm.ptr -> i64
%25 = arith.constant 0 : i32
%27 = arith.extsi %25 : i32 to i64
%26 = arith.cmpi ne, %24, %27 : i64
cf.cond_br %26, ^bb4, ^bb5
^bb4:
%28 = llvm.load %23 : !llvm.ptr -> i64
%29 = arith.constant 10 : i32
%31 = arith.extsi %29 : i32 to i64
%30 = arith.muli %28, %31 : i64
%32 = arith.constant 1 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.addi %30, %34 : i64
%35 = arith.remsi %33, %arg0 : i64
llvm.store %35, %23 : i64, !llvm.ptr
%36 = llvm.load %19 : !llvm.ptr -> i64
%37 = arith.constant 1 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.addi %36, %39 : i64
llvm.store %38, %19 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%40 = llvm.load %19 : !llvm.ptr -> i64
func.return %40 : i64
}
func.func @main() -> i32 {
%41 = arith.constant 1000000 : i32
%42 = arith.extsi %41 : i32 to i64
%43 = llvm.mlir.constant(1 : i64) : i64
%44 = llvm.alloca %43 x i64 : (i64) -> !llvm.ptr
llvm.store %42, %44 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%46 = llvm.load %44 : !llvm.ptr -> i64
%45 = func.call @A(%46) : (i64) -> i64
%47 = arith.cmpi sle, %45, %42 : i64
cf.cond_br %47, ^bb7, ^bb8
^bb7:
%48 = llvm.load %44 : !llvm.ptr -> i64
%49 = arith.constant 1 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.addi %48, %51 : i64
llvm.store %50, %44 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%52 = llvm.mlir.addressof @str_0 : !llvm.ptr
%53 = llvm.load %44 : !llvm.ptr -> i64
%54 = llvm.call @printf(%52, %53) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%55 = arith.constant 0 : i32
func.return %55 : i32
}
}