Problem 710
Least n>=43 with t(n)≡0 (mod 10^6).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | O(n log n) |
| Space complexity | O(n^2) | O(n) |
| Approach | Flow solution | Search with pruning or sieve |
| Verdict | Optimal |
Flow source
# Project Euler 710
# Least n>=43 with t(n)≡0 (mod 10^6).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let N: i64 = 2000000
let c1: ptr<i64> = calloc(N + 1, 8)
let c2: ptr<i64> = calloc(N + 1, 8)
let sc1: ptr<i64> = calloc(N + 1, 8)
let sc2: ptr<i64> = calloc(N + 1, 8)
let t: ptr<i64> = calloc(N + 1, 8)
if c1 == null { return 1 }
let mod: i64 = 1000000
c1[0] = 1; c1[1] = 1; c1[2] = 1
c2[2] = 1
sc1[0] = 1; sc1[1] = 2; sc1[2] = 3
sc2[2] = 1
t[2] = 1
let mut n: i64 = 2
while (t[n] != 0) || (n < 43) {
n = n + 1
c2[n] = (sc2[n - 1] + c1[n - 2]) % mod
c1[n] = (sc1[n - 1] - c1[n - 2]) % mod
if c1[n] < 0 { c1[n] = c1[n] + mod }
sc2[n] = (sc2[n - 1] + c2[n]) % mod
sc1[n] = (sc1[n - 1] + c1[n]) % mod
if n % 2 == 0 {
t[n] = (sc2[n / 2] + c1[n / 2 - 1]) % mod
} else {
t[n] = sc2[n / 2] % mod
}
}
printf("%lld\n", n)
free(c1); free(c2); free(sc1); free(sc2); free(t)
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 N = 2000000;
int64_t* c1 = (int64_t*)(calloc((N + 1), 8));
int64_t* c2 = (int64_t*)(calloc((N + 1), 8));
int64_t* sc1 = (int64_t*)(calloc((N + 1), 8));
int64_t* sc2 = (int64_t*)(calloc((N + 1), 8));
int64_t* t = (int64_t*)(calloc((N + 1), 8));
if (c1 == NULL) {
return 1;
}
int64_t mod = 1000000;
c1[0] = 1;
c1[1] = 1;
c1[2] = 1;
c2[2] = 1;
sc1[0] = 1;
sc1[1] = 2;
sc1[2] = 3;
sc2[2] = 1;
t[2] = 1;
int64_t n = 2;
while ((t[n] != 0 || n < 43)) {
n = (n + 1);
c2[n] = FLOW_CHECKED_MOD(((sc2[(n - 1)] + c1[(n - 2)])), (mod));
c1[n] = FLOW_CHECKED_MOD(((sc1[(n - 1)] - c1[(n - 2)])), (mod));
if (c1[n] < 0) {
c1[n] = (c1[n] + mod);
}
sc2[n] = FLOW_CHECKED_MOD(((sc2[(n - 1)] + c2[n])), (mod));
sc1[n] = FLOW_CHECKED_MOD(((sc1[(n - 1)] + c1[n])), (mod));
if (FLOW_CHECKED_MOD((n), (2)) == 0) {
t[n] = FLOW_CHECKED_MOD(((sc2[FLOW_CHECKED_DIV((n), (2))] + c1[(FLOW_CHECKED_DIV((n), (2)) - 1)])), (mod));
} else {
t[n] = FLOW_CHECKED_MOD((sc2[FLOW_CHECKED_DIV((n), (2))]), (mod));
}
}
printf("%lld\n", n);
free(c1);
free(c2);
free(sc1);
free(sc2);
free(t);
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 2000000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 1 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.addi %1, %5 : i64
%6 = arith.constant 8 : i32
%7 = arith.extsi %6 : i32 to i64
%2 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
%9 = arith.constant 1 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.addi %1, %11 : i64
%12 = arith.constant 8 : i32
%13 = arith.extsi %12 : i32 to i64
%8 = func.call @calloc(%10, %13) : (i64, i64) -> !llvm.ptr
%15 = arith.constant 1 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.addi %1, %17 : i64
%18 = arith.constant 8 : i32
%19 = arith.extsi %18 : i32 to i64
%14 = func.call @calloc(%16, %19) : (i64, i64) -> !llvm.ptr
%21 = arith.constant 1 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.addi %1, %23 : i64
%24 = arith.constant 8 : i32
%25 = arith.extsi %24 : i32 to i64
%20 = func.call @calloc(%22, %25) : (i64, i64) -> !llvm.ptr
%27 = arith.constant 1 : i32
%29 = arith.extsi %27 : i32 to i64
%28 = arith.addi %1, %29 : i64
%30 = arith.constant 8 : i32
%31 = arith.extsi %30 : i32 to i64
%26 = func.call @calloc(%28, %31) : (i64, i64) -> !llvm.ptr
%32 = llvm.mlir.zero : !llvm.ptr
%33 = llvm.icmp "eq" %2, %32 : !llvm.ptr
cf.cond_br %33, ^bb0, ^bb1
^bb0:
%34 = arith.constant 1 : i32
func.return %34 : i32
^bb1:
cf.br ^bb2
^bb2:
%35 = arith.constant 1000000 : i32
%36 = arith.extsi %35 : i32 to i64
%37 = arith.constant 1 : i32
%38 = arith.constant 0 : i32
%39 = arith.extsi %37 : i32 to i64
%40 = arith.extsi %38 : i32 to i64
%41 = llvm.getelementptr %2[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %39, %41 : i64, !llvm.ptr
%42 = arith.constant 1 : i32
%43 = arith.constant 1 : i32
%44 = arith.extsi %42 : i32 to i64
%45 = arith.extsi %43 : i32 to i64
%46 = llvm.getelementptr %2[%45] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %44, %46 : i64, !llvm.ptr
%47 = arith.constant 1 : i32
%48 = arith.constant 2 : i32
%49 = arith.extsi %47 : i32 to i64
%50 = arith.extsi %48 : i32 to i64
%51 = llvm.getelementptr %2[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %49, %51 : i64, !llvm.ptr
%52 = arith.constant 1 : i32
%53 = arith.constant 2 : i32
%54 = arith.extsi %52 : i32 to i64
%55 = arith.extsi %53 : i32 to i64
%56 = llvm.getelementptr %8[%55] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %54, %56 : i64, !llvm.ptr
%57 = arith.constant 1 : i32
%58 = arith.constant 0 : i32
%59 = arith.extsi %57 : i32 to i64
%60 = arith.extsi %58 : i32 to i64
%61 = llvm.getelementptr %14[%60] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %59, %61 : i64, !llvm.ptr
%62 = arith.constant 2 : i32
%63 = arith.constant 1 : i32
%64 = arith.extsi %62 : i32 to i64
%65 = arith.extsi %63 : i32 to i64
%66 = llvm.getelementptr %14[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %64, %66 : i64, !llvm.ptr
%67 = arith.constant 3 : i32
%68 = arith.constant 2 : i32
%69 = arith.extsi %67 : i32 to i64
%70 = arith.extsi %68 : i32 to i64
%71 = llvm.getelementptr %14[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %69, %71 : i64, !llvm.ptr
%72 = arith.constant 1 : i32
%73 = arith.constant 2 : i32
%74 = arith.extsi %72 : i32 to i64
%75 = arith.extsi %73 : i32 to i64
%76 = llvm.getelementptr %20[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %74, %76 : i64, !llvm.ptr
%77 = arith.constant 1 : i32
%78 = arith.constant 2 : i32
%79 = arith.extsi %77 : i32 to i64
%80 = arith.extsi %78 : i32 to i64
%81 = llvm.getelementptr %26[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %79, %81 : i64, !llvm.ptr
%82 = arith.constant 2 : i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.mlir.constant(1 : i64) : i64
%85 = llvm.alloca %84 x i64 : (i64) -> !llvm.ptr
llvm.store %83, %85 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%87 = llvm.load %85 : !llvm.ptr -> i64
%88 = llvm.getelementptr %26[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%86 = llvm.load %88 : !llvm.ptr -> i64
%89 = arith.constant 0 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.cmpi ne, %86, %91 : i64
%92 = scf.if %90 -> (i1) {
%93 = arith.constant true
scf.yield %93 : i1
} else {
%94 = llvm.load %85 : !llvm.ptr -> i64
%95 = arith.constant 43 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.cmpi slt, %94, %97 : i64
scf.yield %96 : i1
}
cf.cond_br %92, ^bb4, ^bb5
^bb4:
%98 = llvm.load %85 : !llvm.ptr -> i64
%99 = arith.constant 1 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.addi %98, %101 : i64
llvm.store %100, %85 : i64, !llvm.ptr
%103 = llvm.load %85 : !llvm.ptr -> i64
%104 = arith.constant 1 : i32
%106 = arith.extsi %104 : i32 to i64
%105 = arith.subi %103, %106 : i64
%107 = llvm.getelementptr %20[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%102 = llvm.load %107 : !llvm.ptr -> i64
%109 = llvm.load %85 : !llvm.ptr -> i64
%110 = arith.constant 2 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.subi %109, %112 : i64
%113 = llvm.getelementptr %2[%111] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%108 = llvm.load %113 : !llvm.ptr -> i64
%114 = arith.addi %102, %108 : i64
%115 = arith.remsi %114, %36 : i64
%116 = llvm.load %85 : !llvm.ptr -> i64
%117 = llvm.getelementptr %8[%116] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %115, %117 : i64, !llvm.ptr
%119 = llvm.load %85 : !llvm.ptr -> i64
%120 = arith.constant 1 : i32
%122 = arith.extsi %120 : i32 to i64
%121 = arith.subi %119, %122 : i64
%123 = llvm.getelementptr %14[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%118 = llvm.load %123 : !llvm.ptr -> i64
%125 = llvm.load %85 : !llvm.ptr -> i64
%126 = arith.constant 2 : i32
%128 = arith.extsi %126 : i32 to i64
%127 = arith.subi %125, %128 : i64
%129 = llvm.getelementptr %2[%127] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%124 = llvm.load %129 : !llvm.ptr -> i64
%130 = arith.subi %118, %124 : i64
%131 = arith.remsi %130, %36 : i64
%132 = llvm.load %85 : !llvm.ptr -> i64
%133 = llvm.getelementptr %2[%132] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %131, %133 : i64, !llvm.ptr
%135 = llvm.load %85 : !llvm.ptr -> i64
%136 = llvm.getelementptr %2[%135] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%134 = llvm.load %136 : !llvm.ptr -> i64
%137 = arith.constant 0 : i32
%139 = arith.extsi %137 : i32 to i64
%138 = arith.cmpi slt, %134, %139 : i64
cf.cond_br %138, ^bb6, ^bb7
^bb6:
%141 = llvm.load %85 : !llvm.ptr -> i64
%142 = llvm.getelementptr %2[%141] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%140 = llvm.load %142 : !llvm.ptr -> i64
%143 = arith.addi %140, %36 : i64
%144 = llvm.load %85 : !llvm.ptr -> i64
%145 = llvm.getelementptr %2[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %143, %145 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%147 = llvm.load %85 : !llvm.ptr -> i64
%148 = arith.constant 1 : i32
%150 = arith.extsi %148 : i32 to i64
%149 = arith.subi %147, %150 : i64
%151 = llvm.getelementptr %20[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%146 = llvm.load %151 : !llvm.ptr -> i64
%153 = llvm.load %85 : !llvm.ptr -> i64
%154 = llvm.getelementptr %8[%153] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%152 = llvm.load %154 : !llvm.ptr -> i64
%155 = arith.addi %146, %152 : i64
%156 = arith.remsi %155, %36 : i64
%157 = llvm.load %85 : !llvm.ptr -> i64
%158 = llvm.getelementptr %20[%157] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %156, %158 : i64, !llvm.ptr
%160 = llvm.load %85 : !llvm.ptr -> i64
%161 = arith.constant 1 : i32
%163 = arith.extsi %161 : i32 to i64
%162 = arith.subi %160, %163 : i64
%164 = llvm.getelementptr %14[%162] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%159 = llvm.load %164 : !llvm.ptr -> i64
%166 = llvm.load %85 : !llvm.ptr -> i64
%167 = llvm.getelementptr %2[%166] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%165 = llvm.load %167 : !llvm.ptr -> i64
%168 = arith.addi %159, %165 : i64
%169 = arith.remsi %168, %36 : i64
%170 = llvm.load %85 : !llvm.ptr -> i64
%171 = llvm.getelementptr %14[%170] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %169, %171 : i64, !llvm.ptr
%172 = llvm.load %85 : !llvm.ptr -> i64
%173 = arith.constant 2 : i32
%175 = arith.extsi %173 : i32 to i64
%174 = arith.remsi %172, %175 : i64
%176 = arith.constant 0 : i32
%178 = arith.extsi %176 : i32 to i64
%177 = arith.cmpi eq, %174, %178 : i64
cf.cond_br %177, ^bb9, ^bb10
^bb9:
%180 = llvm.load %85 : !llvm.ptr -> i64
%181 = arith.constant 2 : i32
%183 = arith.extsi %181 : i32 to i64
%182 = arith.divsi %180, %183 : i64
%184 = llvm.getelementptr %20[%182] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%179 = llvm.load %184 : !llvm.ptr -> i64
%186 = llvm.load %85 : !llvm.ptr -> i64
%187 = arith.constant 2 : i32
%189 = arith.extsi %187 : i32 to i64
%188 = arith.divsi %186, %189 : i64
%190 = arith.constant 1 : i32
%192 = arith.extsi %190 : i32 to i64
%191 = arith.subi %188, %192 : i64
%193 = llvm.getelementptr %2[%191] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%185 = llvm.load %193 : !llvm.ptr -> i64
%194 = arith.addi %179, %185 : i64
%195 = arith.remsi %194, %36 : i64
%196 = llvm.load %85 : !llvm.ptr -> i64
%197 = llvm.getelementptr %26[%196] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %195, %197 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
%199 = llvm.load %85 : !llvm.ptr -> i64
%200 = arith.constant 2 : i32
%202 = arith.extsi %200 : i32 to i64
%201 = arith.divsi %199, %202 : i64
%203 = llvm.getelementptr %20[%201] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%198 = llvm.load %203 : !llvm.ptr -> i64
%204 = arith.remsi %198, %36 : i64
%205 = llvm.load %85 : !llvm.ptr -> i64
%206 = llvm.getelementptr %26[%205] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %204, %206 : i64, !llvm.ptr
cf.br ^bb11
^bb11:
cf.br ^bb3
^bb5:
%207 = llvm.mlir.addressof @str_0 : !llvm.ptr
%208 = llvm.load %85 : !llvm.ptr -> i64
%209 = llvm.call @printf(%207, %208) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
func.call @free(%8) : (!llvm.ptr) -> ()
func.call @free(%14) : (!llvm.ptr) -> ()
func.call @free(%20) : (!llvm.ptr) -> ()
func.call @free(%26) : (!llvm.ptr) -> ()
%215 = arith.constant 0 : i32
func.return %215 : i32
}
}