← All problems
Problem 243
Least n with resilience R(n) < 15499/94744.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log n)
Space complexity O(1)O(n)
Approach Flow solution Search with pruning or sieve
Verdict Suboptimal
Flow source
# Project Euler 243
# Least n with resilience R(n) < 15499/94744.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function phi_with_primes(x: i64, primes: ptr<i64>, pc: i32) -> i64 {
let mut result: i64 = x
let mut reduced: i64 = x
let mut i: i32 = 0
while i < pc {
let p: i64 = primes[i]
if p * p > reduced { break }
if reduced % p == 0 {
while reduced % p == 0 { reduced = reduced / p }
result = result - result / p
}
i = i + 1
}
if reduced > 1 {
result = result - result / reduced
}
return result
}
function is_less(a1: i64, b1: i64, a2: i64, b2: i64) -> bool {
return a1 * b2 < a2 * b1
}
function main() -> i32 {
let num: i64 = 15499
let den: i64 = 94744
let primes: ptr<i64> = calloc(100, 8)
if primes == null { return 1 }
let mut pc: i32 = 0
let mut current: i64 = 1
let mut i: i64 = 2
while true {
let mut is_p: bool = true
let mut j: i32 = 0
while j < pc {
if primes[j] * primes[j] > i { break }
if i % primes[j] == 0 { is_p = false; break }
j = j + 1
}
if !is_p {
i = i + 1
continue
}
primes[pc] = i
pc = pc + 1
current = current * i
if is_less(phi_with_primes(current, primes, pc), current - 1, num, den) {
break
}
i = i + 1
}
current = current / primes[pc - 1]
let mut k: i64 = 1
while true {
let nxt: i64 = current * k
if is_less(phi_with_primes(nxt, primes, pc), nxt - 1, num, den) {
printf("%lld\n", nxt)
free(primes)
return 0
}
k = k + 1
}
return 1
}
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 phi_with_primes_i64_ptr_i64_i32(int64_t x, int64_t* primes, int32_t pc);
bool is_less_i64_i64_i64_i64(int64_t a1, int64_t b1, int64_t a2, int64_t b2);
int32_t main(void);
int64_t phi_with_primes_i64_ptr_i64_i32(int64_t x, int64_t* primes, int32_t pc) {
int64_t result = x;
int64_t reduced = x;
int32_t i = 0;
while (i < pc) {
int64_t p = primes[i];
if ((p * p) > reduced) {
break;
}
if (FLOW_CHECKED_MOD((reduced), (p)) == 0) {
while (FLOW_CHECKED_MOD((reduced), (p)) == 0) {
reduced = FLOW_CHECKED_DIV((reduced), (p));
}
result = (result - FLOW_CHECKED_DIV((result), (p)));
}
i = (i + 1);
}
if (reduced > 1) {
result = (result - FLOW_CHECKED_DIV((result), (reduced)));
}
return result;
}
bool is_less_i64_i64_i64_i64(int64_t a1, int64_t b1, int64_t a2, int64_t b2) {
return (a1 * b2) < (a2 * b1);
}
int32_t main(void) {
int64_t num = 15499;
int64_t den = 94744;
int64_t* primes = (int64_t*)(calloc(100, 8));
if (primes == NULL) {
return 1;
}
int32_t pc = 0;
int64_t current = 1;
int64_t i = 2;
while (1) {
bool is_p = 1;
int32_t j = 0;
while (j < pc) {
if ((primes[j] * primes[j]) > i) {
break;
}
if (FLOW_CHECKED_MOD((i), (primes[j])) == 0) {
is_p = 0;
break;
}
j = (j + 1);
}
if ((!(is_p))) {
i = (i + 1);
continue;
}
primes[pc] = i;
pc = (pc + 1);
current = (current * i);
if (is_less_i64_i64_i64_i64(phi_with_primes_i64_ptr_i64_i32(current, primes, pc), (current - 1), num, den)) {
break;
}
i = (i + 1);
}
current = FLOW_CHECKED_DIV((current), (primes[(pc - 1)]));
int64_t k = 1;
while (1) {
int64_t nxt = (current * k);
if (is_less_i64_i64_i64_i64(phi_with_primes_i64_ptr_i64_i32(nxt, primes, pc), (nxt - 1), num, den)) {
printf("%lld\n", nxt);
free(primes);
return 0;
}
k = (k + 1);
}
return 1;
}
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 @phi_with_primes(%arg0: i64, %arg1: !llvm.ptr, %arg2: i32) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %3 : i64, !llvm.ptr
%4 = arith.constant 0 : i32
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i32 : (i64) -> !llvm.ptr
llvm.store %4, %6 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%7 = llvm.load %6 : !llvm.ptr -> i32
%8 = arith.cmpi slt, %7, %arg2 : i32
cf.cond_br %8, ^bb1, ^bb2
^bb1:
%10 = llvm.load %6 : !llvm.ptr -> i32
%11 = arith.extsi %10 : i32 to i64
%12 = llvm.getelementptr %arg1[%11] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%9 = llvm.load %12 : !llvm.ptr -> i64
%13 = arith.muli %9, %9 : i64
%14 = llvm.load %3 : !llvm.ptr -> i64
%15 = arith.cmpi sgt, %13, %14 : i64
cf.cond_br %15, ^bb3, ^bb4
^bb3:
cf.br ^bb2
^bb4:
cf.br ^bb5
^bb5:
%16 = llvm.load %3 : !llvm.ptr -> i64
%17 = arith.remsi %16, %9 : i64
%18 = arith.constant 0 : i32
%20 = arith.extsi %18 : i32 to i64
%19 = arith.cmpi eq, %17, %20 : i64
cf.cond_br %19, ^bb6, ^bb7
^bb6:
cf.br ^bb9
^bb9:
%21 = llvm.load %3 : !llvm.ptr -> i64
%22 = arith.remsi %21, %9 : i64
%23 = arith.constant 0 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.cmpi eq, %22, %25 : i64
cf.cond_br %24, ^bb10, ^bb11
^bb10:
%26 = llvm.load %3 : !llvm.ptr -> i64
%27 = arith.divsi %26, %9 : i64
llvm.store %27, %3 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%28 = llvm.load %1 : !llvm.ptr -> i64
%29 = llvm.load %1 : !llvm.ptr -> i64
%30 = arith.divsi %29, %9 : i64
%31 = arith.subi %28, %30 : i64
llvm.store %31, %1 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%32 = llvm.load %6 : !llvm.ptr -> i32
%33 = arith.constant 1 : i32
%34 = arith.addi %32, %33 : i32
llvm.store %34, %6 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
%35 = llvm.load %3 : !llvm.ptr -> i64
%36 = arith.constant 1 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.cmpi sgt, %35, %38 : i64
cf.cond_br %37, ^bb12, ^bb13
^bb12:
%39 = llvm.load %1 : !llvm.ptr -> i64
%40 = llvm.load %1 : !llvm.ptr -> i64
%41 = llvm.load %3 : !llvm.ptr -> i64
%42 = arith.divsi %40, %41 : i64
%43 = arith.subi %39, %42 : i64
llvm.store %43, %1 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%44 = llvm.load %1 : !llvm.ptr -> i64
func.return %44 : i64
}
func.func @is_less(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i64) -> i1 {
%45 = arith.muli %arg0, %arg3 : i64
%46 = arith.muli %arg2, %arg1 : i64
%47 = arith.cmpi slt, %45, %46 : i64
func.return %47 : i1
}
func.func @main() -> i32 {
%48 = arith.constant 15499 : i32
%49 = arith.extsi %48 : i32 to i64
%50 = arith.constant 94744 : i32
%51 = arith.extsi %50 : i32 to i64
%53 = arith.constant 100 : i32
%54 = arith.constant 8 : i32
%55 = arith.extsi %53 : i32 to i64
%56 = arith.extsi %54 : i32 to i64
%52 = func.call @calloc(%55, %56) : (i64, i64) -> !llvm.ptr
%57 = llvm.mlir.zero : !llvm.ptr
%58 = llvm.icmp "eq" %52, %57 : !llvm.ptr
cf.cond_br %58, ^bb15, ^bb16
^bb15:
%59 = arith.constant 1 : i32
func.return %59 : i32
^bb16:
cf.br ^bb17
^bb17:
%60 = arith.constant 0 : i32
%61 = llvm.mlir.constant(1 : i64) : i64
%62 = llvm.alloca %61 x i32 : (i64) -> !llvm.ptr
llvm.store %60, %62 : i32, !llvm.ptr
%63 = arith.constant 1 : i32
%64 = arith.extsi %63 : i32 to i64
%65 = llvm.mlir.constant(1 : i64) : i64
%66 = llvm.alloca %65 x i64 : (i64) -> !llvm.ptr
llvm.store %64, %66 : i64, !llvm.ptr
%67 = arith.constant 2 : i32
%68 = arith.extsi %67 : i32 to i64
%69 = llvm.mlir.constant(1 : i64) : i64
%70 = llvm.alloca %69 x i64 : (i64) -> !llvm.ptr
llvm.store %68, %70 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%71 = arith.constant 1 : i1
cf.cond_br %71, ^bb19, ^bb20
^bb19:
%72 = arith.constant 1 : i1
%73 = llvm.mlir.constant(1 : i64) : i64
%74 = llvm.alloca %73 x i1 : (i64) -> !llvm.ptr
llvm.store %72, %74 : i1, !llvm.ptr
%75 = arith.constant 0 : i32
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i32 : (i64) -> !llvm.ptr
llvm.store %75, %77 : i32, !llvm.ptr
cf.br ^bb21
^bb21:
%78 = llvm.load %77 : !llvm.ptr -> i32
%79 = llvm.load %62 : !llvm.ptr -> i32
%80 = arith.cmpi slt, %78, %79 : i32
cf.cond_br %80, ^bb22, ^bb23
^bb22:
%82 = llvm.load %77 : !llvm.ptr -> i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.getelementptr %52[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%81 = llvm.load %84 : !llvm.ptr -> i64
%86 = llvm.load %77 : !llvm.ptr -> i32
%87 = arith.extsi %86 : i32 to i64
%88 = llvm.getelementptr %52[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%85 = llvm.load %88 : !llvm.ptr -> i64
%89 = arith.muli %81, %85 : i64
%90 = llvm.load %70 : !llvm.ptr -> i64
%91 = arith.cmpi sgt, %89, %90 : i64
cf.cond_br %91, ^bb24, ^bb25
^bb24:
cf.br ^bb23
^bb25:
cf.br ^bb26
^bb26:
%92 = llvm.load %70 : !llvm.ptr -> i64
%94 = llvm.load %77 : !llvm.ptr -> i32
%95 = arith.extsi %94 : i32 to i64
%96 = llvm.getelementptr %52[%95] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%93 = llvm.load %96 : !llvm.ptr -> i64
%97 = arith.remsi %92, %93 : i64
%98 = arith.constant 0 : i32
%100 = arith.extsi %98 : i32 to i64
%99 = arith.cmpi eq, %97, %100 : i64
cf.cond_br %99, ^bb27, ^bb28
^bb27:
%101 = arith.constant 0 : i1
llvm.store %101, %74 : i1, !llvm.ptr
cf.br ^bb23
^bb28:
cf.br ^bb29
^bb29:
%102 = llvm.load %77 : !llvm.ptr -> i32
%103 = arith.constant 1 : i32
%104 = arith.addi %102, %103 : i32
llvm.store %104, %77 : i32, !llvm.ptr
cf.br ^bb21
^bb23:
%105 = llvm.load %74 : !llvm.ptr -> i1
%107 = arith.constant 1 : i1
%106 = arith.xori %105, %107 : i1
cf.cond_br %106, ^bb30, ^bb31
^bb30:
%109 = llvm.load %70 : !llvm.ptr -> i64
%110 = arith.constant 1 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.addi %109, %112 : i64
llvm.store %111, %70 : i64, !llvm.ptr
cf.br ^bb18
^bb31:
cf.br ^bb32
^bb32:
%113 = llvm.load %70 : !llvm.ptr -> i64
%114 = llvm.load %62 : !llvm.ptr -> i32
%115 = arith.extsi %114 : i32 to i64
%116 = llvm.getelementptr %52[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %113, %116 : i64, !llvm.ptr
%117 = llvm.load %62 : !llvm.ptr -> i32
%118 = arith.constant 1 : i32
%119 = arith.addi %117, %118 : i32
llvm.store %119, %62 : i32, !llvm.ptr
%120 = llvm.load %66 : !llvm.ptr -> i64
%121 = llvm.load %70 : !llvm.ptr -> i64
%122 = arith.muli %120, %121 : i64
llvm.store %122, %66 : i64, !llvm.ptr
%125 = llvm.load %66 : !llvm.ptr -> i64
%126 = llvm.load %62 : !llvm.ptr -> i32
%124 = func.call @phi_with_primes(%125, %52, %126) : (i64, !llvm.ptr, i32) -> i64
%127 = llvm.load %66 : !llvm.ptr -> i64
%128 = arith.constant 1 : i32
%130 = arith.extsi %128 : i32 to i64
%129 = arith.subi %127, %130 : i64
%123 = func.call @is_less(%124, %129, %49, %51) : (i64, i64, i64, i64) -> i1
cf.cond_br %123, ^bb33, ^bb34
^bb33:
cf.br ^bb20
^bb34:
cf.br ^bb35
^bb35:
%131 = llvm.load %70 : !llvm.ptr -> i64
%132 = arith.constant 1 : i32
%134 = arith.extsi %132 : i32 to i64
%133 = arith.addi %131, %134 : i64
llvm.store %133, %70 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%135 = llvm.load %66 : !llvm.ptr -> i64
%137 = llvm.load %62 : !llvm.ptr -> i32
%138 = arith.constant 1 : i32
%139 = arith.subi %137, %138 : i32
%140 = arith.extsi %139 : i32 to i64
%141 = llvm.getelementptr %52[%140] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%136 = llvm.load %141 : !llvm.ptr -> i64
%142 = arith.divsi %135, %136 : i64
llvm.store %142, %66 : i64, !llvm.ptr
%143 = arith.constant 1 : i32
%144 = arith.extsi %143 : i32 to i64
%145 = llvm.mlir.constant(1 : i64) : i64
%146 = llvm.alloca %145 x i64 : (i64) -> !llvm.ptr
llvm.store %144, %146 : i64, !llvm.ptr
cf.br ^bb36
^bb36:
%147 = arith.constant 1 : i1
cf.cond_br %147, ^bb37, ^bb38
^bb37:
%148 = llvm.load %66 : !llvm.ptr -> i64
%149 = llvm.load %146 : !llvm.ptr -> i64
%150 = arith.muli %148, %149 : i64
%153 = llvm.load %62 : !llvm.ptr -> i32
%152 = func.call @phi_with_primes(%150, %52, %153) : (i64, !llvm.ptr, i32) -> i64
%154 = arith.constant 1 : i32
%156 = arith.extsi %154 : i32 to i64
%155 = arith.subi %150, %156 : i64
%151 = func.call @is_less(%152, %155, %49, %51) : (i64, i64, i64, i64) -> i1
cf.cond_br %151, ^bb39, ^bb40
^bb39:
%157 = llvm.mlir.addressof @str_0 : !llvm.ptr
%158 = llvm.call @printf(%157, %150) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%52) : (!llvm.ptr) -> ()
%160 = arith.constant 0 : i32
func.return %160 : i32
^bb40:
cf.br ^bb41
^bb41:
%161 = llvm.load %146 : !llvm.ptr -> i64
%162 = arith.constant 1 : i32
%164 = arith.extsi %162 : i32 to i64
%163 = arith.addi %161, %164 : i64
llvm.store %163, %146 : i64, !llvm.ptr
cf.br ^bb36
^bb38:
%165 = arith.constant 1 : i32
func.return %165 : i32
}
}