Problem 053

How many values of C(n,r) for 1 ≤ n ≤ 100 are greater than one million?

Answer4075
Output4075
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^2)
Space complexityO(n)O(n^2)
ApproachFlow solutionPascal triangle combinatorics
VerdictOptimal

Flow source

# Project Euler 053
# How many values of C(n,r) for 1 ≤ n ≤ 100 are greater than one million?

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

function main() -> i32 {
    # Pascal triangle with capping at 1000001 (only care > 1e6)
    let nmax: i32 = 100
    let row: ptr<i64> = calloc((nmax + 1) as i64, 8)
    if row == null { return 1 }
    row[0] = 1
    let mut count: i64 = 0
    let mut n: i32 = 1
    while n <= nmax {
        # build next row right-to-left in place
        row[n] = 1
        let mut r: i32 = n - 1
        while r >= 1 {
            let mut v: i64 = row[r] + row[r - 1]
            if v > 1000001 {
                v = 1000001
            }
            row[r] = v
            r = r - 1
        }
        r = 0
        while r <= n {
            if row[r] > 1000000 {
                count = count + 1
            }
            r = r + 1
        }
        n = n + 1
    }
    printf("%lld\n", count)
    free(row)
    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 nmax = 100;
    int64_t* row = (int64_t*)(calloc(((int64_t)((nmax + 1))), 8));
    if (row == NULL) {
        return 1;
    }
    row[0] = 1;
    int64_t count = 0;
    int32_t n = 1;
    while (n <= nmax) {
        row[n] = 1;
        int32_t r = (n - 1);
        while (r >= 1) {
            int64_t v = (row[r] + row[(r - 1)]);
            if (v > 1000001) {
                v = 1000001;
            }
            row[r] = v;
            r = (r - 1);
        }
        r = 0;
        while (r <= n) {
            if (row[r] > 1000000) {
                count = (count + 1);
            }
            r = (r + 1);
        }
        n = (n + 1);
    }
    printf("%lld\n", count);
    free(row);
    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 100 : i32
    %2 = arith.constant 1 : i32
    %3 = arith.addi %0, %2 : i32
    %4 = arith.extsi %3 : i32 to i64
    %5 = arith.constant 8 : i32
    %6 = arith.extsi %5 : i32 to i64
    %1 = func.call @calloc(%4, %6) : (i64, i64) -> !llvm.ptr
    %7 = llvm.mlir.zero : !llvm.ptr
    %8 = llvm.icmp "eq" %1, %7 : !llvm.ptr
    cf.cond_br %8, ^bb0, ^bb1
    ^bb0:
      %9 = arith.constant 1 : i32
      func.return %9 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %10 = arith.constant 1 : i32
    %11 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %13 = arith.extsi %11 : i32 to i64
    %14 = llvm.getelementptr %1[%13] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %12, %14 : i64, !llvm.ptr
    %15 = arith.constant 0 : i32
    %16 = arith.extsi %15 : i32 to i64
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %16, %18 : i64, !llvm.ptr
    %19 = arith.constant 1 : i32
    %20 = llvm.mlir.constant(1 : i64) : i64
    %21 = llvm.alloca %20 x i32 : (i64) -> !llvm.ptr
    llvm.store %19, %21 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %22 = llvm.load %21 : !llvm.ptr -> i32
    %23 = arith.cmpi sle, %22, %0 : i32
    cf.cond_br %23, ^bb4, ^bb5
    ^bb4:
      %24 = arith.constant 1 : i32
      %25 = llvm.load %21 : !llvm.ptr -> i32
      %26 = arith.extsi %24 : i32 to i64
      %27 = arith.extsi %25 : i32 to i64
      %28 = llvm.getelementptr %1[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %26, %28 : i64, !llvm.ptr
      %29 = llvm.load %21 : !llvm.ptr -> i32
      %30 = arith.constant 1 : i32
      %31 = arith.subi %29, %30 : i32
      %32 = llvm.mlir.constant(1 : i64) : i64
      %33 = llvm.alloca %32 x i32 : (i64) -> !llvm.ptr
      llvm.store %31, %33 : i32, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %34 = llvm.load %33 : !llvm.ptr -> i32
      %35 = arith.constant 1 : i32
      %36 = arith.cmpi sge, %34, %35 : i32
      cf.cond_br %36, ^bb7, ^bb8
      ^bb7:
        %38 = llvm.load %33 : !llvm.ptr -> i32
        %39 = arith.extsi %38 : i32 to i64
        %40 = llvm.getelementptr %1[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %37 = llvm.load %40 : !llvm.ptr -> i64
        %42 = llvm.load %33 : !llvm.ptr -> i32
        %43 = arith.constant 1 : i32
        %44 = arith.subi %42, %43 : i32
        %45 = arith.extsi %44 : i32 to i64
        %46 = llvm.getelementptr %1[%45] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %41 = llvm.load %46 : !llvm.ptr -> i64
        %47 = arith.addi %37, %41 : i64
        %48 = llvm.mlir.constant(1 : i64) : i64
        %49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
        llvm.store %47, %49 : i64, !llvm.ptr
        %50 = llvm.load %49 : !llvm.ptr -> i64
        %51 = arith.constant 1000001 : i32
        %53 = arith.extsi %51 : i32 to i64
        %52 = arith.cmpi sgt, %50, %53 : i64
        cf.cond_br %52, ^bb9, ^bb10
        ^bb9:
          %54 = arith.constant 1000001 : i32
          %55 = arith.extsi %54 : i32 to i64
          llvm.store %55, %49 : i64, !llvm.ptr
          cf.br ^bb11
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %56 = llvm.load %49 : !llvm.ptr -> i64
        %57 = llvm.load %33 : !llvm.ptr -> i32
        %58 = arith.extsi %57 : i32 to i64
        %59 = llvm.getelementptr %1[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %56, %59 : i64, !llvm.ptr
        %60 = llvm.load %33 : !llvm.ptr -> i32
        %61 = arith.constant 1 : i32
        %62 = arith.subi %60, %61 : i32
        llvm.store %62, %33 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %63 = arith.constant 0 : i32
      llvm.store %63, %33 : i32, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %64 = llvm.load %33 : !llvm.ptr -> i32
      %65 = llvm.load %21 : !llvm.ptr -> i32
      %66 = arith.cmpi sle, %64, %65 : i32
      cf.cond_br %66, ^bb13, ^bb14
      ^bb13:
        %68 = llvm.load %33 : !llvm.ptr -> i32
        %69 = arith.extsi %68 : i32 to i64
        %70 = llvm.getelementptr %1[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %67 = llvm.load %70 : !llvm.ptr -> i64
        %71 = arith.constant 1000000 : i32
        %73 = arith.extsi %71 : i32 to i64
        %72 = arith.cmpi sgt, %67, %73 : i64
        cf.cond_br %72, ^bb15, ^bb16
        ^bb15:
          %74 = llvm.load %18 : !llvm.ptr -> i64
          %75 = arith.constant 1 : i32
          %77 = arith.extsi %75 : i32 to i64
          %76 = arith.addi %74, %77 : i64
          llvm.store %76, %18 : i64, !llvm.ptr
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %78 = llvm.load %33 : !llvm.ptr -> i32
        %79 = arith.constant 1 : i32
        %80 = arith.addi %78, %79 : i32
        llvm.store %80, %33 : i32, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %81 = llvm.load %21 : !llvm.ptr -> i32
      %82 = arith.constant 1 : i32
      %83 = arith.addi %81, %82 : i32
      llvm.store %83, %21 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %84 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %85 = llvm.load %18 : !llvm.ptr -> i64
    %86 = llvm.call @printf(%84, %85) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%1) : (!llvm.ptr) -> ()
    %88 = arith.constant 0 : i32
    func.return %88 : i32
  }
}