Problem 057

In the first one-thousand expansions of √2, how many fractions have a numerator with more digits than the denominator? Expansion: 1 + 1/(2 + 1/(2 + ...)); recurrence p/q: p'=p+2q, q'=p+q Starting from 1/1, next = 1 + 1/(1 + p/q) = (p+2q)/(p+q)

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

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve or enumeration
VerdictOptimal

Flow source

# Project Euler 057
# In the first one-thousand expansions of √2, how many fractions have a
# numerator with more digits than the denominator?
# Expansion: 1 + 1/(2 + 1/(2 + ...)); recurrence p/q: p'=p+2q, q'=p+q
# Starting from 1/1, next = 1 + 1/(1 + p/q) = (p+2q)/(p+q)

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

function max2i(a: i32, b: i32) -> i32 {
    if a > b { return a }
    return b
}

function add(a: ptr<i32>, alen: i32, b: ptr<i32>, blen: i32, out: ptr<i32>) -> i32 {
    let mut carry: i32 = 0
    let mut i: i32 = 0
    let maxlen: i32 = max2i(alen, blen)
    while i < maxlen || carry > 0 {
        let mut v: i32 = carry
        if i < alen { v = v + a[i] }
        if i < blen { v = v + b[i] }
        out[i] = v % 10
        carry = v / 10
        i = i + 1
    }
    return i
}

function copy_digits(src: ptr<i32>, dst: ptr<i32>, len: i32) -> void {
    let mut i: i32 = 0
    while i < len {
        dst[i] = src[i]
        i = i + 1
    }
}

