Problem 225

124th odd number that does not divide any term of the tribonacci sequence.

Answer2009
Output2009
StatusPASS
Native helperno
Runtime20 ms
Peak memory1104 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(1)O(n^2)
ApproachFlow solutionMatrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 225
# 124th odd number that does not divide any term of the tribonacci sequence.

function divides_trib(mod: i64) -> bool {
    let mut a: i64 = 1
    let mut b: i64 = 1
    let mut c: i64 = 1
    # period at most mod^3; detect return to (1,1,1) or zero
    let mut steps: i64 = 0
    let limit: i64 = mod * mod * mod + 5
    while steps < limit {
        let nxt: i64 = (a + b + c) % mod
        if nxt == 0 { return true }
        a = b
        b = c
        c = nxt
        if a == 1 && b == 1 && c == 1 { return false }
        steps = steps + 1
    }
    return false
}

function main() -> i32 {
    let mut count: i32 = 0
    let mut n: i64 = 1
    while true {
        n = n + 2
        if !divides_trib(n) {
            count = count + 1
            if count == 124 {
                printf("%lld\n", n)
                return 0
            }
        }
    }
    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; }

bool divides_trib_i64(int64_t mod);
int32_t main(void);

bool divides_trib_i64(int64_t mod) {
    int64_t a = 1;
    int64_t b = 1;
    int64_t c = 1;
    int64_t steps = 0;
    int64_t limit = (((mod * mod) * mod) + 5);
    while (steps < limit) {
        int64_t nxt = FLOW_CHECKED_MOD((((a + b) + c)), (mod));
        if (nxt == 0) {
            return 1;
        }
        a = b;
        b = c;
        c = nxt;
        if (((a == 1 && b == 1) && c == 1)) {
            return 0;
        }
        steps = (steps + 1);
    }
    return 0;
}

int32_t main(void) {
    int32_t count = 0;
    int64_t n = 1;
    while (1) {
        n = (n + 2);
        if ((!(divides_trib_i64(n)))) {
            count = (count + 1);
            if (count == 124) {
                printf("%lld\n", n);
                return 0;
            }
        }
    }
    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 @divides_trib(%arg0: i64) -> i1 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = arith.constant 1 : i32
    %5 = arith.extsi %4 : i32 to i64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i64, !llvm.ptr
    %8 = arith.constant 1 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    %12 = arith.constant 0 : i32
    %13 = arith.extsi %12 : i32 to i64
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
    llvm.store %13, %15 : i64, !llvm.ptr
    %16 = arith.muli %arg0, %arg0 : i64
    %17 = arith.muli %16, %arg0 : i64
    %18 = arith.constant 5 : i32
    %20 = arith.extsi %18 : i32 to i64
    %19 = arith.addi %17, %20 : i64
    cf.br ^bb0
    ^bb0:
    %21 = llvm.load %15 : !llvm.ptr -> i64
    %22 = arith.cmpi slt, %21, %19 : i64
    cf.cond_br %22, ^bb1, ^bb2
    ^bb1:
      %23 = llvm.load %3 : !llvm.ptr -> i64
      %24 = llvm.load %7 : !llvm.ptr -> i64
      %25 = arith.addi %23, %24 : i64
      %26 = llvm.load %11 : !llvm.ptr -> i64
      %27 = arith.addi %25, %26 : i64
      %28 = arith.remsi %27, %arg0 : i64
      %29 = arith.constant 0 : i32
      %31 = arith.extsi %29 : i32 to i64
      %30 = arith.cmpi eq, %28, %31 : i64
      cf.cond_br %30, ^bb3, ^bb4
      ^bb3:
        %32 = arith.constant 1 : i1
        func.return %32 : i1
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %33 = llvm.load %7 : !llvm.ptr -> i64
      llvm.store %33, %3 : i64, !llvm.ptr
      %34 = llvm.load %11 : !llvm.ptr -> i64
      llvm.store %34, %7 : i64, !llvm.ptr
      llvm.store %28, %11 : i64, !llvm.ptr
      %35 = llvm.load %3 : !llvm.ptr -> i64
      %36 = arith.constant 1 : i32
      %38 = arith.extsi %36 : i32 to i64
      %37 = arith.cmpi eq, %35, %38 : i64
      %39 = scf.if %37 -> (i1) {
        %40 = llvm.load %7 : !llvm.ptr -> i64
        %41 = arith.constant 1 : i32
        %43 = arith.extsi %41 : i32 to i64
        %42 = arith.cmpi eq, %40, %43 : i64
        scf.yield %42 : i1
      } else {
        %44 = arith.constant false
        scf.yield %44 : i1
      }
      %45 = scf.if %39 -> (i1) {
        %46 = llvm.load %11 : !llvm.ptr -> i64
        %47 = arith.constant 1 : i32
        %49 = arith.extsi %47 : i32 to i64
        %48 = arith.cmpi eq, %46, %49 : i64
        scf.yield %48 : i1
      } else {
        %50 = arith.constant false
        scf.yield %50 : i1
      }
      cf.cond_br %45, ^bb6, ^bb7
      ^bb6:
        %51 = arith.constant 0 : i1
        func.return %51 : i1
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %52 = llvm.load %15 : !llvm.ptr -> i64
      %53 = arith.constant 1 : i32
      %55 = arith.extsi %53 : i32 to i64
      %54 = arith.addi %52, %55 : i64
      llvm.store %54, %15 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %56 = arith.constant 0 : i1
    func.return %56 : i1
  }
  func.func @main() -> i32 {
    %57 = arith.constant 0 : i32
    %58 = llvm.mlir.constant(1 : i64) : i64
    %59 = llvm.alloca %58 x i32 : (i64) -> !llvm.ptr
    llvm.store %57, %59 : i32, !llvm.ptr
    %60 = arith.constant 1 : i32
    %61 = arith.extsi %60 : i32 to i64
    %62 = llvm.mlir.constant(1 : i64) : i64
    %63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
    llvm.store %61, %63 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %64 = arith.constant 1 : i1
    cf.cond_br %64, ^bb10, ^bb11
    ^bb10:
      %65 = llvm.load %63 : !llvm.ptr -> i64
      %66 = arith.constant 2 : i32
      %68 = arith.extsi %66 : i32 to i64
      %67 = arith.addi %65, %68 : i64
      llvm.store %67, %63 : i64, !llvm.ptr
      %70 = llvm.load %63 : !llvm.ptr -> i64
      %69 = func.call @divides_trib(%70) : (i64) -> i1
      %72 = arith.constant 1 : i1
      %71 = arith.xori %69, %72 : i1
      cf.cond_br %71, ^bb12, ^bb13
      ^bb12:
        %74 = llvm.load %59 : !llvm.ptr -> i32
        %75 = arith.constant 1 : i32
        %76 = arith.addi %74, %75 : i32
        llvm.store %76, %59 : i32, !llvm.ptr
        %77 = llvm.load %59 : !llvm.ptr -> i32
        %78 = arith.constant 124 : i32
        %79 = arith.cmpi eq, %77, %78 : i32
        cf.cond_br %79, ^bb15, ^bb16
        ^bb15:
          %80 = llvm.mlir.addressof @str_0 : !llvm.ptr
          %81 = llvm.load %63 : !llvm.ptr -> i64
          %82 = llvm.call @printf(%80, %81) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
          %83 = arith.constant 0 : i32
          func.return %83 : i32
        ^bb16:
          cf.br ^bb17
        ^bb17:
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      cf.br ^bb9
    ^bb11:
    %84 = arith.constant 1 : i32
    func.return %84 : i32
  }
}