Problem 159

Sum of maximum digital root sums of factorisations for n = 2..999999.

Answer14489159
Output14489159
StatusPASS
Native helperno
Runtime10 ms
Peak memory5056 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(sqrt(n))
Space complexityO(n)O(1)
ApproachFlow solutionTrial division or Pollard rho
VerdictSuboptimal

Flow source

# Project Euler 159
# Sum of maximum digital root sums of factorisations for n = 2..999999.

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

function digit_root(x0: i64) -> i32 {
    let mut x: i64 = x0
    while x >= 10 {
        let mut s: i64 = 0
        while x > 0 {
            s = s + (x % 10)
            x = x / 10
        }
        x = s
    }
    return x as i32
}

function main() -> i32 {
    let limit: i64 = 999999
    let mdrs: ptr<i32> = calloc(limit + 1, 4)
    if mdrs == null { return 1 }

    let mut i: i64 = 2
    while i <= limit {
        mdrs[i] = digit_root(i)
        i = i + 1
    }

    let mut a: i64 = 2
    while a <= limit {
        let mut b: i64 = 2
        while a * b <= limit && b <= a {
            let prod: i64 = a * b
            let cand: i32 = mdrs[a] + mdrs[b]
            if mdrs[prod] < cand {
                mdrs[prod] = cand
            }
            b = b + 1
        }
        a = a + 1
    }

    let mut total: i64 = 0
    i = 2
    while i <= limit {
        total = total + (mdrs[i] as i64)
        i = i + 1
    }
    printf("%lld\n", total)
    free(mdrs)
    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 digit_root_i64(int64_t x0);
int32_t main(void);



int32_t digit_root_i64(int64_t x0) {
    int64_t x = x0;
    while (x >= 10) {
        int64_t s = 0;
        while (x > 0) {
            s = (s + FLOW_CHECKED_MOD((x), (10)));
            x = FLOW_CHECKED_DIV((x), (10));
        }
        x = s;
    }
    return ((int32_t)(x));
}

int32_t main(void) {
    int64_t limit = 999999;
    int32_t* mdrs = (int32_t*)(calloc((limit + 1), 4));
    if (mdrs == NULL) {
        return 1;
    }
    int64_t i = 2;
    while (i <= limit) {
        mdrs[i] = digit_root_i64(i);
        i = (i + 1);
    }
    int64_t a = 2;
    while (a <= limit) {
        int64_t b = 2;
        while (((a * b) <= limit && b <= a)) {
            int64_t prod = (a * b);
            int32_t cand = (mdrs[a] + mdrs[b]);
            if (mdrs[prod] < cand) {
                mdrs[prod] = cand;
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    int64_t total = 0;
    i = 2;
    while (i <= limit) {
        total = (total + ((int64_t)(mdrs[i])));
        i = (i + 1);
    }
    printf("%lld\n", total);
    free(mdrs);
    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 @digit_root(%arg0: i64) -> i32 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %2 = llvm.load %1 : !llvm.ptr -> i64
    %3 = arith.constant 10 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.cmpi sge, %2, %5 : i64
    cf.cond_br %4, ^bb1, ^bb2
    ^bb1:
      %6 = arith.constant 0 : i32
      %7 = arith.extsi %6 : i32 to i64
      %8 = llvm.mlir.constant(1 : i64) : i64
      %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
      llvm.store %7, %9 : i64, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %10 = llvm.load %1 : !llvm.ptr -> i64
      %11 = arith.constant 0 : i32
      %13 = arith.extsi %11 : i32 to i64
      %12 = arith.cmpi sgt, %10, %13 : i64
      cf.cond_br %12, ^bb4, ^bb5
      ^bb4:
        %14 = llvm.load %9 : !llvm.ptr -> i64
        %15 = llvm.load %1 : !llvm.ptr -> i64
        %16 = arith.constant 10 : i32
        %18 = arith.extsi %16 : i32 to i64
        %17 = arith.remsi %15, %18 : i64
        %19 = arith.addi %14, %17 : i64
        llvm.store %19, %9 : i64, !llvm.ptr
        %20 = llvm.load %1 : !llvm.ptr -> i64
        %21 = arith.constant 10 : i32
        %23 = arith.extsi %21 : i32 to i64
        %22 = arith.divsi %20, %23 : i64
        llvm.store %22, %1 : i64, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %24 = llvm.load %9 : !llvm.ptr -> i64
      llvm.store %24, %1 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %25 = llvm.load %1 : !llvm.ptr -> i64
    %26 = arith.trunci %25 : i64 to i32
    func.return %26 : i32
  }
  func.func @main() -> i32 {
    %27 = arith.constant 999999 : i32
    %28 = arith.extsi %27 : i32 to i64
    %30 = arith.constant 1 : i32
    %32 = arith.extsi %30 : i32 to i64
    %31 = arith.addi %28, %32 : i64
    %33 = arith.constant 4 : i32
    %34 = arith.extsi %33 : i32 to i64
    %29 = func.call @calloc(%31, %34) : (i64, i64) -> !llvm.ptr
    %35 = llvm.mlir.zero : !llvm.ptr
    %36 = llvm.icmp "eq" %29, %35 : !llvm.ptr
    cf.cond_br %36, ^bb6, ^bb7
    ^bb6:
      %37 = arith.constant 1 : i32
      func.return %37 : i32
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %38 = arith.constant 2 : i32
    %39 = arith.extsi %38 : i32 to i64
    %40 = llvm.mlir.constant(1 : i64) : i64
    %41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
    llvm.store %39, %41 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %42 = llvm.load %41 : !llvm.ptr -> i64
    %43 = arith.cmpi sle, %42, %28 : i64
    cf.cond_br %43, ^bb10, ^bb11
    ^bb10:
      %45 = llvm.load %41 : !llvm.ptr -> i64
      %44 = func.call @digit_root(%45) : (i64) -> i32
      %46 = llvm.load %41 : !llvm.ptr -> i64
      %47 = llvm.getelementptr %29[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %44, %47 : i32, !llvm.ptr
      %48 = llvm.load %41 : !llvm.ptr -> i64
      %49 = arith.constant 1 : i32
      %51 = arith.extsi %49 : i32 to i64
      %50 = arith.addi %48, %51 : i64
      llvm.store %50, %41 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %52 = arith.constant 2 : i32
    %53 = arith.extsi %52 : i32 to i64
    %54 = llvm.mlir.constant(1 : i64) : i64
    %55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
    llvm.store %53, %55 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %56 = llvm.load %55 : !llvm.ptr -> i64
    %57 = arith.cmpi sle, %56, %28 : i64
    cf.cond_br %57, ^bb13, ^bb14
    ^bb13:
      %58 = arith.constant 2 : i32
      %59 = arith.extsi %58 : i32 to i64
      %60 = llvm.mlir.constant(1 : i64) : i64
      %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
      llvm.store %59, %61 : i64, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %62 = llvm.load %55 : !llvm.ptr -> i64
      %63 = llvm.load %61 : !llvm.ptr -> i64
      %64 = arith.muli %62, %63 : i64
      %65 = arith.cmpi sle, %64, %28 : i64
      %66 = scf.if %65 -> (i1) {
        %67 = llvm.load %61 : !llvm.ptr -> i64
        %68 = llvm.load %55 : !llvm.ptr -> i64
        %69 = arith.cmpi sle, %67, %68 : i64
        scf.yield %69 : i1
      } else {
        %70 = arith.constant false
        scf.yield %70 : i1
      }
      cf.cond_br %66, ^bb16, ^bb17
      ^bb16:
        %71 = llvm.load %55 : !llvm.ptr -> i64
        %72 = llvm.load %61 : !llvm.ptr -> i64
        %73 = arith.muli %71, %72 : i64
        %75 = llvm.load %55 : !llvm.ptr -> i64
        %76 = llvm.getelementptr %29[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %74 = llvm.load %76 : !llvm.ptr -> i32
        %78 = llvm.load %61 : !llvm.ptr -> i64
        %79 = llvm.getelementptr %29[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %77 = llvm.load %79 : !llvm.ptr -> i32
        %80 = arith.addi %74, %77 : i32
        %82 = llvm.getelementptr %29[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %81 = llvm.load %82 : !llvm.ptr -> i32
        %83 = arith.cmpi slt, %81, %80 : i32
        cf.cond_br %83, ^bb18, ^bb19
        ^bb18:
          %84 = llvm.getelementptr %29[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %80, %84 : i32, !llvm.ptr
          cf.br ^bb20
        ^bb19:
          cf.br ^bb20
        ^bb20:
        %85 = llvm.load %61 : !llvm.ptr -> i64
        %86 = arith.constant 1 : i32
        %88 = arith.extsi %86 : i32 to i64
        %87 = arith.addi %85, %88 : i64
        llvm.store %87, %61 : i64, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %89 = llvm.load %55 : !llvm.ptr -> i64
      %90 = arith.constant 1 : i32
      %92 = arith.extsi %90 : i32 to i64
      %91 = arith.addi %89, %92 : i64
      llvm.store %91, %55 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %93 = arith.constant 0 : i32
    %94 = arith.extsi %93 : i32 to i64
    %95 = llvm.mlir.constant(1 : i64) : i64
    %96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
    llvm.store %94, %96 : i64, !llvm.ptr
    %97 = arith.constant 2 : i32
    %98 = arith.extsi %97 : i32 to i64
    llvm.store %98, %41 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %99 = llvm.load %41 : !llvm.ptr -> i64
    %100 = arith.cmpi sle, %99, %28 : i64
    cf.cond_br %100, ^bb22, ^bb23
    ^bb22:
      %101 = llvm.load %96 : !llvm.ptr -> i64
      %103 = llvm.load %41 : !llvm.ptr -> i64
      %104 = llvm.getelementptr %29[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %102 = llvm.load %104 : !llvm.ptr -> i32
      %105 = arith.extsi %102 : i32 to i64
      %106 = arith.addi %101, %105 : i64
      llvm.store %106, %96 : i64, !llvm.ptr
      %107 = llvm.load %41 : !llvm.ptr -> i64
      %108 = arith.constant 1 : i32
      %110 = arith.extsi %108 : i32 to i64
      %109 = arith.addi %107, %110 : i64
      llvm.store %109, %41 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %111 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %112 = llvm.load %96 : !llvm.ptr -> i64
    %113 = llvm.call @printf(%111, %112) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%29) : (!llvm.ptr) -> ()
    %115 = arith.constant 0 : i32
    func.return %115 : i32
  }
}