function main() -> i32 {
    let width: i32 = 400
    let p: ptr<i32> = calloc(width as i64, 4)
    let q: ptr<i32> = calloc(width as i64, 4)
    let np: ptr<i32> = calloc(width as i64, 4)
    let nq: ptr<i32> = calloc(width as i64, 4)
    let tmp: ptr<i32> = calloc(width as i64, 4)
    if p == null || q == null || np == null || nq == null || tmp == null { return 1 }

    # start with first expansion 3/2
    p[0] = 3
    q[0] = 2
    let mut plen: i32 = 1
    let mut qlen: i32 = 1
    let mut count: i64 = 0
    let mut i: i32 = 1
    while i <= 1000 {
        if plen > qlen {
            count = count + 1
        }
        # np = p + 2q; nq = p + q
        let tlen: i32 = add(q, qlen, q, qlen, tmp)  # 2q
        let nplen: i32 = add(p, plen, tmp, tlen, np)
        let nqlen: i32 = add(p, plen, q, qlen, nq)
        copy_digits(np, p, nplen)
        copy_digits(nq, q, nqlen)
        plen = nplen
        qlen = nqlen
        i = i + 1
    }
    printf("%lld\n", count)
    free(tmp)
    free(nq)
    free(np)
    free(q)
    free(p)
    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 max2i_i32_i32(int32_t a, int32_t b);
int32_t add_ptr_i32_i32_ptr_i32_i32_ptr_i32(int32_t* a, int32_t alen, int32_t* b, int32_t blen, int32_t* out);
void copy_digits_ptr_i32_ptr_i32_i32(int32_t* src, int32_t* dst, int32_t len);
int32_t main(void);



int32_t max2i_i32_i32(int32_t a, int32_t b) {
    if (a > b) {
        return a;
    }
    return b;
}

int32_t add_ptr_i32_i32_ptr_i32_i32_ptr_i32(int32_t* a, int32_t alen, int32_t* b, int32_t blen, int32_t* out) {
    int32_t carry = 0;
    int32_t i = 0;
    int32_t maxlen = max2i_i32_i32(alen, blen);
    while ((i < maxlen || carry > 0)) {
        int32_t v = carry;
        if (i < alen) {
            v = (v + a[i]);
        }
        if (i < blen) {
            v = (v + b[i]);
        }
        out[i] = FLOW_CHECKED_MOD((v), (10));
        carry = FLOW_CHECKED_DIV((v), (10));
        i = (i + 1);
    }
    return i;
}

void copy_digits_ptr_i32_ptr_i32_i32(int32_t* src, int32_t* dst, int32_t len) {
    int32_t i = 0;
    while (i < len) {
        dst[i] = src[i];
        i = (i + 1);
    }
}

int32_t main(void) {
    int32_t width = 400;
    int32_t* p = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* q = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* np = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* nq = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* tmp = (int32_t*)(calloc(((int64_t)(width)), 4));
    if (((((p == NULL || q == NULL) || np == NULL) || nq == NULL) || tmp == NULL)) {
        return 1;
    }
    p[0] = 3;
    q[0] = 2;
    int32_t plen = 1;
    int32_t qlen = 1;
    int64_t count = 0;
    int32_t i = 1;
    while (i <= 1000) {
        if (plen > qlen) {
            count = (count + 1);
        }
        int32_t tlen = add_ptr_i32_i32_ptr_i32_i32_ptr_i32(q, qlen, q, qlen, tmp);
        int32_t nplen = add_ptr_i32_i32_ptr_i32_i32_ptr_i32(p, plen, tmp, tlen, np);
        int32_t nqlen = add_ptr_i32_i32_ptr_i32_i32_ptr_i32(p, plen, q, qlen, nq);
        copy_digits_ptr_i32_ptr_i32_i32(np, p, nplen);
        copy_digits_ptr_i32_ptr_i32_i32(nq, q, nqlen);
        plen = nplen;
        qlen = nqlen;
        i = (i + 1);
    }
    printf("%lld\n", count);
    free(tmp);
    free(nq);
    free(np);
    free(q);
    free(p);
    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 @max2i(%arg0: i32, %arg1: i32) -> i32 {
    %0 = arith.cmpi sgt, %arg0, %arg1 : i32
    cf.cond_br %0, ^bb0, ^bb1
    ^bb0:
      func.return %arg0 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    func.return %arg1 : i32
  }
  func.func @add(%arg0: !llvm.ptr, %arg1: i32, %arg2: !llvm.ptr, %arg3: i32, %arg4: !llvm.ptr) -> i32 {
    %1 = arith.constant 0 : i32
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i32 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i32, !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
    %7 = func.call @max2i(%arg1, %arg3) : (i32, i32) -> i32
    cf.br ^bb3
    ^bb3:
    %8 = llvm.load %6 : !llvm.ptr -> i32
    %9 = arith.cmpi slt, %8, %7 : i32
    %10 = scf.if %9 -> (i1) {
      %11 = arith.constant true
      scf.yield %11 : i1
    } else {
      %12 = llvm.load %3 : !llvm.ptr -> i32
      %13 = arith.constant 0 : i32
      %14 = arith.cmpi sgt, %12, %13 : i32
      scf.yield %14 : i1
    }
    cf.cond_br %10, ^bb4, ^bb5
    ^bb4:
      %15 = llvm.load %3 : !llvm.ptr -> i32
      %16 = llvm.mlir.constant(1 : i64) : i64
      %17 = llvm.alloca %16 x i32 : (i64) -> !llvm.ptr
      llvm.store %15, %17 : i32, !llvm.ptr
      %18 = llvm.load %6 : !llvm.ptr -> i32
      %19 = arith.cmpi slt, %18, %arg1 : i32
      cf.cond_br %19, ^bb6, ^bb7
      ^bb6:
        %20 = llvm.load %17 : !llvm.ptr -> i32
        %22 = llvm.load %6 : !llvm.ptr -> i32
        %23 = arith.extsi %22 : i32 to i64
        %24 = llvm.getelementptr %arg0[%23] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %21 = llvm.load %24 : !llvm.ptr -> i32
        %25 = arith.addi %20, %21 : i32
        llvm.store %25, %17 : i32, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %26 = llvm.load %6 : !llvm.ptr -> i32
      %27 = arith.cmpi slt, %26, %arg3 : i32
      cf.cond_br %27, ^bb9, ^bb10
      ^bb9:
        %28 = llvm.load %17 : !llvm.ptr -> i32
        %30 = llvm.load %6 : !llvm.ptr -> i32
        %31 = arith.extsi %30 : i32 to i64
        %32 = llvm.getelementptr %arg2[%31] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %29 = llvm.load %32 : !llvm.ptr -> i32
        %33 = arith.addi %28, %29 : i32
        llvm.store %33, %17 : i32, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %34 = llvm.load %17 : !llvm.ptr -> i32
      %35 = arith.constant 10 : i32
      %36 = arith.remsi %34, %35 : i32
      %37 = llvm.load %6 : !llvm.ptr -> i32
      %38 = arith.extsi %37 : i32 to i64
      %39 = llvm.getelementptr %arg4[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %36, %39 : i32, !llvm.ptr
      %40 = llvm.load %17 : !llvm.ptr -> i32
      %41 = arith.constant 10 : i32
      %42 = arith.divsi %40, %41 : i32
      llvm.store %42, %3 : i32, !llvm.ptr
      %43 = llvm.load %6 : !llvm.ptr -> i32
      %44 = arith.constant 1 : i32
      %45 = arith.addi %43, %44 : i32
      llvm.store %45, %6 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %46 = llvm.load %6 : !llvm.ptr -> i32
    func.return %46 : i32
  }
  func.func @copy_digits(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i32) -> () {
    %47 = arith.constant 0 : i32
    %48 = llvm.mlir.constant(1 : i64) : i64
    %49 = llvm.alloca %48 x i32 : (i64) -> !llvm.ptr
    llvm.store %47, %49 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %50 = llvm.load %49 : !llvm.ptr -> i32
    %51 = arith.cmpi slt, %50, %arg2 : i32
    cf.cond_br %51, ^bb13, ^bb14
    ^bb13:
      %53 = llvm.load %49 : !llvm.ptr -> i32
      %54 = arith.extsi %53 : i32 to i64
      %55 = llvm.getelementptr %arg0[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %52 = llvm.load %55 : !llvm.ptr -> i32
      %56 = llvm.load %49 : !llvm.ptr -> i32
      %57 = arith.extsi %56 : i32 to i64
      %58 = llvm.getelementptr %arg1[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %52, %58 : i32, !llvm.ptr
      %59 = llvm.load %49 : !llvm.ptr -> i32
      %60 = arith.constant 1 : i32
      %61 = arith.addi %59, %60 : i32
      llvm.store %61, %49 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    func.return
  }
  func.func @main() -> i32 {
    %62 = arith.constant 400 : i32
    %64 = arith.extsi %62 : i32 to i64
    %65 = arith.constant 4 : i32
    %66 = arith.extsi %65 : i32 to i64
    %63 = func.call @calloc(%64, %66) : (i64, i64) -> !llvm.ptr
    %68 = arith.extsi %62 : i32 to i64
    %69 = arith.constant 4 : i32
    %70 = arith.extsi %69 : i32 to i64
    %67 = func.call @calloc(%68, %70) : (i64, i64) -> !llvm.ptr
    %72 = arith.extsi %62 : i32 to i64
    %73 = arith.constant 4 : i32
    %74 = arith.extsi %73 : i32 to i64
    %71 = func.call @calloc(%72, %74) : (i64, i64) -> !llvm.ptr
    %76 = arith.extsi %62 : i32 to i64
    %77 = arith.constant 4 : i32
    %78 = arith.extsi %77 : i32 to i64
    %75 = func.call @calloc(%76, %78) : (i64, i64) -> !llvm.ptr
    %80 = arith.extsi %62 : i32 to i64
    %81 = arith.constant 4 : i32
    %82 = arith.extsi %81 : i32 to i64
    %79 = func.call @calloc(%80, %82) : (i64, i64) -> !llvm.ptr
    %83 = llvm.mlir.zero : !llvm.ptr
    %84 = llvm.icmp "eq" %63, %83 : !llvm.ptr
    %85 = scf.if %84 -> (i1) {
      %86 = arith.constant true
      scf.yield %86 : i1
    } else {
      %87 = llvm.mlir.zero : !llvm.ptr
      %88 = llvm.icmp "eq" %67, %87 : !llvm.ptr
      scf.yield %88 : i1
    }
    %89 = scf.if %85 -> (i1) {
      %90 = arith.constant true
      scf.yield %90 : i1
    } else {
      %91 = llvm.mlir.zero : !llvm.ptr
      %92 = llvm.icmp "eq" %71, %91 : !llvm.ptr
      scf.yield %92 : i1
    }
    %93 = scf.if %89 -> (i1) {
      %94 = arith.constant true
      scf.yield %94 : i1
    } else {
      %95 = llvm.mlir.zero : !llvm.ptr
      %96 = llvm.icmp "eq" %75, %95 : !llvm.ptr
      scf.yield %96 : i1
    }
    %97 = scf.if %93 -> (i1) {
      %98 = arith.constant true
      scf.yield %98 : i1
    } else {
      %99 = llvm.mlir.zero : !llvm.ptr
      %100 = llvm.icmp "eq" %79, %99 : !llvm.ptr
      scf.yield %100 : i1
    }
    cf.cond_br %97, ^bb15, ^bb16
    ^bb15:
      %101 = arith.constant 1 : i32
      func.return %101 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %102 = arith.constant 3 : i32
    %103 = arith.constant 0 : i32
    %104 = arith.extsi %103 : i32 to i64
    %105 = llvm.getelementptr %63[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %102, %105 : i32, !llvm.ptr
    %106 = arith.constant 2 : i32
    %107 = arith.constant 0 : i32
    %108 = arith.extsi %107 : i32 to i64
    %109 = llvm.getelementptr %67[%108] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %106, %109 : i32, !llvm.ptr
    %110 = arith.constant 1 : i32
    %111 = llvm.mlir.constant(1 : i64) : i64
    %112 = llvm.alloca %111 x i32 : (i64) -> !llvm.ptr
    llvm.store %110, %112 : i32, !llvm.ptr
    %113 = arith.constant 1 : i32
    %114 = llvm.mlir.constant(1 : i64) : i64
    %115 = llvm.alloca %114 x i32 : (i64) -> !llvm.ptr
    llvm.store %113, %115 : i32, !llvm.ptr
    %116 = arith.constant 0 : i32
    %117 = arith.extsi %116 : i32 to i64
    %118 = llvm.mlir.constant(1 : i64) : i64
    %119 = llvm.alloca %118 x i64 : (i64) -> !llvm.ptr
    llvm.store %117, %119 : i64, !llvm.ptr
    %120 = arith.constant 1 : i32
    %121 = llvm.mlir.constant(1 : i64) : i64
    %122 = llvm.alloca %121 x i32 : (i64) -> !llvm.ptr
    llvm.store %120, %122 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %123 = llvm.load %122 : !llvm.ptr -> i32
    %124 = arith.constant 1000 : i32
    %125 = arith.cmpi sle, %123, %124 : i32
    cf.cond_br %125, ^bb19, ^bb20
    ^bb19:
      %126 = llvm.load %112 : !llvm.ptr -> i32
      %127 = llvm.load %115 : !llvm.ptr -> i32
      %128 = arith.cmpi sgt, %126, %127 : i32
      cf.cond_br %128, ^bb21, ^bb22
      ^bb21:
        %129 = llvm.load %119 : !llvm.ptr -> i64
        %130 = arith.constant 1 : i32
        %132 = arith.extsi %130 : i32 to i64
        %131 = arith.addi %129, %132 : i64
        llvm.store %131, %119 : i64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %134 = llvm.load %115 : !llvm.ptr -> i32
      %135 = llvm.load %115 : !llvm.ptr -> i32
      %133 = func.call @add(%67, %134, %67, %135, %79) : (!llvm.ptr, i32, !llvm.ptr, i32, !llvm.ptr) -> i32
      %137 = llvm.load %112 : !llvm.ptr -> i32
      %136 = func.call @add(%63, %137, %79, %133, %71) : (!llvm.ptr, i32, !llvm.ptr, i32, !llvm.ptr) -> i32
      %139 = llvm.load %112 : !llvm.ptr -> i32
      %140 = llvm.load %115 : !llvm.ptr -> i32
      %138 = func.call @add(%63, %139, %67, %140, %75) : (!llvm.ptr, i32, !llvm.ptr, i32, !llvm.ptr) -> i32
      func.call @copy_digits(%71, %63, %136) : (!llvm.ptr, !llvm.ptr, i32) -> ()
      func.call @copy_digits(%75, %67, %138) : (!llvm.ptr, !llvm.ptr, i32) -> ()
      llvm.store %136, %112 : i32, !llvm.ptr
      llvm.store %138, %115 : i32, !llvm.ptr
      %143 = llvm.load %122 : !llvm.ptr -> i32
      %144 = arith.constant 1 : i32
      %145 = arith.addi %143, %144 : i32
      llvm.store %145, %122 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %146 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %147 = llvm.load %119 : !llvm.ptr -> i64
    %148 = llvm.call @printf(%146, %147) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%79) : (!llvm.ptr) -> ()
    func.call @free(%75) : (!llvm.ptr) -> ()
    func.call @free(%71) : (!llvm.ptr) -> ()
    func.call @free(%67) : (!llvm.ptr) -> ()
    func.call @free(%63) : (!llvm.ptr) -> ()
    %154 = arith.constant 0 : i32
    func.return %154 : i32
  }
}