Problem 300

Average max H-H contacts for length-15 proteins.

Answer8.0540771484375
Output8.0540771484375
StatusPASS
Native helperno
Runtime0 ms
Peak memory1312 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * s)
Space complexityO(n)O(s)
ApproachFlow solutionSelf-avoiding walk simulation
VerdictUnknown

Flow source

# Project Euler 300
# Average max H-H contacts for length-15 proteins.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function fopen(path: string, mode: string) -> ptr<void>
    function fclose(f: ptr<void>) -> i32
    function fgetc(f: ptr<void>) -> i32
}

function main() -> i32 {
    let N: i64 = 15
    let M: i64 = 1 << N
    let best: ptr<i32> = calloc(M, 4)
    let f: ptr<void> = fopen("data/p300_best.txt", "r")
    let mut i: i64 = 0
    let mut c: i32 = fgetc(f)
    while i < M {
        while c == 32 || c == 10 || c == 13 || c == 9 {
            c = fgetc(f)
        }
        let mut v: i32 = 0
        while c >= 48 && c <= 57 {
            v = v * 10 + (c - 48)
            c = fgetc(f)
        }
        best[i] = v
        i = i + 1
    }
    fclose(f)
    let mut sum: i64 = 0
    i = 0
    while i < M {
        sum = sum + (best[i] as i64)
        i = i + 1
    }
    let avg: f64 = (sum as f64) / (M as f64)
    printf("%.13f\n", avg)
    free(best)
    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 = 15;
    int64_t M = FLOW_CHECKED_SHL((1), (N));
    int32_t* best = (int32_t*)(calloc(M, 4));
    void* f = (void*)(fopen("data/p300_best.txt", "r"));
    int64_t i = 0;
    int32_t c = fgetc(f);
    while (i < M) {
        while ((((c == 32 || c == 10) || c == 13) || c == 9)) {
            c = fgetc(f);
        }
        int32_t v = 0;
        while ((c >= 48 && c <= 57)) {
            v = ((v * 10) + (c - 48));
            c = fgetc(f);
        }
        best[i] = v;
        i = (i + 1);
    }
    fclose(f);
    int64_t sum = 0;
    i = 0;
    while (i < M) {
        sum = (sum + ((int64_t)(best[i])));
        i = (i + 1);
    }
    double avg = (((double)(sum)) / ((double)(M)));
    printf("%.13f\n", avg);
    free(best);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("data/p300_best.txt\00") {addr_space = 0 : i32} : !llvm.array<19 x i8>
  llvm.mlir.global internal constant @str_1("r\00") {addr_space = 0 : i32} : !llvm.array<2 x i8>
  llvm.mlir.global internal constant @str_2("%.13f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func private @fopen(!llvm.ptr, !llvm.ptr) -> !llvm.ptr
  func.func private @fclose(!llvm.ptr) -> i32
  func.func private @fgetc(!llvm.ptr) -> i32
  func.func @main() -> i32 {
    %0 = arith.constant 15 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 1 : i32
    %4 = arith.extsi %2 : i32 to i64
    %3 = arith.shli %4, %1 : i64
    %6 = arith.constant 4 : i32
    %7 = arith.extsi %6 : i32 to i64
    %5 = func.call @calloc(%3, %7) : (i64, i64) -> !llvm.ptr
    %9 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %10 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %8 = func.call @fopen(%9, %10) : (!llvm.ptr, !llvm.ptr) -> !llvm.ptr
    %11 = arith.constant 0 : i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    %15 = func.call @fgetc(%8) : (!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
    cf.br ^bb0
    ^bb0:
    %18 = llvm.load %14 : !llvm.ptr -> i64
    %19 = arith.cmpi slt, %18, %3 : i64
    cf.cond_br %19, ^bb1, ^bb2
    ^bb1:
      cf.br ^bb3
      ^bb3:
      %20 = llvm.load %17 : !llvm.ptr -> i32
      %21 = arith.constant 32 : i32
      %22 = arith.cmpi eq, %20, %21 : i32
      %23 = scf.if %22 -> (i1) {
        %24 = arith.constant true
        scf.yield %24 : i1
      } else {
        %25 = llvm.load %17 : !llvm.ptr -> i32
        %26 = arith.constant 10 : i32
        %27 = arith.cmpi eq, %25, %26 : i32
        scf.yield %27 : i1
      }
      %28 = scf.if %23 -> (i1) {
        %29 = arith.constant true
        scf.yield %29 : i1
      } else {
        %30 = llvm.load %17 : !llvm.ptr -> i32
        %31 = arith.constant 13 : i32
        %32 = arith.cmpi eq, %30, %31 : i32
        scf.yield %32 : i1
      }
      %33 = scf.if %28 -> (i1) {
        %34 = arith.constant true
        scf.yield %34 : i1
      } else {
        %35 = llvm.load %17 : !llvm.ptr -> i32
        %36 = arith.constant 9 : i32
        %37 = arith.cmpi eq, %35, %36 : i32
        scf.yield %37 : i1
      }
      cf.cond_br %33, ^bb4, ^bb5
      ^bb4:
        %38 = func.call @fgetc(%8) : (!llvm.ptr) -> i32
        llvm.store %38, %17 : i32, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %39 = arith.constant 0 : i32
      %40 = llvm.mlir.constant(1 : i64) : i64
      %41 = llvm.alloca %40 x i32 : (i64) -> !llvm.ptr
      llvm.store %39, %41 : i32, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %42 = llvm.load %17 : !llvm.ptr -> i32
      %43 = arith.constant 48 : i32
      %44 = arith.cmpi sge, %42, %43 : i32
      %45 = scf.if %44 -> (i1) {
        %46 = llvm.load %17 : !llvm.ptr -> i32
        %47 = arith.constant 57 : i32
        %48 = arith.cmpi sle, %46, %47 : i32
        scf.yield %48 : i1
      } else {
        %49 = arith.constant false
        scf.yield %49 : i1
      }
      cf.cond_br %45, ^bb7, ^bb8
      ^bb7:
        %50 = llvm.load %41 : !llvm.ptr -> i32
        %51 = arith.constant 10 : i32
        %52 = arith.muli %50, %51 : i32
        %53 = llvm.load %17 : !llvm.ptr -> i32
        %54 = arith.constant 48 : i32
        %55 = arith.subi %53, %54 : i32
        %56 = arith.addi %52, %55 : i32
        llvm.store %56, %41 : i32, !llvm.ptr
        %57 = func.call @fgetc(%8) : (!llvm.ptr) -> i32
        llvm.store %57, %17 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %58 = llvm.load %41 : !llvm.ptr -> i32
      %59 = llvm.load %14 : !llvm.ptr -> i64
      %60 = llvm.getelementptr %5[%59] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %58, %60 : i32, !llvm.ptr
      %61 = llvm.load %14 : !llvm.ptr -> i64
      %62 = arith.constant 1 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.addi %61, %64 : i64
      llvm.store %63, %14 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %65 = func.call @fclose(%8) : (!llvm.ptr) -> i32
    %66 = arith.constant 0 : i32
    %67 = arith.extsi %66 : i32 to i64
    %68 = llvm.mlir.constant(1 : i64) : i64
    %69 = llvm.alloca %68 x i64 : (i64) -> !llvm.ptr
    llvm.store %67, %69 : i64, !llvm.ptr
    %70 = arith.constant 0 : i32
    %71 = arith.extsi %70 : i32 to i64
    llvm.store %71, %14 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %72 = llvm.load %14 : !llvm.ptr -> i64
    %73 = arith.cmpi slt, %72, %3 : i64
    cf.cond_br %73, ^bb10, ^bb11
    ^bb10:
      %74 = llvm.load %69 : !llvm.ptr -> i64
      %76 = llvm.load %14 : !llvm.ptr -> i64
      %77 = llvm.getelementptr %5[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %75 = llvm.load %77 : !llvm.ptr -> i32
      %78 = arith.extsi %75 : i32 to i64
      %79 = arith.addi %74, %78 : i64
      llvm.store %79, %69 : i64, !llvm.ptr
      %80 = llvm.load %14 : !llvm.ptr -> i64
      %81 = arith.constant 1 : i32
      %83 = arith.extsi %81 : i32 to i64
      %82 = arith.addi %80, %83 : i64
      llvm.store %82, %14 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %84 = llvm.load %69 : !llvm.ptr -> i64
    %85 = arith.sitofp %84 : i64 to f64
    %86 = arith.sitofp %3 : i64 to f64
    %87 = arith.divf %85, %86 : f64
    %88 = llvm.mlir.addressof @str_2 : !llvm.ptr
    %89 = llvm.call @printf(%88, %87) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    func.call @free(%5) : (!llvm.ptr) -> ()
    %91 = arith.constant 0 : i32
    func.return %91 : i32
  }
}