Problem 214

Sum of primes < 40e6 with totient chain length 25.

Answer1677366278943
Output1677366278943
StatusPASS
Native helperno
Runtime1040 ms
Peak memory235456 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 of Eratosthenes
VerdictSuboptimal

Flow source

# Project Euler 214
# Sum of primes < 40e6 with totient chain length 25.

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

function main() -> i32 {
    let LIMIT: i64 = 40000000
    let MAX_STEPS: i32 = 25
    let tot: ptr<i32> = calloc(LIMIT, 4)
    let chain: ptr<i16> = calloc(LIMIT, 2)
    if tot == null || chain == null { return 1 }

    let mut i: i64 = 0
    while i < LIMIT {
        tot[i] = i as i32
        i = i + 1
    }
    i = 2
    while i < LIMIT {
        if tot[i] == i as i32 {
            let mut j: i64 = i
            while j < LIMIT {
                tot[j] = tot[j] - tot[j] / (i as i32)
                j = j + i
            }
        }
        i = i + 1
    }

    chain[1] = 1
    i = 2
    while i < LIMIT {
        chain[i] = (chain[tot[i] as i64] as i32 + 1) as i16
        i = i + 1
    }

    let mut result: i64 = 0
    i = 2
    while i < LIMIT {
        if tot[i] == i as i32 - 1 {
            # prime: totient(p)=p-1
            if chain[i] as i32 == MAX_STEPS {
                result = result + i
            }
        }
        i = i + 1
    }
    # Wait: for prime p, tot[p] should be p-1 after sieve. Check: initially tot[p]=p, then tot[p]=p-p/p=p-1. Yes.
    # But condition tot[i]==i-1 identifies primes. Good.
    printf("%lld\n", result)
    free(tot); free(chain)
    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) {
    int64_t LIMIT = 40000000;
    int32_t MAX_STEPS = 25;
    int32_t* tot = (int32_t*)(calloc(LIMIT, 4));
    int16_t* chain = (int16_t*)(calloc(LIMIT, 2));
    if ((tot == NULL || chain == NULL)) {
        return 1;
    }
    int64_t i = 0;
    while (i < LIMIT) {
        tot[i] = ((int32_t)(i));
        i = (i + 1);
    }
    i = 2;
    while (i < LIMIT) {
        if (tot[i] == ((int32_t)(i))) {
            int64_t j = i;
            while (j < LIMIT) {
                tot[j] = (tot[j] - FLOW_CHECKED_DIV((tot[j]), (((int32_t)(i)))));
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    chain[1] = 1;
    i = 2;
    while (i < LIMIT) {
        chain[i] = ((int16_t)((((int32_t)(chain[((int64_t)(tot[i]))])) + 1)));
        i = (i + 1);
    }
    int64_t result = 0;
    i = 2;
    while (i < LIMIT) {
        if (tot[i] == (((int32_t)(i)) - 1)) {
            if (((int32_t)(chain[i])) == MAX_STEPS) {
                result = (result + i);
            }
        }
        i = (i + 1);
    }
    printf("%lld\n", result);
    free(tot);
    free(chain);
    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 40000000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 25 : i32
    %4 = arith.constant 4 : i32
    %5 = arith.extsi %4 : i32 to i64
    %3 = func.call @calloc(%1, %5) : (i64, i64) -> !llvm.ptr
    %7 = arith.constant 2 : i32
    %8 = arith.extsi %7 : i32 to i64
    %6 = func.call @calloc(%1, %8) : (i64, i64) -> !llvm.ptr
    %9 = llvm.mlir.zero : !llvm.ptr
    %10 = llvm.icmp "eq" %3, %9 : !llvm.ptr
    %11 = scf.if %10 -> (i1) {
      %12 = arith.constant true
      scf.yield %12 : i1
    } else {
      %13 = llvm.mlir.zero : !llvm.ptr
      %14 = llvm.icmp "eq" %6, %13 : !llvm.ptr
      scf.yield %14 : i1
    }
    cf.cond_br %11, ^bb0, ^bb1
    ^bb0:
      %15 = arith.constant 1 : i32
      func.return %15 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %16 = arith.constant 0 : i32
    %17 = arith.extsi %16 : i32 to i64
    %18 = llvm.mlir.constant(1 : i64) : i64
    %19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
    llvm.store %17, %19 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %20 = llvm.load %19 : !llvm.ptr -> i64
    %21 = arith.cmpi slt, %20, %1 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %22 = llvm.load %19 : !llvm.ptr -> i64
      %23 = arith.trunci %22 : i64 to i32
      %24 = llvm.load %19 : !llvm.ptr -> i64
      %25 = llvm.getelementptr %3[%24] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %23, %25 : i32, !llvm.ptr
      %26 = llvm.load %19 : !llvm.ptr -> i64
      %27 = arith.constant 1 : i32
      %29 = arith.extsi %27 : i32 to i64
      %28 = arith.addi %26, %29 : i64
      llvm.store %28, %19 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %30 = arith.constant 2 : i32
    %31 = arith.extsi %30 : i32 to i64
    llvm.store %31, %19 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %32 = llvm.load %19 : !llvm.ptr -> i64
    %33 = arith.cmpi slt, %32, %1 : i64
    cf.cond_br %33, ^bb7, ^bb8
    ^bb7:
      %35 = llvm.load %19 : !llvm.ptr -> i64
      %36 = llvm.getelementptr %3[%35] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %34 = llvm.load %36 : !llvm.ptr -> i32
      %37 = llvm.load %19 : !llvm.ptr -> i64
      %38 = arith.trunci %37 : i64 to i32
      %39 = arith.cmpi eq, %34, %38 : i32
      cf.cond_br %39, ^bb9, ^bb10
      ^bb9:
        %40 = llvm.load %19 : !llvm.ptr -> i64
        %41 = llvm.mlir.constant(1 : i64) : i64
        %42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
        llvm.store %40, %42 : i64, !llvm.ptr
        cf.br ^bb12
        ^bb12:
        %43 = llvm.load %42 : !llvm.ptr -> i64
        %44 = arith.cmpi slt, %43, %1 : i64
        cf.cond_br %44, ^bb13, ^bb14
        ^bb13:
          %46 = llvm.load %42 : !llvm.ptr -> i64
          %47 = llvm.getelementptr %3[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %45 = llvm.load %47 : !llvm.ptr -> i32
          %49 = llvm.load %42 : !llvm.ptr -> i64
          %50 = llvm.getelementptr %3[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %48 = llvm.load %50 : !llvm.ptr -> i32
          %51 = llvm.load %19 : !llvm.ptr -> i64
          %52 = arith.trunci %51 : i64 to i32
          %53 = arith.divsi %48, %52 : i32
          %54 = arith.subi %45, %53 : i32
          %55 = llvm.load %42 : !llvm.ptr -> i64
          %56 = llvm.getelementptr %3[%55] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %54, %56 : i32, !llvm.ptr
          %57 = llvm.load %42 : !llvm.ptr -> i64
          %58 = llvm.load %19 : !llvm.ptr -> i64
          %59 = arith.addi %57, %58 : i64
          llvm.store %59, %42 : i64, !llvm.ptr
          cf.br ^bb12
        ^bb14:
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %60 = llvm.load %19 : !llvm.ptr -> i64
      %61 = arith.constant 1 : i32
      %63 = arith.extsi %61 : i32 to i64
      %62 = arith.addi %60, %63 : i64
      llvm.store %62, %19 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %64 = arith.constant 1 : i32
    %65 = arith.constant 1 : i32
    %66 = arith.trunci %64 : i32 to i16
    %67 = arith.extsi %65 : i32 to i64
    %68 = llvm.getelementptr %6[%67] : (!llvm.ptr, i64) -> !llvm.ptr, i16
    llvm.store %66, %68 : i16, !llvm.ptr
    %69 = arith.constant 2 : i32
    %70 = arith.extsi %69 : i32 to i64
    llvm.store %70, %19 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %71 = llvm.load %19 : !llvm.ptr -> i64
    %72 = arith.cmpi slt, %71, %1 : i64
    cf.cond_br %72, ^bb16, ^bb17
    ^bb16:
      %75 = llvm.load %19 : !llvm.ptr -> i64
      %76 = llvm.getelementptr %3[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %74 = llvm.load %76 : !llvm.ptr -> i32
      %77 = arith.extsi %74 : i32 to i64
      %78 = llvm.getelementptr %6[%77] : (!llvm.ptr, i64) -> !llvm.ptr, i16
      %73 = llvm.load %78 : !llvm.ptr -> i16
      %79 = arith.extsi %73 : i16 to i32
      %80 = arith.constant 1 : i32
      %81 = arith.addi %79, %80 : i32
      %82 = arith.trunci %81 : i32 to i16
      %83 = llvm.load %19 : !llvm.ptr -> i64
      %84 = llvm.getelementptr %6[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i16
      llvm.store %82, %84 : i16, !llvm.ptr
      %85 = llvm.load %19 : !llvm.ptr -> i64
      %86 = arith.constant 1 : i32
      %88 = arith.extsi %86 : i32 to i64
      %87 = arith.addi %85, %88 : i64
      llvm.store %87, %19 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %89 = arith.constant 0 : i32
    %90 = arith.extsi %89 : i32 to i64
    %91 = llvm.mlir.constant(1 : i64) : i64
    %92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
    llvm.store %90, %92 : i64, !llvm.ptr
    %93 = arith.constant 2 : i32
    %94 = arith.extsi %93 : i32 to i64
    llvm.store %94, %19 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %95 = llvm.load %19 : !llvm.ptr -> i64
    %96 = arith.cmpi slt, %95, %1 : i64
    cf.cond_br %96, ^bb19, ^bb20
    ^bb19:
      %98 = llvm.load %19 : !llvm.ptr -> i64
      %99 = llvm.getelementptr %3[%98] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %97 = llvm.load %99 : !llvm.ptr -> i32
      %100 = llvm.load %19 : !llvm.ptr -> i64
      %101 = arith.trunci %100 : i64 to i32
      %102 = arith.constant 1 : i32
      %103 = arith.subi %101, %102 : i32
      %104 = arith.cmpi eq, %97, %103 : i32
      cf.cond_br %104, ^bb21, ^bb22
      ^bb21:
        %106 = llvm.load %19 : !llvm.ptr -> i64
        %107 = llvm.getelementptr %6[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i16
        %105 = llvm.load %107 : !llvm.ptr -> i16
        %108 = arith.extsi %105 : i16 to i32
        %109 = arith.cmpi eq, %108, %2 : i32
        cf.cond_br %109, ^bb24, ^bb25
        ^bb24:
          %110 = llvm.load %92 : !llvm.ptr -> i64
          %111 = llvm.load %19 : !llvm.ptr -> i64
          %112 = arith.addi %110, %111 : i64
          llvm.store %112, %92 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %113 = llvm.load %19 : !llvm.ptr -> i64
      %114 = arith.constant 1 : i32
      %116 = arith.extsi %114 : i32 to i64
      %115 = arith.addi %113, %116 : i64
      llvm.store %115, %19 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %117 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %118 = llvm.load %92 : !llvm.ptr -> i64
    %119 = llvm.call @printf(%117, %118) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%3) : (!llvm.ptr) -> ()
    func.call @free(%6) : (!llvm.ptr) -> ()
    %122 = arith.constant 0 : i32
    func.return %122 : i32
  }
}