← All problems
Problem 228
Sides of Minkowski sum S_1864+...+S_1909.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log n)
Space complexity O(n)O(n)
Approach Flow solution Convex polygon sum
Verdict Suboptimal
Flow source
# Project Euler 228
# Sides of Minkowski sum S_1864+...+S_1909.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let L: i64 = 1864
let R: i64 = 1909
let phi: ptr<i32> = calloc(R + 1, 4)
if phi == null { return 1 }
let mut i: i64 = 0
while i <= R {
phi[i] = i as i32
i = i + 1
}
i = 2
while i <= R {
if phi[i] == i as i32 {
let mut j: i64 = i
while j <= R {
phi[j] = phi[j] - phi[j] / (i as i32)
j = j + i
}
}
i = i + 1
}
let mut total: i64 = 0
let mut d: i64 = 1
while d <= R {
let first: i64 = ((L + d - 1) / d) * d
if first <= R {
total = total + (phi[d] as i64)
}
d = d + 1
}
printf("%lld\n", total)
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 L = 1864;
int64_t R = 1909;
int32_t* phi = (int32_t*)(calloc((R + 1), 4));
if (phi == NULL) {
return 1;
}
int64_t i = 0;
while (i <= R) {
phi[i] = ((int32_t)(i));
i = (i + 1);
}
i = 2;
while (i <= R) {
if (phi[i] == ((int32_t)(i))) {
int64_t j = i;
while (j <= R) {
phi[j] = (phi[j] - FLOW_CHECKED_DIV((phi[j]), (((int32_t)(i)))));
j = (j + i);
}
}
i = (i + 1);
}
int64_t total = 0;
int64_t d = 1;
while (d <= R) {
int64_t first = (FLOW_CHECKED_DIV((((L + d) - 1)), (d)) * d);
if (first <= R) {
total = (total + ((int64_t)(phi[d])));
}
d = (d + 1);
}
printf("%lld\n", total);
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 1864 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 1909 : i32
%3 = arith.extsi %2 : i32 to i64
%5 = arith.constant 1 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.addi %3, %7 : i64
%8 = arith.constant 4 : i32
%9 = arith.extsi %8 : i32 to i64
%4 = func.call @calloc(%6, %9) : (i64, i64) -> !llvm.ptr
%10 = llvm.mlir.zero : !llvm.ptr
%11 = llvm.icmp "eq" %4, %10 : !llvm.ptr
cf.cond_br %11, ^bb0, ^bb1
^bb0:
%12 = arith.constant 1 : i32
func.return %12 : i32
^bb1:
cf.br ^bb2
^bb2:
%13 = arith.constant 0 : i32
%14 = arith.extsi %13 : i32 to i64
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i64 : (i64) -> !llvm.ptr
llvm.store %14, %16 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%17 = llvm.load %16 : !llvm.ptr -> i64
%18 = arith.cmpi sle, %17, %3 : i64
cf.cond_br %18, ^bb4, ^bb5
^bb4:
%19 = llvm.load %16 : !llvm.ptr -> i64
%20 = arith.trunci %19 : i64 to i32
%21 = llvm.load %16 : !llvm.ptr -> i64
%22 = llvm.getelementptr %4[%21] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %20, %22 : i32, !llvm.ptr
%23 = llvm.load %16 : !llvm.ptr -> i64
%24 = arith.constant 1 : i32
%26 = arith.extsi %24 : i32 to i64
%25 = arith.addi %23, %26 : i64
llvm.store %25, %16 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%27 = arith.constant 2 : i32
%28 = arith.extsi %27 : i32 to i64
llvm.store %28, %16 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%29 = llvm.load %16 : !llvm.ptr -> i64
%30 = arith.cmpi sle, %29, %3 : i64
cf.cond_br %30, ^bb7, ^bb8
^bb7:
%32 = llvm.load %16 : !llvm.ptr -> i64
%33 = llvm.getelementptr %4[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%31 = llvm.load %33 : !llvm.ptr -> i32
%34 = llvm.load %16 : !llvm.ptr -> i64
%35 = arith.trunci %34 : i64 to i32
%36 = arith.cmpi eq, %31, %35 : i32
cf.cond_br %36, ^bb9, ^bb10
^bb9:
%37 = llvm.load %16 : !llvm.ptr -> i64
%38 = llvm.mlir.constant(1 : i64) : i64
%39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
llvm.store %37, %39 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%40 = llvm.load %39 : !llvm.ptr -> i64
%41 = arith.cmpi sle, %40, %3 : i64
cf.cond_br %41, ^bb13, ^bb14
^bb13:
%43 = llvm.load %39 : !llvm.ptr -> i64
%44 = llvm.getelementptr %4[%43] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%42 = llvm.load %44 : !llvm.ptr -> i32
%46 = llvm.load %39 : !llvm.ptr -> i64
%47 = llvm.getelementptr %4[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%45 = llvm.load %47 : !llvm.ptr -> i32
%48 = llvm.load %16 : !llvm.ptr -> i64
%49 = arith.trunci %48 : i64 to i32
%50 = arith.divsi %45, %49 : i32
%51 = arith.subi %42, %50 : i32
%52 = llvm.load %39 : !llvm.ptr -> i64
%53 = llvm.getelementptr %4[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %51, %53 : i32, !llvm.ptr
%54 = llvm.load %39 : !llvm.ptr -> i64
%55 = llvm.load %16 : !llvm.ptr -> i64
%56 = arith.addi %54, %55 : i64
llvm.store %56, %39 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%57 = llvm.load %16 : !llvm.ptr -> i64
%58 = arith.constant 1 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.addi %57, %60 : i64
llvm.store %59, %16 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%61 = arith.constant 0 : i32
%62 = arith.extsi %61 : i32 to i64
%63 = llvm.mlir.constant(1 : i64) : i64
%64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
llvm.store %62, %64 : i64, !llvm.ptr
%65 = arith.constant 1 : i32
%66 = arith.extsi %65 : i32 to i64
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i64 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%69 = llvm.load %68 : !llvm.ptr -> i64
%70 = arith.cmpi sle, %69, %3 : i64
cf.cond_br %70, ^bb16, ^bb17
^bb16:
%71 = llvm.load %68 : !llvm.ptr -> i64
%72 = arith.addi %1, %71 : i64
%73 = arith.constant 1 : i32
%75 = arith.extsi %73 : i32 to i64
%74 = arith.subi %72, %75 : i64
%76 = llvm.load %68 : !llvm.ptr -> i64
%77 = arith.divsi %74, %76 : i64
%78 = llvm.load %68 : !llvm.ptr -> i64
%79 = arith.muli %77, %78 : i64
%80 = arith.cmpi sle, %79, %3 : i64
cf.cond_br %80, ^bb18, ^bb19
^bb18:
%81 = llvm.load %64 : !llvm.ptr -> i64
%83 = llvm.load %68 : !llvm.ptr -> i64
%84 = llvm.getelementptr %4[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%82 = llvm.load %84 : !llvm.ptr -> i32
%85 = arith.extsi %82 : i32 to i64
%86 = arith.addi %81, %85 : i64
llvm.store %86, %64 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%87 = llvm.load %68 : !llvm.ptr -> i64
%88 = arith.constant 1 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.addi %87, %90 : i64
llvm.store %89, %68 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%91 = llvm.mlir.addressof @str_0 : !llvm.ptr
%92 = llvm.load %64 : !llvm.ptr -> i64
%93 = llvm.call @printf(%91, %92) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%4) : (!llvm.ptr) -> ()
%95 = arith.constant 0 : i32
func.return %95 : i32
}
}