Problem 351

Hexagonal orchards: H(n) = 3*n*(n+1) - 6*sum_{i=1..n} φ(i), n = 10^8.

Answer11762187201804552
Output11762187201804552
StatusPASS
Native helperno
Runtime2570 ms
Peak memory391744 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 351
# Hexagonal orchards: H(n) = 3*n*(n+1) - 6*sum_{i=1..n} φ(i), n = 10^8.

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

function main() -> i32 {
    let n: i64 = 100000000
    let phi: ptr<i32> = calloc(n + 1, 4)
    if phi == null { return 1 }

    let mut i: i64 = 0
    while i <= n {
        phi[i] = i as i32
        i = i + 1
    }

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

    let mut sum_phi: i64 = 0
    i = 1
    while i <= n {
        sum_phi = sum_phi + (phi[i] as i64)
        i = i + 1
    }

    let ans: i64 = 3 * n * (n + 1) - 6 * sum_phi
    printf("%lld\n", ans)
    free(phi)
    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 n = 100000000;
    int32_t* phi = (int32_t*)(calloc((n + 1), 4));
    if (phi == NULL) {
        return 1;
    }
    int64_t i = 0;
    while (i <= n) {
        phi[i] = ((int32_t)(i));
        i = (i + 1);
    }
    i = 2;
    while (i <= n) {
        if (phi[i] == ((int32_t)(i))) {
            int64_t j = i;
            while (j <= n) {
                phi[j] = (phi[j] - FLOW_CHECKED_DIV((phi[j]), (((int32_t)(i)))));
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t sum_phi = 0;
    i = 1;
    while (i <= n) {
        sum_phi = (sum_phi + ((int64_t)(phi[i])));
        i = (i + 1);
    }
    int64_t ans = (((3 * n) * (n + 1)) - (6 * sum_phi));
    printf("%lld\n", ans);
    free(phi);
    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 100000000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 1 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.addi %1, %5 : i64
    %6 = arith.constant 4 : i32
    %7 = arith.extsi %6 : i32 to i64
    %2 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
    %8 = llvm.mlir.zero : !llvm.ptr
    %9 = llvm.icmp "eq" %2, %8 : !llvm.ptr
    cf.cond_br %9, ^bb0, ^bb1
    ^bb0:
      %10 = arith.constant 1 : i32
      func.return %10 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %11 = arith.constant 0 : i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.cmpi sle, %15, %1 : i64
    cf.cond_br %16, ^bb4, ^bb5
    ^bb4:
      %17 = llvm.load %14 : !llvm.ptr -> i64
      %18 = arith.trunci %17 : i64 to i32
      %19 = llvm.load %14 : !llvm.ptr -> i64
      %20 = llvm.getelementptr %2[%19] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %18, %20 : i32, !llvm.ptr
      %21 = llvm.load %14 : !llvm.ptr -> i64
      %22 = arith.constant 1 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.addi %21, %24 : i64
      llvm.store %23, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %25 = arith.constant 2 : i32
    %26 = arith.extsi %25 : i32 to i64
    llvm.store %26, %14 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %27 = llvm.load %14 : !llvm.ptr -> i64
    %28 = arith.cmpi sle, %27, %1 : i64
    cf.cond_br %28, ^bb7, ^bb8
    ^bb7:
      %30 = llvm.load %14 : !llvm.ptr -> i64
      %31 = llvm.getelementptr %2[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %29 = llvm.load %31 : !llvm.ptr -> i32
      %32 = llvm.load %14 : !llvm.ptr -> i64
      %33 = arith.trunci %32 : i64 to i32
      %34 = arith.cmpi eq, %29, %33 : i32
      cf.cond_br %34, ^bb9, ^bb10
      ^bb9:
        %35 = llvm.load %14 : !llvm.ptr -> i64
        %36 = llvm.mlir.constant(1 : i64) : i64
        %37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
        llvm.store %35, %37 : i64, !llvm.ptr
        cf.br ^bb12
        ^bb12:
        %38 = llvm.load %37 : !llvm.ptr -> i64
        %39 = arith.cmpi sle, %38, %1 : i64
        cf.cond_br %39, ^bb13, ^bb14
        ^bb13:
          %41 = llvm.load %37 : !llvm.ptr -> i64
          %42 = llvm.getelementptr %2[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %40 = llvm.load %42 : !llvm.ptr -> i32
          %44 = llvm.load %37 : !llvm.ptr -> i64
          %45 = llvm.getelementptr %2[%44] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %43 = llvm.load %45 : !llvm.ptr -> i32
          %46 = llvm.load %14 : !llvm.ptr -> i64
          %47 = arith.trunci %46 : i64 to i32
          %48 = arith.divsi %43, %47 : i32
          %49 = arith.subi %40, %48 : i32
          %50 = llvm.load %37 : !llvm.ptr -> i64
          %51 = llvm.getelementptr %2[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %49, %51 : i32, !llvm.ptr
          %52 = llvm.load %37 : !llvm.ptr -> i64
          %53 = llvm.load %14 : !llvm.ptr -> i64
          %54 = arith.addi %52, %53 : i64
          llvm.store %54, %37 : i64, !llvm.ptr
          cf.br ^bb12
        ^bb14:
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %55 = llvm.load %14 : !llvm.ptr -> i64
      %56 = arith.constant 1 : i32
      %58 = arith.extsi %56 : i32 to i64
      %57 = arith.addi %55, %58 : i64
      llvm.store %57, %14 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %59 = arith.constant 0 : 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 1 : i32
    %64 = arith.extsi %63 : i32 to i64
    llvm.store %64, %14 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %65 = llvm.load %14 : !llvm.ptr -> i64
    %66 = arith.cmpi sle, %65, %1 : i64
    cf.cond_br %66, ^bb16, ^bb17
    ^bb16:
      %67 = llvm.load %62 : !llvm.ptr -> i64
      %69 = llvm.load %14 : !llvm.ptr -> i64
      %70 = llvm.getelementptr %2[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %68 = llvm.load %70 : !llvm.ptr -> i32
      %71 = arith.extsi %68 : i32 to i64
      %72 = arith.addi %67, %71 : i64
      llvm.store %72, %62 : i64, !llvm.ptr
      %73 = llvm.load %14 : !llvm.ptr -> i64
      %74 = arith.constant 1 : i32
      %76 = arith.extsi %74 : i32 to i64
      %75 = arith.addi %73, %76 : i64
      llvm.store %75, %14 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %77 = arith.constant 3 : i32
    %79 = arith.extsi %77 : i32 to i64
    %78 = arith.muli %79, %1 : i64
    %80 = arith.constant 1 : i32
    %82 = arith.extsi %80 : i32 to i64
    %81 = arith.addi %1, %82 : i64
    %83 = arith.muli %78, %81 : i64
    %84 = arith.constant 6 : i32
    %85 = llvm.load %62 : !llvm.ptr -> i64
    %87 = arith.extsi %84 : i32 to i64
    %86 = arith.muli %87, %85 : i64
    %88 = arith.subi %83, %86 : i64
    %89 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %90 = llvm.call @printf(%89, %88) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %92 = arith.constant 0 : i32
    func.return %92 : i32
  }
}