Problem 008

Greatest product of thirteen adjacent digits in the 1000-digit number.

Answer23514624000
Output23514624000
StatusPASS
Native helperno
Runtime0 ms
Peak memory1104 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(1)O(1)
ApproachFlow solutionSliding window product
VerdictSuboptimal

Flow source

# Project Euler 008
# Greatest product of thirteen adjacent digits in the 1000-digit number.

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

function load_digits(path: string, out: ptr<i32>, cap: i32) -> i32 {
    let f: ptr<void> = fopen(path, "r")
    if f == null {
        return -1
    }
    let mut n: i32 = 0
    let mut c: i32 = fgetc(f)
    while c >= 0 && n < cap {
        if c >= 48 && c <= 57 {
            out[n] = c - 48
            n = n + 1
        }
        c = fgetc(f)
    }
    fclose(f)
    return n
}

function solve(digits: ptr<i32>, n: i32, window: i32) -> i64 {
    let mut best: i64 = 0
    for i in 0..(n - window + 1) {
        let mut prod: i64 = 1
        for j in 0..window {
            prod = prod * (digits[i + j] as i64)
        }
        if prod > best {
            best = prod
        }
    }
    return best
}

function main() -> i32 {
    let digits: ptr<i32> = calloc(1000, 4)
    if digits == null {
        printf("oom\n")
        return 1
    }
    let n: i32 = load_digits("data/p008.txt", digits, 1000)
    if n < 0 {
        printf("failed to read data/p008.txt\n")
        free(digits)
        return 1
    }
    printf("%lld\n", solve(digits, n, 13))
    free(digits)
    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 load_digits_string_ptr_i32_i32(char* path, int32_t* out, int32_t cap);
int64_t solve_ptr_i32_i32_i32(int32_t* digits, int32_t n, int32_t window);
int32_t main(void);






int32_t load_digits_string_ptr_i32_i32(char* path, int32_t* out, int32_t cap) {
    void* f = (void*)(fopen(path, "r"));
    if (f == NULL) {
        return (-1);
    }
    int32_t n = 0;
    int32_t c = fgetc(f);
    while ((c >= 0 && n < cap)) {
        if ((c >= 48 && c <= 57)) {
            out[n] = (c - 48);
            n = (n + 1);
        }
        c = fgetc(f);
    }
    fclose(f);
    return n;
}

int64_t solve_ptr_i32_i32_i32(int32_t* digits, int32_t n, int32_t window) {
    int64_t best = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= ((n - window) + 1)) ? i < ((n - window) + 1) : i > ((n - window) + 1); i += (0 <= ((n - window) + 1)) ? 1 : -1) {
        int64_t prod = 1;
        int32_t __flow_step_2 = 1;
        for (int32_t j = 0; (0 <= window) ? j < window : j > window; j += (0 <= window) ? 1 : -1) {
            prod = (prod * ((int64_t)(digits[(i + j)])));
        }
        if (prod > best) {
            best = prod;
        }
    }
    return best;
}

