Problem 095

Smallest member of the longest amicable chain with no element exceeding 1e6.

Answer14316
Output14316
StatusPASS
Native helperno
Runtime10 ms
Peak memory9008 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve-based divisor sums
VerdictSuboptimal

Flow source

# Project Euler 095
# Smallest member of the longest amicable chain with no element exceeding 1e6.

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

function main() -> i32 {
    let limit: i32 = 1000000
    let sigma: ptr<i32> = calloc((limit + 1) as i64, 4)
    if sigma == null { return 1 }
    let mut i: i32 = 1
    while i <= limit {
        let mut j: i32 = 2 * i
        while j <= limit {
            sigma[j] = sigma[j] + i
            j = j + i
        }
        i = i + 1
    }

    let seen: ptr<i32> = calloc((limit + 1) as i64, 4)
    if seen == null { return 1 }
    # seen[x] = visit token; 0 unused
    let mut best_len: i32 = 0
    let mut best_min: i32 = 0
    let mut token: i32 = 1
    let mut start: i32 = 1
    while start <= limit {
        if seen[start] == 0 {
            let mut x: i32 = start
            let mut step: i32 = 0
            # walk until repeat or out of range
            while x > 0 && x <= limit && seen[x] == 0 {
                seen[x] = token
                x = sigma[x]
                step = step + 1
                if step > 1000 { break }
            }
            if x > 0 && x <= limit && seen[x] == token {
                # found a cycle in this chain; measure it
                let mut y: i32 = x
                let mut clen: i32 = 0
                let mut cmin: i32 = x
                while true {
                    if y < cmin { cmin = y }
                    y = sigma[y]
                    clen = clen + 1
                    if y == x { break }
                }
                if clen > best_len {
                    best_len = clen
                    best_min = cmin
                }
            }
            token = token + 1
        }
        start = start + 1
    }
    printf("%lld\n", best_min as i64)
    free(sigma)
    free(seen)
    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 limit = 1000000;
    int32_t* sigma = (int32_t*)(calloc(((int64_t)((limit + 1))), 4));
    if (sigma == NULL) {
        return 1;
    }
    int32_t i = 1;
    while (i <= limit) {
        int32_t j = (2 * i);
        while (j <= limit) {
            sigma[j] = (sigma[j] + i);
            j = (j + i);
        }
        i = (i + 1);
    }
    int32_t* seen = (int32_t*)(calloc(((int64_t)((limit + 1))), 4));
    if (seen == NULL) {
        return 1;
    }
    int32_t best_len = 0;
    int32_t best_min = 0;
    int32_t token = 1;
    int32_t start = 1;
    while (start <= limit) {
        if (seen[start] == 0) {
            int32_t x = start;
            int32_t step = 0;
            while (((x > 0 && x <= limit) && seen[x] == 0)) {
                seen[x] = token;
                x = sigma[x];
                step = (step + 1);
                if (step > 1000) {
                    break;
                }
            }
            if (((x > 0 && x <= limit) && seen[x] == token)) {
                int32_t y = x;
                int32_t clen = 0;
                int32_t cmin = x;
                while (1) {
                    if (y < cmin) {
                        cmin = y;
                    }
                    y = sigma[y];
                    clen = (clen + 1);
                    if (y == x) {
                        break;
                    }
                }
                if (clen > best_len) {
                    best_len = clen;
                    best_min = cmin;
                }
            }
            token = (token + 1);
        }
        start = (start + 1);
    }
    printf("%lld\n", ((int64_t)(best_min)));
    free(sigma);
    free(seen);
    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 1000000 : i32
    %2 = arith.constant 1 : i32
    %3 = arith.addi %0, %2 : i32
    %4 = arith.extsi %3 : i32 to i64
    %5 = arith.constant 4 : 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 = 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.cmpi sle, %13, %0 : i32
    cf.cond_br %14, ^bb4, ^bb5
    ^bb4:
      %15 = arith.constant 2 : i32
      %16 = llvm.load %12 : !llvm.ptr -> i32
      %17 = arith.muli %15, %16 : i32
      %18 = llvm.mlir.constant(1 : i64) : i64
      %19 = llvm.alloca %18 x i32 : (i64) -> !llvm.ptr
      llvm.store %17, %19 : i32, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %20 = llvm.load %19 : !llvm.ptr -> i32
      %21 = arith.cmpi sle, %20, %0 : i32
      cf.cond_br %21, ^bb7, ^bb8
      ^bb7:
        %23 = llvm.load %19 : !llvm.ptr -> i32
        %24 = arith.extsi %23 : i32 to i64
        %25 = llvm.getelementptr %1[%24] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %22 = llvm.load %25 : !llvm.ptr -> i32
        %26 = llvm.load %12 : !llvm.ptr -> i32
        %27 = arith.addi %22, %26 : i32
        %28 = llvm.load %19 : !llvm.ptr -> i32
        %29 = arith.extsi %28 : i32 to i64
        %30 = llvm.getelementptr %1[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %27, %30 : i32, !llvm.ptr
        %31 = llvm.load %19 : !llvm.ptr -> i32
        %32 = llvm.load %12 : !llvm.ptr -> i32
        %33 = arith.addi %31, %32 : i32
        llvm.store %33, %19 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %34 = llvm.load %12 : !llvm.ptr -> i32
      %35 = arith.constant 1 : i32
      %36 = arith.addi %34, %35 : i32
      llvm.store %36, %12 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %38 = arith.constant 1 : i32
    %39 = arith.addi %0, %38 : i32
    %40 = arith.extsi %39 : i32 to i64
    %41 = arith.constant 4 : i32
    %42 = arith.extsi %41 : i32 to i64
    %37 = func.call @calloc(%40, %42) : (i64, i64) -> !llvm.ptr
    %43 = llvm.mlir.zero : !llvm.ptr
    %44 = llvm.icmp "eq" %37, %43 : !llvm.ptr
    cf.cond_br %44, ^bb9, ^bb10
    ^bb9:
      %45 = arith.constant 1 : i32
      func.return %45 : i32
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %46 = arith.constant 0 : i32
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i32 : (i64) -> !llvm.ptr
    llvm.store %46, %48 : i32, !llvm.ptr
    %49 = arith.constant 0 : i32
    %50 = llvm.mlir.constant(1 : i64) : i64
    %51 = llvm.alloca %50 x i32 : (i64) -> !llvm.ptr
    llvm.store %49, %51 : i32, !llvm.ptr
    %52 = arith.constant 1 : i32
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i32 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i32, !llvm.ptr
    %55 = arith.constant 1 : i32
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i32 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %58 = llvm.load %57 : !llvm.ptr -> i32
    %59 = arith.cmpi sle, %58, %0 : i32
    cf.cond_br %59, ^bb13, ^bb14
    ^bb13:
      %61 = llvm.load %57 : !llvm.ptr -> i32
      %62 = arith.extsi %61 : i32 to i64
      %63 = llvm.getelementptr %37[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %60 = llvm.load %63 : !llvm.ptr -> i32
      %64 = arith.constant 0 : i32
      %65 = arith.cmpi eq, %60, %64 : i32
      cf.cond_br %65, ^bb15, ^bb16
      ^bb15:
        %66 = llvm.load %57 : !llvm.ptr -> i32
        %67 = llvm.mlir.constant(1 : i64) : i64
        %68 = llvm.alloca %67 x i32 : (i64) -> !llvm.ptr
        llvm.store %66, %68 : i32, !llvm.ptr
        %69 = arith.constant 0 : i32
        %70 = llvm.mlir.constant(1 : i64) : i64
        %71 = llvm.alloca %70 x i32 : (i64) -> !llvm.ptr
        llvm.store %69, %71 : i32, !llvm.ptr
        cf.br ^bb18
        ^bb18:
        %72 = llvm.load %68 : !llvm.ptr -> i32
        %73 = arith.constant 0 : i32
        %74 = arith.cmpi sgt, %72, %73 : i32
        %75 = scf.if %74 -> (i1) {
          %76 = llvm.load %68 : !llvm.ptr -> i32
          %77 = arith.cmpi sle, %76, %0 : i32
          scf.yield %77 : i1
        } else {
          %78 = arith.constant false
          scf.yield %78 : i1
        }
        %79 = scf.if %75 -> (i1) {
          %81 = llvm.load %68 : !llvm.ptr -> i32
          %82 = arith.extsi %81 : i32 to i64
          %83 = llvm.getelementptr %37[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %80 = llvm.load %83 : !llvm.ptr -> i32
          %84 = arith.constant 0 : i32
          %85 = arith.cmpi eq, %80, %84 : i32
          scf.yield %85 : i1
        } else {
          %86 = arith.constant false
          scf.yield %86 : i1
        }
        cf.cond_br %79, ^bb19, ^bb20
        ^bb19:
          %87 = llvm.load %54 : !llvm.ptr -> i32
          %88 = llvm.load %68 : !llvm.ptr -> i32
          %89 = arith.extsi %88 : i32 to i64
          %90 = llvm.getelementptr %37[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %87, %90 : i32, !llvm.ptr
          %92 = llvm.load %68 : !llvm.ptr -> i32
          %93 = arith.extsi %92 : i32 to i64
          %94 = llvm.getelementptr %1[%93] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %91 = llvm.load %94 : !llvm.ptr -> i32
          llvm.store %91, %68 : i32, !llvm.ptr
          %95 = llvm.load %71 : !llvm.ptr -> i32
          %96 = arith.constant 1 : i32
          %97 = arith.addi %95, %96 : i32
          llvm.store %97, %71 : i32, !llvm.ptr
          %98 = llvm.load %71 : !llvm.ptr -> i32
          %99 = arith.constant 1000 : i32
          %100 = arith.cmpi sgt, %98, %99 : i32
          cf.cond_br %100, ^bb21, ^bb22
          ^bb21:
            cf.br ^bb20
          ^bb22:
            cf.br ^bb23
          ^bb23:
          cf.br ^bb18
        ^bb20:
        %101 = llvm.load %68 : !llvm.ptr -> i32
        %102 = arith.constant 0 : i32
        %103 = arith.cmpi sgt, %101, %102 : i32
        %104 = scf.if %103 -> (i1) {
          %105 = llvm.load %68 : !llvm.ptr -> i32
          %106 = arith.cmpi sle, %105, %0 : i32
          scf.yield %106 : i1
        } else {
          %107 = arith.constant false
          scf.yield %107 : i1
        }
        %108 = scf.if %104 -> (i1) {
          %110 = llvm.load %68 : !llvm.ptr -> i32
          %111 = arith.extsi %110 : i32 to i64
          %112 = llvm.getelementptr %37[%111] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %109 = llvm.load %112 : !llvm.ptr -> i32
          %113 = llvm.load %54 : !llvm.ptr -> i32
          %114 = arith.cmpi eq, %109, %113 : i32
          scf.yield %114 : i1
        } else {
          %115 = arith.constant false
          scf.yield %115 : i1
        }
        cf.cond_br %108, ^bb24, ^bb25
        ^bb24:
          %116 = llvm.load %68 : !llvm.ptr -> i32
          %117 = llvm.mlir.constant(1 : i64) : i64
          %118 = llvm.alloca %117 x i32 : (i64) -> !llvm.ptr
          llvm.store %116, %118 : i32, !llvm.ptr
          %119 = arith.constant 0 : i32
          %120 = llvm.mlir.constant(1 : i64) : i64
          %121 = llvm.alloca %120 x i32 : (i64) -> !llvm.ptr
          llvm.store %119, %121 : i32, !llvm.ptr
          %122 = llvm.load %68 : !llvm.ptr -> i32
          %123 = llvm.mlir.constant(1 : i64) : i64
          %124 = llvm.alloca %123 x i32 : (i64) -> !llvm.ptr
          llvm.store %122, %124 : i32, !llvm.ptr
          cf.br ^bb27
          ^bb27:
          %125 = arith.constant 1 : i1
          cf.cond_br %125, ^bb28, ^bb29
          ^bb28:
            %126 = llvm.load %118 : !llvm.ptr -> i32
            %127 = llvm.load %124 : !llvm.ptr -> i32
            %128 = arith.cmpi slt, %126, %127 : i32
            cf.cond_br %128, ^bb30, ^bb31
            ^bb30:
              %129 = llvm.load %118 : !llvm.ptr -> i32
              llvm.store %129, %124 : i32, !llvm.ptr
              cf.br ^bb32
            ^bb31:
              cf.br ^bb32
            ^bb32:
            %131 = llvm.load %118 : !llvm.ptr -> i32
            %132 = arith.extsi %131 : i32 to i64
            %133 = llvm.getelementptr %1[%132] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %130 = llvm.load %133 : !llvm.ptr -> i32
            llvm.store %130, %118 : i32, !llvm.ptr
            %134 = llvm.load %121 : !llvm.ptr -> i32
            %135 = arith.constant 1 : i32
            %136 = arith.addi %134, %135 : i32
            llvm.store %136, %121 : i32, !llvm.ptr
            %137 = llvm.load %118 : !llvm.ptr -> i32
            %138 = llvm.load %68 : !llvm.ptr -> i32
            %139 = arith.cmpi eq, %137, %138 : i32
            cf.cond_br %139, ^bb33, ^bb34
            ^bb33:
              cf.br ^bb29
            ^bb34:
              cf.br ^bb35
            ^bb35:
            cf.br ^bb27
          ^bb29:
          %140 = llvm.load %121 : !llvm.ptr -> i32
          %141 = llvm.load %48 : !llvm.ptr -> i32
          %142 = arith.cmpi sgt, %140, %141 : i32
          cf.cond_br %142, ^bb36, ^bb37
          ^bb36:
            %143 = llvm.load %121 : !llvm.ptr -> i32
            llvm.store %143, %48 : i32, !llvm.ptr
            %144 = llvm.load %124 : !llvm.ptr -> i32
            llvm.store %144, %51 : i32, !llvm.ptr
            cf.br ^bb38
          ^bb37:
            cf.br ^bb38
          ^bb38:
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        %145 = llvm.load %54 : !llvm.ptr -> i32
        %146 = arith.constant 1 : i32
        %147 = arith.addi %145, %146 : i32
        llvm.store %147, %54 : i32, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %148 = llvm.load %57 : !llvm.ptr -> i32
      %149 = arith.constant 1 : i32
      %150 = arith.addi %148, %149 : i32
      llvm.store %150, %57 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %151 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %152 = llvm.load %51 : !llvm.ptr -> i32
    %153 = arith.extsi %152 : i32 to i64
    %154 = llvm.call @printf(%151, %153) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%1) : (!llvm.ptr) -> ()
    func.call @free(%37) : (!llvm.ptr) -> ()
    %157 = arith.constant 0 : i32
    func.return %157 : i32
  }
}