Problem 013

First ten digits of the sum of one hundred 50-digit numbers. Digit-array addition — expressive and still native-speed.

Answer5537376230
Output5537376230
StatusPASS
Native helperno
Runtime0 ms
Peak memory1136 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 solutionBig-integer addition
VerdictSuboptimal

Flow source

# Project Euler 013
# First ten digits of the sum of one hundred 50-digit numbers.
# Digit-array addition — expressive and still native-speed.

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 main() -> i32 {
    # sum digits, least-significant first; allow carry room
    let sum: ptr<i32> = calloc(60, 4)
    if sum == null {
        return 1
    }

    let f: ptr<void> = fopen("data/p013.txt", "r")
    if f == null {
        printf("failed to read data/p013.txt\n")
        free(sum)
        return 1
    }

    let mut line: ptr<i32> = calloc(50, 4)
    let mut c: i32 = fgetc(f)
    while c >= 0 {
        let mut len: i32 = 0
        while c >= 0 && c != 10 {
            if c >= 48 && c <= 57 {
                line[len] = c - 48
                len = len + 1
            }
            c = fgetc(f)
        }
        if len == 50 {
            let mut i: i32 = 0
            while i < 50 {
                sum[i] = sum[i] + line[49 - i]
                i = i + 1
            }
        }
        if c == 10 {
            c = fgetc(f)
        }
    }
    fclose(f)
    free(line)

    # propagate carry
    let mut i: i32 = 0
    while i < 59 {
        sum[i + 1] = sum[i + 1] + sum[i] / 10
        sum[i] = sum[i] % 10
        i = i + 1
    }

    # find most-significant non-zero digit
    let mut top: i32 = 59
    while top > 0 && sum[top] == 0 {
        top = top - 1
    }

    let mut result: i64 = 0
    let mut k: i32 = 0
    while k < 10 {
        result = result * 10 + (sum[top - k] as i64)
        k = k + 1
    }
    printf("%lld\n", result)
    free(sum)
    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* sum = (int32_t*)(calloc(60, 4));
    if (sum == NULL) {
        return 1;
    }
    void* f = (void*)(fopen("data/p013.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p013.txt\n");
        free(sum);
        return 1;
    }
    int32_t* line = (int32_t*)(calloc(50, 4));
    int32_t c = fgetc(f);
    while (c >= 0) {
        int32_t len = 0;
        while ((c >= 0 && c != 10)) {
            if ((c >= 48 && c <= 57)) {
                line[len] = (c - 48);
                len = (len + 1);
            }
            c = fgetc(f);
        }
        if (len == 50) {
            int32_t i = 0;
            while (i < 50) {
                sum[i] = (sum[i] + line[(49 - i)]);
                i = (i + 1);
            }
        }
        if (c == 10) {
            c = fgetc(f);
        }
    }
    fclose(f);
    free(line);
    int32_t i = 0;
    while (i < 59) {
        sum[(i + 1)] = (sum[(i + 1)] + FLOW_CHECKED_DIV((sum[i]), (10)));
        sum[i] = FLOW_CHECKED_MOD((sum[i]), (10));
        i = (i + 1);
    }
    int32_t top = 59;
    while ((top > 0 && sum[top] == 0)) {
        top = (top - 1);
    }
    int64_t result = 0;
    int32_t k = 0;
    while (k < 10) {
        result = ((result * 10) + ((int64_t)(sum[(top - k)])));
        k = (k + 1);
    }
    printf("%lld\n", result);
    free(sum);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("data/p013.txt\00") {addr_space = 0 : i32} : !llvm.array<14 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("failed to read data/p013.txt\n\00") {addr_space = 0 : i32} : !llvm.array<30 x i8>
  llvm.mlir.global internal constant @str_3("%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 @main() -> i32 {
    %1 = arith.constant 60 : i32
    %2 = arith.constant 4 : i32
    %3 = arith.extsi %1 : i32 to i64
    %4 = arith.extsi %2 : i32 to i64
    %0 = func.call @calloc(%3, %4) : (i64, i64) -> !llvm.ptr
    %5 = llvm.mlir.zero : !llvm.ptr
    %6 = llvm.icmp "eq" %0, %5 : !llvm.ptr
    cf.cond_br %6, ^bb0, ^bb1
    ^bb0:
      %7 = arith.constant 1 : i32
      func.return %7 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %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 = llvm.mlir.zero : !llvm.ptr
    %12 = llvm.icmp "eq" %8, %11 : !llvm.ptr
    cf.cond_br %12, ^bb3, ^bb4
    ^bb3:
      %13 = llvm.mlir.addressof @str_2 : !llvm.ptr
      %14 = llvm.call @printf(%13) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      func.call @free(%0) : (!llvm.ptr) -> ()
      %16 = arith.constant 1 : i32
      func.return %16 : i32
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %18 = arith.constant 50 : i32
    %19 = arith.constant 4 : i32
    %20 = arith.extsi %18 : i32 to i64
    %21 = arith.extsi %19 : i32 to i64
    %17 = func.call @calloc(%20, %21) : (i64, i64) -> !llvm.ptr
    %22 = llvm.mlir.constant(1 : i64) : i64
    %23 = llvm.alloca %22 x !llvm.ptr : (i64) -> !llvm.ptr
    llvm.store %17, %23 : !llvm.ptr, !llvm.ptr
    %24 = func.call @fgetc(%8) : (!llvm.ptr) -> i32
    %25 = llvm.mlir.constant(1 : i64) : i64
    %26 = llvm.alloca %25 x i32 : (i64) -> !llvm.ptr
    llvm.store %24, %26 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %27 = llvm.load %26 : !llvm.ptr -> i32
    %28 = arith.constant 0 : i32
    %29 = arith.cmpi sge, %27, %28 : i32
    cf.cond_br %29, ^bb7, ^bb8
    ^bb7:
      %30 = arith.constant 0 : i32
      %31 = llvm.mlir.constant(1 : i64) : i64
      %32 = llvm.alloca %31 x i32 : (i64) -> !llvm.ptr
      llvm.store %30, %32 : i32, !llvm.ptr
      cf.br ^bb9
      ^bb9:
      %33 = llvm.load %26 : !llvm.ptr -> i32
      %34 = arith.constant 0 : i32
      %35 = arith.cmpi sge, %33, %34 : i32
      %36 = scf.if %35 -> (i1) {
        %37 = llvm.load %26 : !llvm.ptr -> i32
        %38 = arith.constant 10 : i32
        %39 = arith.cmpi ne, %37, %38 : i32
        scf.yield %39 : i1
      } else {
        %40 = arith.constant false
        scf.yield %40 : i1
      }
      cf.cond_br %36, ^bb10, ^bb11
      ^bb10:
        %41 = llvm.load %26 : !llvm.ptr -> i32
        %42 = arith.constant 48 : i32
        %43 = arith.cmpi sge, %41, %42 : i32
        %44 = scf.if %43 -> (i1) {
          %45 = llvm.load %26 : !llvm.ptr -> i32
          %46 = arith.constant 57 : i32
          %47 = arith.cmpi sle, %45, %46 : i32
          scf.yield %47 : i1
        } else {
          %48 = arith.constant false
          scf.yield %48 : i1
        }
        cf.cond_br %44, ^bb12, ^bb13
        ^bb12:
          %49 = llvm.load %26 : !llvm.ptr -> i32
          %50 = arith.constant 48 : i32
          %51 = arith.subi %49, %50 : i32
          %52 = llvm.load %23 : !llvm.ptr -> !llvm.ptr
          %53 = llvm.load %32 : !llvm.ptr -> i32
          %54 = arith.extsi %53 : i32 to i64
          %55 = llvm.getelementptr %52[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %51, %55 : i32, !llvm.ptr
          %56 = llvm.load %32 : !llvm.ptr -> i32
          %57 = arith.constant 1 : i32
          %58 = arith.addi %56, %57 : i32
          llvm.store %58, %32 : i32, !llvm.ptr
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        %59 = func.call @fgetc(%8) : (!llvm.ptr) -> i32
        llvm.store %59, %26 : i32, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %60 = llvm.load %32 : !llvm.ptr -> i32
      %61 = arith.constant 50 : i32
      %62 = arith.cmpi eq, %60, %61 : i32
      cf.cond_br %62, ^bb15, ^bb16
      ^bb15:
        %63 = arith.constant 0 : i32
        %64 = llvm.mlir.constant(1 : i64) : i64
        %65 = llvm.alloca %64 x i32 : (i64) -> !llvm.ptr
        llvm.store %63, %65 : i32, !llvm.ptr
        cf.br ^bb18
        ^bb18:
        %66 = llvm.load %65 : !llvm.ptr -> i32
        %67 = arith.constant 50 : i32
        %68 = arith.cmpi slt, %66, %67 : i32
        cf.cond_br %68, ^bb19, ^bb20
        ^bb19:
          %70 = llvm.load %65 : !llvm.ptr -> i32
          %71 = arith.extsi %70 : i32 to i64
          %72 = llvm.getelementptr %0[%71] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %69 = llvm.load %72 : !llvm.ptr -> i32
          %74 = llvm.load %23 : !llvm.ptr -> !llvm.ptr
          %75 = arith.constant 49 : i32
          %76 = llvm.load %65 : !llvm.ptr -> i32
          %77 = arith.subi %75, %76 : i32
          %78 = arith.extsi %77 : i32 to i64
          %79 = llvm.getelementptr %74[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %73 = llvm.load %79 : !llvm.ptr -> i32
          %80 = arith.addi %69, %73 : i32
          %81 = llvm.load %65 : !llvm.ptr -> i32
          %82 = arith.extsi %81 : i32 to i64
          %83 = llvm.getelementptr %0[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %80, %83 : i32, !llvm.ptr
          %84 = llvm.load %65 : !llvm.ptr -> i32
          %85 = arith.constant 1 : i32
          %86 = arith.addi %84, %85 : i32
          llvm.store %86, %65 : i32, !llvm.ptr
          cf.br ^bb18
        ^bb20:
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %87 = llvm.load %26 : !llvm.ptr -> i32
      %88 = arith.constant 10 : i32
      %89 = arith.cmpi eq, %87, %88 : i32
      cf.cond_br %89, ^bb21, ^bb22
      ^bb21:
        %90 = func.call @fgetc(%8) : (!llvm.ptr) -> i32
        llvm.store %90, %26 : i32, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      cf.br ^bb6
    ^bb8:
    %91 = func.call @fclose(%8) : (!llvm.ptr) -> i32
    %93 = llvm.load %23 : !llvm.ptr -> !llvm.ptr
    func.call @free(%93) : (!llvm.ptr) -> ()
    %94 = arith.constant 0 : i32
    %95 = llvm.mlir.constant(1 : i64) : i64
    %96 = llvm.alloca %95 x i32 : (i64) -> !llvm.ptr
    llvm.store %94, %96 : i32, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %97 = llvm.load %96 : !llvm.ptr -> i32
    %98 = arith.constant 59 : i32
    %99 = arith.cmpi slt, %97, %98 : i32
    cf.cond_br %99, ^bb25, ^bb26
    ^bb25:
      %101 = llvm.load %96 : !llvm.ptr -> i32
      %102 = arith.constant 1 : i32
      %103 = arith.addi %101, %102 : i32
      %104 = arith.extsi %103 : i32 to i64
      %105 = llvm.getelementptr %0[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %100 = llvm.load %105 : !llvm.ptr -> i32
      %107 = llvm.load %96 : !llvm.ptr -> i32
      %108 = arith.extsi %107 : i32 to i64
      %109 = llvm.getelementptr %0[%108] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %106 = llvm.load %109 : !llvm.ptr -> i32
      %110 = arith.constant 10 : i32
      %111 = arith.divsi %106, %110 : i32
      %112 = arith.addi %100, %111 : i32
      %113 = llvm.load %96 : !llvm.ptr -> i32
      %114 = arith.constant 1 : i32
      %115 = arith.addi %113, %114 : i32
      %116 = arith.extsi %115 : i32 to i64
      %117 = llvm.getelementptr %0[%116] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %112, %117 : i32, !llvm.ptr
      %119 = llvm.load %96 : !llvm.ptr -> i32
      %120 = arith.extsi %119 : i32 to i64
      %121 = llvm.getelementptr %0[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %118 = llvm.load %121 : !llvm.ptr -> i32
      %122 = arith.constant 10 : i32
      %123 = arith.remsi %118, %122 : i32
      %124 = llvm.load %96 : !llvm.ptr -> i32
      %125 = arith.extsi %124 : i32 to i64
      %126 = llvm.getelementptr %0[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %123, %126 : i32, !llvm.ptr
      %127 = llvm.load %96 : !llvm.ptr -> i32
      %128 = arith.constant 1 : i32
      %129 = arith.addi %127, %128 : i32
      llvm.store %129, %96 : i32, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %130 = arith.constant 59 : i32
    %131 = llvm.mlir.constant(1 : i64) : i64
    %132 = llvm.alloca %131 x i32 : (i64) -> !llvm.ptr
    llvm.store %130, %132 : i32, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %133 = llvm.load %132 : !llvm.ptr -> i32
    %134 = arith.constant 0 : i32
    %135 = arith.cmpi sgt, %133, %134 : i32
    %136 = scf.if %135 -> (i1) {
      %138 = llvm.load %132 : !llvm.ptr -> i32
      %139 = arith.extsi %138 : i32 to i64
      %140 = llvm.getelementptr %0[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %137 = llvm.load %140 : !llvm.ptr -> i32
      %141 = arith.constant 0 : i32
      %142 = arith.cmpi eq, %137, %141 : i32
      scf.yield %142 : i1
    } else {
      %143 = arith.constant false
      scf.yield %143 : i1
    }
    cf.cond_br %136, ^bb28, ^bb29
    ^bb28:
      %144 = llvm.load %132 : !llvm.ptr -> i32
      %145 = arith.constant 1 : i32
      %146 = arith.subi %144, %145 : i32
      llvm.store %146, %132 : i32, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %147 = arith.constant 0 : i32
    %148 = arith.extsi %147 : i32 to i64
    %149 = llvm.mlir.constant(1 : i64) : i64
    %150 = llvm.alloca %149 x i64 : (i64) -> !llvm.ptr
    llvm.store %148, %150 : i64, !llvm.ptr
    %151 = arith.constant 0 : i32
    %152 = llvm.mlir.constant(1 : i64) : i64
    %153 = llvm.alloca %152 x i32 : (i64) -> !llvm.ptr
    llvm.store %151, %153 : i32, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %154 = llvm.load %153 : !llvm.ptr -> i32
    %155 = arith.constant 10 : i32
    %156 = arith.cmpi slt, %154, %155 : i32
    cf.cond_br %156, ^bb31, ^bb32
    ^bb31:
      %157 = llvm.load %150 : !llvm.ptr -> i64
      %158 = arith.constant 10 : i32
      %160 = arith.extsi %158 : i32 to i64
      %159 = arith.muli %157, %160 : i64
      %162 = llvm.load %132 : !llvm.ptr -> i32
      %163 = llvm.load %153 : !llvm.ptr -> i32
      %164 = arith.subi %162, %163 : i32
      %165 = arith.extsi %164 : i32 to i64
      %166 = llvm.getelementptr %0[%165] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %161 = llvm.load %166 : !llvm.ptr -> i32
      %167 = arith.extsi %161 : i32 to i64
      %168 = arith.addi %159, %167 : i64
      llvm.store %168, %150 : i64, !llvm.ptr
      %169 = llvm.load %153 : !llvm.ptr -> i32
      %170 = arith.constant 1 : i32
      %171 = arith.addi %169, %170 : i32
      llvm.store %171, %153 : i32, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %172 = llvm.mlir.addressof @str_3 : !llvm.ptr
    %173 = llvm.load %150 : !llvm.ptr -> i64
    %174 = llvm.call @printf(%172, %173) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%0) : (!llvm.ptr) -> ()
    %176 = arith.constant 0 : i32
    func.return %176 : i32
  }
}