int32_t main(void) {
    int32_t* digits = (int32_t*)(calloc(1000, 4));
    if (digits == NULL) {
        printf("oom\n");
        return 1;
    }
    int32_t n = load_digits_string_ptr_i32_i32("data/p008.txt", digits, 1000);
    if (n < 0) {
        printf("failed to read data/p008.txt\n");
        free(digits);
        return 1;
    }
    printf("%lld\n", solve_ptr_i32_i32_i32(digits, n, 13));
    free(digits);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("r\00") {addr_space = 0 : i32} : !llvm.array<2 x i8>
  llvm.mlir.global internal constant @str_1("oom\n\00") {addr_space = 0 : i32} : !llvm.array<5 x i8>
  llvm.mlir.global internal constant @str_2("data/p008.txt\00") {addr_space = 0 : i32} : !llvm.array<14 x i8>
  llvm.mlir.global internal constant @str_3("failed to read data/p008.txt\n\00") {addr_space = 0 : i32} : !llvm.array<30 x i8>
  llvm.mlir.global internal constant @str_4("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @fopen(!llvm.ptr, !llvm.ptr) -> !llvm.ptr
  func.func private @fgetc(!llvm.ptr) -> i32
  func.func private @fclose(!llvm.ptr) -> i32
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @load_digits(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i32) -> i32 {
    %1 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %0 = func.call @fopen(%arg0, %1) : (!llvm.ptr, !llvm.ptr) -> !llvm.ptr
    %2 = llvm.mlir.zero : !llvm.ptr
    %3 = llvm.icmp "eq" %0, %2 : !llvm.ptr
    cf.cond_br %3, ^bb0, ^bb1
    ^bb0:
      %4 = arith.constant 1 : i32
      %6 = arith.constant 0 : i32
      %5 = arith.subi %6, %4 : i32
      func.return %5 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %7 = arith.constant 0 : i32
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i32 : (i64) -> !llvm.ptr
    llvm.store %7, %9 : i32, !llvm.ptr
    %10 = func.call @fgetc(%0) : (!llvm.ptr) -> i32
    %11 = llvm.mlir.constant(1 : i64) : i64
    %12 = llvm.alloca %11 x i32 : (i64) -> !llvm.ptr
    llvm.store %10, %12 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %13 = llvm.load %12 : !llvm.ptr -> i32
    %14 = arith.constant 0 : i32
    %15 = arith.cmpi sge, %13, %14 : i32
    %16 = scf.if %15 -> (i1) {
      %17 = llvm.load %9 : !llvm.ptr -> i32
      %18 = arith.cmpi slt, %17, %arg2 : i32
      scf.yield %18 : i1
    } else {
      %19 = arith.constant false
      scf.yield %19 : i1
    }
    cf.cond_br %16, ^bb4, ^bb5
    ^bb4:
      %20 = llvm.load %12 : !llvm.ptr -> i32
      %21 = arith.constant 48 : i32
      %22 = arith.cmpi sge, %20, %21 : i32
      %23 = scf.if %22 -> (i1) {
        %24 = llvm.load %12 : !llvm.ptr -> i32
        %25 = arith.constant 57 : i32
        %26 = arith.cmpi sle, %24, %25 : i32
        scf.yield %26 : i1
      } else {
        %27 = arith.constant false
        scf.yield %27 : i1
      }
      cf.cond_br %23, ^bb6, ^bb7
      ^bb6:
        %28 = llvm.load %12 : !llvm.ptr -> i32
        %29 = arith.constant 48 : i32
        %30 = arith.subi %28, %29 : i32
        %31 = llvm.load %9 : !llvm.ptr -> i32
        %32 = arith.extsi %31 : i32 to i64
        %33 = llvm.getelementptr %arg1[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %30, %33 : i32, !llvm.ptr
        %34 = llvm.load %9 : !llvm.ptr -> i32
        %35 = arith.constant 1 : i32
        %36 = arith.addi %34, %35 : i32
        llvm.store %36, %9 : i32, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %37 = func.call @fgetc(%0) : (!llvm.ptr) -> i32
      llvm.store %37, %12 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %38 = func.call @fclose(%0) : (!llvm.ptr) -> i32
    %39 = llvm.load %9 : !llvm.ptr -> i32
    func.return %39 : i32
  }
  func.func @solve(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32) -> i64 {
    %40 = arith.constant 0 : i32
    %41 = arith.extsi %40 : i32 to i64
    %42 = llvm.mlir.constant(1 : i64) : i64
    %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
    llvm.store %41, %43 : i64, !llvm.ptr
    %44 = arith.constant 0 : i32
    %45 = arith.subi %arg1, %arg2 : i32
    %46 = arith.constant 1 : i32
    %47 = arith.addi %45, %46 : i32
    %48 = arith.index_cast %44 : i32 to index
    %49 = arith.index_cast %47 : i32 to index
    %51 = arith.constant 1 : index
    %52 = arith.constant -1 : index
    %53 = arith.cmpi sle, %48, %49 : index
    %50 = arith.select %53, %51, %52 : index
    cf.br ^bb9(%48 : index)
    ^bb9(%54: index):
    %55 = arith.cmpi slt, %54, %49 : index
    %56 = arith.cmpi sgt, %54, %49 : index
    %57 = arith.select %53, %55, %56 : i1
    cf.cond_br %57, ^bb10(%54 : index), ^bb11(%54 : index)
    ^bb10(%58: index):
      %59 = arith.constant 1 : 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
      %63 = arith.constant 0 : i32
      %64 = arith.index_cast %63 : i32 to index
      %65 = arith.index_cast %arg2 : i32 to index
      %67 = arith.constant 1 : index
      %68 = arith.constant -1 : index
      %69 = arith.cmpi sle, %64, %65 : index
      %66 = arith.select %69, %67, %68 : index
      cf.br ^bb12(%64 : index)
      ^bb12(%70: index):
      %71 = arith.cmpi slt, %70, %65 : index
      %72 = arith.cmpi sgt, %70, %65 : index
      %73 = arith.select %69, %71, %72 : i1
      cf.cond_br %73, ^bb13(%70 : index), ^bb14(%70 : index)
      ^bb13(%74: index):
        %75 = llvm.load %62 : !llvm.ptr -> i64
        %77 = arith.addi %58, %74 : index
        %78 = arith.index_cast %77 : index to i64
        %79 = llvm.getelementptr %arg0[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %76 = llvm.load %79 : !llvm.ptr -> i32
        %80 = arith.extsi %76 : i32 to i64
        %81 = arith.muli %75, %80 : i64
        llvm.store %81, %62 : i64, !llvm.ptr
        %82 = arith.addi %74, %66 : index
        cf.br ^bb12(%82 : index)
      ^bb14(%83: index):
      %84 = llvm.load %62 : !llvm.ptr -> i64
      %85 = llvm.load %43 : !llvm.ptr -> i64
      %86 = arith.cmpi sgt, %84, %85 : i64
      cf.cond_br %86, ^bb15, ^bb16
      ^bb15:
        %87 = llvm.load %62 : !llvm.ptr -> i64
        llvm.store %87, %43 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %88 = arith.addi %58, %50 : index
      cf.br ^bb9(%88 : index)
    ^bb11(%89: index):
    %90 = llvm.load %43 : !llvm.ptr -> i64
    func.return %90 : i64
  }
  func.func @main() -> i32 {
    %92 = arith.constant 1000 : i32
    %93 = arith.constant 4 : i32
    %94 = arith.extsi %92 : i32 to i64
    %95 = arith.extsi %93 : i32 to i64
    %91 = func.call @calloc(%94, %95) : (i64, i64) -> !llvm.ptr
    %96 = llvm.mlir.zero : !llvm.ptr
    %97 = llvm.icmp "eq" %91, %96 : !llvm.ptr
    cf.cond_br %97, ^bb18, ^bb19
    ^bb18:
      %98 = llvm.mlir.addressof @str_1 : !llvm.ptr
      %99 = llvm.call @printf(%98) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      %100 = arith.constant 1 : i32
      func.return %100 : i32
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %102 = llvm.mlir.addressof @str_2 : !llvm.ptr
    %103 = arith.constant 1000 : i32
    %101 = func.call @load_digits(%102, %91, %103) : (!llvm.ptr, !llvm.ptr, i32) -> i32
    %104 = arith.constant 0 : i32
    %105 = arith.cmpi slt, %101, %104 : i32
    cf.cond_br %105, ^bb21, ^bb22
    ^bb21:
      %106 = llvm.mlir.addressof @str_3 : !llvm.ptr
      %107 = llvm.call @printf(%106) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      func.call @free(%91) : (!llvm.ptr) -> ()
      %109 = arith.constant 1 : i32
      func.return %109 : i32
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %110 = llvm.mlir.addressof @str_4 : !llvm.ptr
    %112 = arith.constant 13 : i32
    %111 = func.call @solve(%91, %101, %112) : (!llvm.ptr, i32, i32) -> i64
    %113 = llvm.call @printf(%110, %111) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%91) : (!llvm.ptr) -> ()
    %115 = arith.constant 0 : i32
    func.return %115 : i32
  }
}