Problem 948

Left vs Right — Catalan-based recurrence for F(n). Find F(60).

Answer1033654680825334184
Output1033654680825334184
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n^2)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 948
# Left vs Right — Catalan-based recurrence for F(n). Find F(60).

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

function main() -> i32 {
    let n_max: i64 = 60
    let m_max: i64 = n_max / 2
    let Cat: ptr<i64> = calloc(m_max + 1, 8)
    let A: ptr<i64> = calloc(n_max + 1, 8)
    let B: ptr<i64> = calloc(n_max + 1, 8)
    let C: ptr<i64> = calloc(n_max + 1, 8)
    if Cat == null || A == null || B == null || C == null { return 1 }
    Cat[0] = 1
    let mut n: i64 = 1
    while n <= m_max {
        let mut s: i64 = 0
        let mut i: i64 = 0
        while i < n {
            s = s + Cat[i] * Cat[n - 1 - i]
            i = i + 1
        }
        Cat[n] = s
        n = n + 1
    }
    C[2] = 1
    n = 3
    while n <= n_max {
        C[n] = (1 as i64) << (n - 2)
        if (n & 1) == 0 {
            let m: i64 = n / 2
            A[n] = 2 * A[n - 1]
            B[n] = 2 * B[n - 1] + Cat[m - 1]
        } else {
            let m2: i64 = (n - 1) / 2
            A[n] = 2 * A[n - 1] + Cat[m2 - 1]
            B[n] = 2 * B[n - 1]
        }
        n = n + 1
    }
    let F60: i64 = 2 * A[60] + B[60] + C[60]
    printf("%lld\n", F60)
    free(Cat)
    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) {
    int64_t n_max = 60;
    int64_t m_max = FLOW_CHECKED_DIV((n_max), (2));
    int64_t* Cat = (int64_t*)(calloc((m_max + 1), 8));
    int64_t* A = (int64_t*)(calloc((n_max + 1), 8));
    int64_t* B = (int64_t*)(calloc((n_max + 1), 8));
    int64_t* C = (int64_t*)(calloc((n_max + 1), 8));
    if ((((Cat == NULL || A == NULL) || B == NULL) || C == NULL)) {
        return 1;
    }
    Cat[0] = 1;
    int64_t n = 1;
    while (n <= m_max) {
        int64_t s = 0;
        int64_t i = 0;
        while (i < n) {
            s = (s + (Cat[i] * Cat[((n - 1) - i)]));
            i = (i + 1);
        }
        Cat[n] = s;
        n = (n + 1);
    }
    C[2] = 1;
    n = 3;
    while (n <= n_max) {
        C[n] = FLOW_CHECKED_SHL((((int64_t)(1))), ((n - 2)));
        if ((n & 1) == 0) {
            int64_t m = FLOW_CHECKED_DIV((n), (2));
            A[n] = (2 * A[(n - 1)]);
            B[n] = ((2 * B[(n - 1)]) + Cat[(m - 1)]);
        } else {
            int64_t m2 = FLOW_CHECKED_DIV(((n - 1)), (2));
            A[n] = ((2 * A[(n - 1)]) + Cat[(m2 - 1)]);
            B[n] = (2 * B[(n - 1)]);
        }
        n = (n + 1);
    }
    int64_t F60 = (((2 * A[60]) + B[60]) + C[60]);
    printf("%lld\n", F60);
    free(Cat);
    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 @main() -> i32 {
    %0 = arith.constant 60 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 2 : i32
    %4 = arith.extsi %2 : i32 to i64
    %3 = arith.divsi %1, %4 : i64
    %6 = arith.constant 1 : i32
    %8 = arith.extsi %6 : i32 to i64
    %7 = arith.addi %3, %8 : i64
    %9 = arith.constant 8 : i32
    %10 = arith.extsi %9 : i32 to i64
    %5 = func.call @calloc(%7, %10) : (i64, i64) -> !llvm.ptr
    %12 = arith.constant 1 : i32
    %14 = arith.extsi %12 : i32 to i64
    %13 = arith.addi %1, %14 : i64
    %15 = arith.constant 8 : i32
    %16 = arith.extsi %15 : i32 to i64
    %11 = func.call @calloc(%13, %16) : (i64, i64) -> !llvm.ptr
    %18 = arith.constant 1 : i32
    %20 = arith.extsi %18 : i32 to i64
    %19 = arith.addi %1, %20 : i64
    %21 = arith.constant 8 : i32
    %22 = arith.extsi %21 : i32 to i64
    %17 = func.call @calloc(%19, %22) : (i64, i64) -> !llvm.ptr
    %24 = arith.constant 1 : i32
    %26 = arith.extsi %24 : i32 to i64
    %25 = arith.addi %1, %26 : i64
    %27 = arith.constant 8 : i32
    %28 = arith.extsi %27 : i32 to i64
    %23 = func.call @calloc(%25, %28) : (i64, i64) -> !llvm.ptr
    %29 = llvm.mlir.zero : !llvm.ptr
    %30 = llvm.icmp "eq" %5, %29 : !llvm.ptr
    %31 = scf.if %30 -> (i1) {
      %32 = arith.constant true
      scf.yield %32 : i1
    } else {
      %33 = llvm.mlir.zero : !llvm.ptr
      %34 = llvm.icmp "eq" %11, %33 : !llvm.ptr
      scf.yield %34 : i1
    }
    %35 = scf.if %31 -> (i1) {
      %36 = arith.constant true
      scf.yield %36 : i1
    } else {
      %37 = llvm.mlir.zero : !llvm.ptr
      %38 = llvm.icmp "eq" %17, %37 : !llvm.ptr
      scf.yield %38 : i1
    }
    %39 = scf.if %35 -> (i1) {
      %40 = arith.constant true
      scf.yield %40 : i1
    } else {
      %41 = llvm.mlir.zero : !llvm.ptr
      %42 = llvm.icmp "eq" %23, %41 : !llvm.ptr
      scf.yield %42 : i1
    }
    cf.cond_br %39, ^bb0, ^bb1
    ^bb0:
      %43 = arith.constant 1 : i32
      func.return %43 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %44 = arith.constant 1 : i32
    %45 = arith.constant 0 : i32
    %46 = arith.extsi %44 : i32 to i64
    %47 = arith.extsi %45 : i32 to i64
    %48 = llvm.getelementptr %5[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %46, %48 : i64, !llvm.ptr
    %49 = arith.constant 1 : i32
    %50 = arith.extsi %49 : i32 to i64
    %51 = llvm.mlir.constant(1 : i64) : i64
    %52 = llvm.alloca %51 x i64 : (i64) -> !llvm.ptr
    llvm.store %50, %52 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %53 = llvm.load %52 : !llvm.ptr -> i64
    %54 = arith.cmpi sle, %53, %3 : i64
    cf.cond_br %54, ^bb4, ^bb5
    ^bb4:
      %55 = arith.constant 0 : i32
      %56 = arith.extsi %55 : i32 to i64
      %57 = llvm.mlir.constant(1 : i64) : i64
      %58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
      llvm.store %56, %58 : i64, !llvm.ptr
      %59 = arith.constant 0 : i32
      %60 = arith.extsi %59 : i32 to i64
      %61 = llvm.mlir.constant(1 : i64) : i64
      %62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
      llvm.store %60, %62 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %63 = llvm.load %62 : !llvm.ptr -> i64
      %64 = llvm.load %52 : !llvm.ptr -> i64
      %65 = arith.cmpi slt, %63, %64 : i64
      cf.cond_br %65, ^bb7, ^bb8
      ^bb7:
        %66 = llvm.load %58 : !llvm.ptr -> i64
        %68 = llvm.load %62 : !llvm.ptr -> i64
        %69 = llvm.getelementptr %5[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %67 = llvm.load %69 : !llvm.ptr -> i64
        %71 = llvm.load %52 : !llvm.ptr -> i64
        %72 = arith.constant 1 : i32
        %74 = arith.extsi %72 : i32 to i64
        %73 = arith.subi %71, %74 : i64
        %75 = llvm.load %62 : !llvm.ptr -> i64
        %76 = arith.subi %73, %75 : i64
        %77 = llvm.getelementptr %5[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %70 = llvm.load %77 : !llvm.ptr -> i64
        %78 = arith.muli %67, %70 : i64
        %79 = arith.addi %66, %78 : i64
        llvm.store %79, %58 : i64, !llvm.ptr
        %80 = llvm.load %62 : !llvm.ptr -> i64
        %81 = arith.constant 1 : i32
        %83 = arith.extsi %81 : i32 to i64
        %82 = arith.addi %80, %83 : i64
        llvm.store %82, %62 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %84 = llvm.load %58 : !llvm.ptr -> i64
      %85 = llvm.load %52 : !llvm.ptr -> i64
      %86 = llvm.getelementptr %5[%85] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %84, %86 : i64, !llvm.ptr
      %87 = llvm.load %52 : !llvm.ptr -> i64
      %88 = arith.constant 1 : i32
      %90 = arith.extsi %88 : i32 to i64
      %89 = arith.addi %87, %90 : i64
      llvm.store %89, %52 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %91 = arith.constant 1 : i32
    %92 = arith.constant 2 : i32
    %93 = arith.extsi %91 : i32 to i64
    %94 = arith.extsi %92 : i32 to i64
    %95 = llvm.getelementptr %23[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %93, %95 : i64, !llvm.ptr
    %96 = arith.constant 3 : i32
    %97 = arith.extsi %96 : i32 to i64
    llvm.store %97, %52 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %98 = llvm.load %52 : !llvm.ptr -> i64
    %99 = arith.cmpi sle, %98, %1 : i64
    cf.cond_br %99, ^bb10, ^bb11
    ^bb10:
      %100 = arith.constant 1 : i32
      %101 = arith.extsi %100 : i32 to i64
      %102 = llvm.load %52 : !llvm.ptr -> i64
      %103 = arith.constant 2 : i32
      %105 = arith.extsi %103 : i32 to i64
      %104 = arith.subi %102, %105 : i64
      %106 = arith.shli %101, %104 : i64
      %107 = llvm.load %52 : !llvm.ptr -> i64
      %108 = llvm.getelementptr %23[%107] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %106, %108 : i64, !llvm.ptr
      %109 = llvm.load %52 : !llvm.ptr -> i64
      %110 = arith.constant 1 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.andi %109, %112 : i64
      %113 = arith.constant 0 : i32
      %115 = arith.extsi %113 : i32 to i64
      %114 = arith.cmpi eq, %111, %115 : i64
      cf.cond_br %114, ^bb12, ^bb13
      ^bb12:
        %116 = llvm.load %52 : !llvm.ptr -> i64
        %117 = arith.constant 2 : i32
        %119 = arith.extsi %117 : i32 to i64
        %118 = arith.divsi %116, %119 : i64
        %120 = arith.constant 2 : i32
        %122 = llvm.load %52 : !llvm.ptr -> i64
        %123 = arith.constant 1 : i32
        %125 = arith.extsi %123 : i32 to i64
        %124 = arith.subi %122, %125 : i64
        %126 = llvm.getelementptr %11[%124] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %121 = llvm.load %126 : !llvm.ptr -> i64
        %128 = arith.extsi %120 : i32 to i64
        %127 = arith.muli %128, %121 : i64
        %129 = llvm.load %52 : !llvm.ptr -> i64
        %130 = llvm.getelementptr %11[%129] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %127, %130 : i64, !llvm.ptr
        %131 = arith.constant 2 : i32
        %133 = llvm.load %52 : !llvm.ptr -> i64
        %134 = arith.constant 1 : i32
        %136 = arith.extsi %134 : i32 to i64
        %135 = arith.subi %133, %136 : i64
        %137 = llvm.getelementptr %17[%135] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %132 = llvm.load %137 : !llvm.ptr -> i64
        %139 = arith.extsi %131 : i32 to i64
        %138 = arith.muli %139, %132 : i64
        %141 = arith.constant 1 : i32
        %143 = arith.extsi %141 : i32 to i64
        %142 = arith.subi %118, %143 : i64
        %144 = llvm.getelementptr %5[%142] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %140 = llvm.load %144 : !llvm.ptr -> i64
        %145 = arith.addi %138, %140 : i64
        %146 = llvm.load %52 : !llvm.ptr -> i64
        %147 = llvm.getelementptr %17[%146] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %145, %147 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        %148 = llvm.load %52 : !llvm.ptr -> i64
        %149 = arith.constant 1 : i32
        %151 = arith.extsi %149 : i32 to i64
        %150 = arith.subi %148, %151 : i64
        %152 = arith.constant 2 : i32
        %154 = arith.extsi %152 : i32 to i64
        %153 = arith.divsi %150, %154 : i64
        %155 = arith.constant 2 : i32
        %157 = llvm.load %52 : !llvm.ptr -> i64
        %158 = arith.constant 1 : i32
        %160 = arith.extsi %158 : i32 to i64
        %159 = arith.subi %157, %160 : i64
        %161 = llvm.getelementptr %11[%159] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %156 = llvm.load %161 : !llvm.ptr -> i64
        %163 = arith.extsi %155 : i32 to i64
        %162 = arith.muli %163, %156 : i64
        %165 = arith.constant 1 : i32
        %167 = arith.extsi %165 : i32 to i64
        %166 = arith.subi %153, %167 : i64
        %168 = llvm.getelementptr %5[%166] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %164 = llvm.load %168 : !llvm.ptr -> i64
        %169 = arith.addi %162, %164 : i64
        %170 = llvm.load %52 : !llvm.ptr -> i64
        %171 = llvm.getelementptr %11[%170] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %169, %171 : i64, !llvm.ptr
        %172 = arith.constant 2 : i32
        %174 = llvm.load %52 : !llvm.ptr -> i64
        %175 = arith.constant 1 : i32
        %177 = arith.extsi %175 : i32 to i64
        %176 = arith.subi %174, %177 : i64
        %178 = llvm.getelementptr %17[%176] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %173 = llvm.load %178 : !llvm.ptr -> i64
        %180 = arith.extsi %172 : i32 to i64
        %179 = arith.muli %180, %173 : i64
        %181 = llvm.load %52 : !llvm.ptr -> i64
        %182 = llvm.getelementptr %17[%181] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %179, %182 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb14:
      %183 = llvm.load %52 : !llvm.ptr -> i64
      %184 = arith.constant 1 : i32
      %186 = arith.extsi %184 : i32 to i64
      %185 = arith.addi %183, %186 : i64
      llvm.store %185, %52 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %187 = arith.constant 2 : i32
    %189 = arith.constant 60 : i32
    %190 = arith.extsi %189 : i32 to i64
    %191 = llvm.getelementptr %11[%190] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %188 = llvm.load %191 : !llvm.ptr -> i64
    %193 = arith.extsi %187 : i32 to i64
    %192 = arith.muli %193, %188 : i64
    %195 = arith.constant 60 : i32
    %196 = arith.extsi %195 : i32 to i64
    %197 = llvm.getelementptr %17[%196] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %194 = llvm.load %197 : !llvm.ptr -> i64
    %198 = arith.addi %192, %194 : i64
    %200 = arith.constant 60 : i32
    %201 = arith.extsi %200 : i32 to i64
    %202 = llvm.getelementptr %23[%201] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %199 = llvm.load %202 : !llvm.ptr -> i64
    %203 = arith.addi %198, %199 : i64
    %204 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %205 = llvm.call @printf(%204, %203) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%5) : (!llvm.ptr) -> ()
    func.call @free(%11) : (!llvm.ptr) -> ()
    func.call @free(%17) : (!llvm.ptr) -> ()
    func.call @free(%23) : (!llvm.ptr) -> ()
    %210 = arith.constant 0 : i32
    func.return %210 : i32
  }
}