Problem 025

Index of the first Fibonacci term containing 1000 digits. Digits-only addition — reads like the math, runs like C.

Answer4782
Output4782
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(n)O(1)
ApproachFlow solutionIterate Fibonacci, check digit count
VerdictSuboptimal

Flow source

# Project Euler 025
# Index of the first Fibonacci term containing 1000 digits.
# Digits-only addition — reads like the math, runs like C.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function memcpy(dst: ptr<void>, src: ptr<void>, n: i64) -> ptr<void>
}

function main() -> i32 {
    let digits: i32 = 1000
    let a: ptr<i32> = calloc(digits as i64, 4)
    let b: ptr<i32> = calloc(digits as i64, 4)
    let c: ptr<i32> = calloc(digits as i64, 4)
    if a == null || b == null || c == null {
        return 1
    }

    a[0] = 1  # F1
    b[0] = 1  # F2
    let mut len: i32 = 1
    let mut index: i64 = 2

    while len < digits {
        let mut carry: i32 = 0
        let mut i: i32 = 0
        while i < len {
            let v: i32 = a[i] + b[i] + carry
            c[i] = v % 10
            carry = v / 10
            i = i + 1
        }
        if carry > 0 {
            c[len] = carry
            len = len + 1
        }
        # a,b = b,c
        memcpy(a, b, (digits as i64) * 4)
        memcpy(b, c, (digits as i64) * 4)
        index = index + 1
    }

    printf("%lld\n", index)
    free(a)
    free(b)
    free(c)
    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) {
    int32_t digits = 1000;
    int32_t* a = (int32_t*)(calloc(((int64_t)(digits)), 4));
    int32_t* b = (int32_t*)(calloc(((int64_t)(digits)), 4));
    int32_t* c = (int32_t*)(calloc(((int64_t)(digits)), 4));
    if (((a == NULL || b == NULL) || c == NULL)) {
        return 1;
    }
    a[0] = 1;
    b[0] = 1;
    int32_t len = 1;
    int64_t index = 2;
    while (len < digits) {
        int32_t carry = 0;
        int32_t i = 0;
        while (i < len) {
            int32_t v = ((a[i] + b[i]) + carry);
            c[i] = FLOW_CHECKED_MOD((v), (10));
            carry = FLOW_CHECKED_DIV((v), (10));
            i = (i + 1);
        }
        if (carry > 0) {
            c[len] = carry;
            len = (len + 1);
        }
        memcpy(a, b, (((int64_t)(digits)) * 4));
        memcpy(b, c, (((int64_t)(digits)) * 4));
        index = (index + 1);
    }
    printf("%lld\n", index);
    free(a);
    free(b);
    free(c);
    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 private @memcpy(!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
  func.func @main() -> i32 {
    %0 = arith.constant 1000 : i32
    %2 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 4 : i32
    %4 = arith.extsi %3 : i32 to i64
    %1 = func.call @calloc(%2, %4) : (i64, i64) -> !llvm.ptr
    %6 = arith.extsi %0 : i32 to i64
    %7 = arith.constant 4 : i32
    %8 = arith.extsi %7 : i32 to i64
    %5 = func.call @calloc(%6, %8) : (i64, i64) -> !llvm.ptr
    %10 = arith.extsi %0 : i32 to i64
    %11 = arith.constant 4 : i32
    %12 = arith.extsi %11 : i32 to i64
    %9 = func.call @calloc(%10, %12) : (i64, i64) -> !llvm.ptr
    %13 = llvm.mlir.zero : !llvm.ptr
    %14 = llvm.icmp "eq" %1, %13 : !llvm.ptr
    %15 = scf.if %14 -> (i1) {
      %16 = arith.constant true
      scf.yield %16 : i1
    } else {
      %17 = llvm.mlir.zero : !llvm.ptr
      %18 = llvm.icmp "eq" %5, %17 : !llvm.ptr
      scf.yield %18 : i1
    }
    %19 = scf.if %15 -> (i1) {
      %20 = arith.constant true
      scf.yield %20 : i1
    } else {
      %21 = llvm.mlir.zero : !llvm.ptr
      %22 = llvm.icmp "eq" %9, %21 : !llvm.ptr
      scf.yield %22 : i1
    }
    cf.cond_br %19, ^bb0, ^bb1
    ^bb0:
      %23 = arith.constant 1 : i32
      func.return %23 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %24 = arith.constant 1 : i32
    %25 = arith.constant 0 : i32
    %26 = arith.extsi %25 : i32 to i64
    %27 = llvm.getelementptr %1[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %24, %27 : i32, !llvm.ptr
    %28 = arith.constant 1 : i32
    %29 = arith.constant 0 : i32
    %30 = arith.extsi %29 : i32 to i64
    %31 = llvm.getelementptr %5[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %28, %31 : i32, !llvm.ptr
    %32 = arith.constant 1 : i32
    %33 = llvm.mlir.constant(1 : i64) : i64
    %34 = llvm.alloca %33 x i32 : (i64) -> !llvm.ptr
    llvm.store %32, %34 : i32, !llvm.ptr
    %35 = arith.constant 2 : i32
    %36 = arith.extsi %35 : i32 to i64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %39 = llvm.load %34 : !llvm.ptr -> i32
    %40 = arith.cmpi slt, %39, %0 : i32
    cf.cond_br %40, ^bb4, ^bb5
    ^bb4:
      %41 = arith.constant 0 : i32
      %42 = llvm.mlir.constant(1 : i64) : i64
      %43 = llvm.alloca %42 x i32 : (i64) -> !llvm.ptr
      llvm.store %41, %43 : i32, !llvm.ptr
      %44 = arith.constant 0 : i32
      %45 = llvm.mlir.constant(1 : i64) : i64
      %46 = llvm.alloca %45 x i32 : (i64) -> !llvm.ptr
      llvm.store %44, %46 : i32, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %47 = llvm.load %46 : !llvm.ptr -> i32
      %48 = llvm.load %34 : !llvm.ptr -> i32
      %49 = arith.cmpi slt, %47, %48 : i32
      cf.cond_br %49, ^bb7, ^bb8
      ^bb7:
        %51 = llvm.load %46 : !llvm.ptr -> i32
        %52 = arith.extsi %51 : i32 to i64
        %53 = llvm.getelementptr %1[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %50 = llvm.load %53 : !llvm.ptr -> i32
        %55 = llvm.load %46 : !llvm.ptr -> i32
        %56 = arith.extsi %55 : i32 to i64
        %57 = llvm.getelementptr %5[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %54 = llvm.load %57 : !llvm.ptr -> i32
        %58 = arith.addi %50, %54 : i32
        %59 = llvm.load %43 : !llvm.ptr -> i32
        %60 = arith.addi %58, %59 : i32
        %61 = arith.constant 10 : i32
        %62 = arith.remsi %60, %61 : i32
        %63 = llvm.load %46 : !llvm.ptr -> i32
        %64 = arith.extsi %63 : i32 to i64
        %65 = llvm.getelementptr %9[%64] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %62, %65 : i32, !llvm.ptr
        %66 = arith.constant 10 : i32
        %67 = arith.divsi %60, %66 : i32
        llvm.store %67, %43 : i32, !llvm.ptr
        %68 = llvm.load %46 : !llvm.ptr -> i32
        %69 = arith.constant 1 : i32
        %70 = arith.addi %68, %69 : i32
        llvm.store %70, %46 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %71 = llvm.load %43 : !llvm.ptr -> i32
      %72 = arith.constant 0 : i32
      %73 = arith.cmpi sgt, %71, %72 : i32
      cf.cond_br %73, ^bb9, ^bb10
      ^bb9:
        %74 = llvm.load %43 : !llvm.ptr -> i32
        %75 = llvm.load %34 : !llvm.ptr -> i32
        %76 = arith.extsi %75 : i32 to i64
        %77 = llvm.getelementptr %9[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %74, %77 : i32, !llvm.ptr
        %78 = llvm.load %34 : !llvm.ptr -> i32
        %79 = arith.constant 1 : i32
        %80 = arith.addi %78, %79 : i32
        llvm.store %80, %34 : i32, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %82 = arith.extsi %0 : i32 to i64
      %83 = arith.constant 4 : i32
      %85 = arith.extsi %83 : i32 to i64
      %84 = arith.muli %82, %85 : i64
      %81 = func.call @memcpy(%1, %5, %84) : (!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
      %87 = arith.extsi %0 : i32 to i64
      %88 = arith.constant 4 : i32
      %90 = arith.extsi %88 : i32 to i64
      %89 = arith.muli %87, %90 : i64
      %86 = func.call @memcpy(%5, %9, %89) : (!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
      %91 = llvm.load %38 : !llvm.ptr -> i64
      %92 = arith.constant 1 : i32
      %94 = arith.extsi %92 : i32 to i64
      %93 = arith.addi %91, %94 : i64
      llvm.store %93, %38 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %95 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %96 = llvm.load %38 : !llvm.ptr -> i64
    %97 = llvm.call @printf(%95, %96) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%1) : (!llvm.ptr) -> ()
    func.call @free(%5) : (!llvm.ptr) -> ()
    func.call @free(%9) : (!llvm.ptr) -> ()
    %101 = arith.constant 0 : i32
    func.return %101 : i32
  }
}