← All problems
Problem 512
Sums of Totients of Powers — odd-totient prefix G(5e8).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n log log n)
Space complexity O(n)O(n)
Approach Flow solution Sieve-based totient computation
Verdict Optimal
Flow source
# Project Euler 512
# Sums of Totients of Powers — odd-totient prefix G(5e8).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const CAP: i64 = 2097152
const TARGET: i64 = 500000000
function odd_part_sum(n0: i64) -> i64 {
let mut n: i64 = n0
let mut total: i64 = 0
while n > 0 {
let odd_count: i64 = (n + 1) / 2
total = total + odd_count * odd_count
n = n / 2
}
return total
}
function hslot(key: i64, keys: ptr<i64>, used: ptr<i8>) -> i64 {
let mut h: i64 = key % CAP
if h < 0 { h = h + CAP }
while used[h] == 1 && keys[h] != key {
h = h + 1
if h == CAP { h = 0 }
}
return h
}
function odd_totient(n: i64, keys: ptr<i64>, vals: ptr<i64>, used: ptr<i8>) -> i64 {
if n <= 0 { return 0 }
let s: i64 = hslot(n, keys, used)
if used[s] == 1 { return vals[s] }
let mut total: i64 = odd_part_sum(n)
let mut lo: i64 = 2
while lo <= n {
let q: i64 = n / lo
let hi: i64 = n / q
total = total - (hi - lo + 1) * odd_totient(q, keys, vals, used)
lo = hi + 1
}
used[s] = 1
keys[s] = n
vals[s] = total
return total
}
function main() -> i32 {
let keys: ptr<i64> = calloc(CAP, 8)
let vals: ptr<i64> = calloc(CAP, 8)
let used: ptr<i8> = calloc(CAP, 1)
if keys == null || vals == null || used == null { return 1 }
let ans: i64 = odd_totient(TARGET, keys, vals, used)
printf("%lld\n", ans)
free(keys); free(vals); free(used)
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; }
int64_t odd_part_sum_i64(int64_t n0);
int64_t hslot_i64_ptr_i64_ptr_i8(int64_t key, int64_t* keys, int8_t* used);
int64_t odd_totient_i64_ptr_i64_ptr_i64_ptr_i8(int64_t n, int64_t* keys, int64_t* vals, int8_t* used);
int32_t main(void);
static const int64_t CAP = 2097152;
static const int64_t TARGET = 500000000;
int64_t odd_part_sum_i64(int64_t n0) {
int64_t n = n0;
int64_t total = 0;
while (n > 0) {
int64_t odd_count = FLOW_CHECKED_DIV(((n + 1)), (2));
total = (total + (odd_count * odd_count));
n = FLOW_CHECKED_DIV((n), (2));
}
return total;
}
int64_t hslot_i64_ptr_i64_ptr_i8(int64_t key, int64_t* keys, int8_t* used) {
int64_t h = FLOW_CHECKED_MOD((key), (CAP));
if (h < 0) {
h = (h + CAP);
}
while ((used[h] == 1 && keys[h] != key)) {
h = (h + 1);
if (h == CAP) {
h = 0;
}
}
return h;
}
int64_t odd_totient_i64_ptr_i64_ptr_i64_ptr_i8(int64_t n, int64_t* keys, int64_t* vals, int8_t* used) {
if (n <= 0) {
return 0;
}
int64_t s = hslot_i64_ptr_i64_ptr_i8(n, keys, used);
if (used[s] == 1) {
return vals[s];
}
int64_t total = odd_part_sum_i64(n);
int64_t lo = 2;
while (lo <= n) {
int64_t q = FLOW_CHECKED_DIV((n), (lo));
int64_t hi = FLOW_CHECKED_DIV((n), (q));
total = (total - (((hi - lo) + 1) * odd_totient_i64_ptr_i64_ptr_i64_ptr_i8(q, keys, vals, used)));
lo = (hi + 1);
}
used[s] = 1;
keys[s] = n;
vals[s] = total;
return total;
}
int32_t main(void) {
int64_t* keys = (int64_t*)(calloc(CAP, 8));
int64_t* vals = (int64_t*)(calloc(CAP, 8));
int8_t* used = (int8_t*)(calloc(CAP, 1));
if (((keys == NULL || vals == NULL) || used == NULL)) {
return 1;
}
int64_t ans = odd_totient_i64_ptr_i64_ptr_i64_ptr_i8(TARGET, keys, vals, used);
printf("%lld\n", ans);
free(keys);
free(vals);
free(used);
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) -> ()
// Constant: CAP
llvm.mlir.global internal constant @CAP(2097152 : i64) : i64
// Constant: TARGET
llvm.mlir.global internal constant @TARGET(500000000 : i64) : i64
func.func @odd_part_sum(%arg0: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = arith.constant 0 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
llvm.store %3, %5 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%6 = llvm.load %1 : !llvm.ptr -> i64
%7 = arith.constant 0 : i32
%9 = arith.extsi %7 : i32 to i64
%8 = arith.cmpi sgt, %6, %9 : i64
cf.cond_br %8, ^bb1, ^bb2
^bb1:
%10 = llvm.load %1 : !llvm.ptr -> i64
%11 = arith.constant 1 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.addi %10, %13 : i64
%14 = arith.constant 2 : i32
%16 = arith.extsi %14 : i32 to i64
%15 = arith.divsi %12, %16 : i64
%17 = llvm.load %5 : !llvm.ptr -> i64
%18 = arith.muli %15, %15 : i64
%19 = arith.addi %17, %18 : i64
llvm.store %19, %5 : i64, !llvm.ptr
%20 = llvm.load %1 : !llvm.ptr -> i64
%21 = arith.constant 2 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.divsi %20, %23 : i64
llvm.store %22, %1 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%24 = llvm.load %5 : !llvm.ptr -> i64
func.return %24 : i64
}
func.func @hslot(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr) -> i64 {
%25 = llvm.mlir.addressof @CAP : !llvm.ptr
%26 = llvm.load %25 : !llvm.ptr -> i64
%27 = arith.remsi %arg0, %26 : i64
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
llvm.store %27, %29 : i64, !llvm.ptr
%30 = llvm.load %29 : !llvm.ptr -> i64
%31 = arith.constant 0 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.cmpi slt, %30, %33 : i64
cf.cond_br %32, ^bb3, ^bb4
^bb3:
%34 = llvm.load %29 : !llvm.ptr -> i64
%35 = llvm.mlir.addressof @CAP : !llvm.ptr
%36 = llvm.load %35 : !llvm.ptr -> i64
%37 = arith.addi %34, %36 : i64
llvm.store %37, %29 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
cf.br ^bb6
^bb6:
%39 = llvm.load %29 : !llvm.ptr -> i64
%40 = llvm.getelementptr %arg2[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%38 = llvm.load %40 : !llvm.ptr -> i8
%41 = arith.constant 1 : i32
%43 = arith.extsi %38 : i8 to i32
%42 = arith.cmpi eq, %43, %41 : i32
%44 = scf.if %42 -> (i1) {
%46 = llvm.load %29 : !llvm.ptr -> i64
%47 = llvm.getelementptr %arg1[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%45 = llvm.load %47 : !llvm.ptr -> i64
%48 = arith.cmpi ne, %45, %arg0 : i64
scf.yield %48 : i1
} else {
%49 = arith.constant false
scf.yield %49 : i1
}
cf.cond_br %44, ^bb7, ^bb8
^bb7:
%50 = llvm.load %29 : !llvm.ptr -> i64
%51 = arith.constant 1 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.addi %50, %53 : i64
llvm.store %52, %29 : i64, !llvm.ptr
%54 = llvm.load %29 : !llvm.ptr -> i64
%55 = llvm.mlir.addressof @CAP : !llvm.ptr
%56 = llvm.load %55 : !llvm.ptr -> i64
%57 = arith.cmpi eq, %54, %56 : i64
cf.cond_br %57, ^bb9, ^bb10
^bb9:
%58 = arith.constant 0 : i32
%59 = arith.extsi %58 : i32 to i64
llvm.store %59, %29 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
cf.br ^bb6
^bb8:
%60 = llvm.load %29 : !llvm.ptr -> i64
func.return %60 : i64
}
func.func @odd_totient(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: !llvm.ptr) -> i64 {
%61 = arith.constant 0 : i32
%63 = arith.extsi %61 : i32 to i64
%62 = arith.cmpi sle, %arg0, %63 : i64
cf.cond_br %62, ^bb12, ^bb13
^bb12:
%64 = arith.constant 0 : i32
%65 = arith.extsi %64 : i32 to i64
func.return %65 : i64
^bb13:
cf.br ^bb14
^bb14:
%66 = func.call @hslot(%arg0, %arg1, %arg3) : (i64, !llvm.ptr, !llvm.ptr) -> i64
%68 = llvm.getelementptr %arg3[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%67 = llvm.load %68 : !llvm.ptr -> i8
%69 = arith.constant 1 : i32
%71 = arith.extsi %67 : i8 to i32
%70 = arith.cmpi eq, %71, %69 : i32
cf.cond_br %70, ^bb15, ^bb16
^bb15:
%73 = llvm.getelementptr %arg2[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%72 = llvm.load %73 : !llvm.ptr -> i64
func.return %72 : i64
^bb16:
cf.br ^bb17
^bb17:
%74 = func.call @odd_part_sum(%arg0) : (i64) -> i64
%75 = llvm.mlir.constant(1 : i64) : i64
%76 = llvm.alloca %75 x i64 : (i64) -> !llvm.ptr
llvm.store %74, %76 : i64, !llvm.ptr
%77 = arith.constant 2 : i32
%78 = arith.extsi %77 : i32 to i64
%79 = llvm.mlir.constant(1 : i64) : i64
%80 = llvm.alloca %79 x i64 : (i64) -> !llvm.ptr
llvm.store %78, %80 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%81 = llvm.load %80 : !llvm.ptr -> i64
%82 = arith.cmpi sle, %81, %arg0 : i64
cf.cond_br %82, ^bb19, ^bb20
^bb19:
%83 = llvm.load %80 : !llvm.ptr -> i64
%84 = arith.divsi %arg0, %83 : i64
%85 = arith.divsi %arg0, %84 : i64
%86 = llvm.load %76 : !llvm.ptr -> i64
%87 = llvm.load %80 : !llvm.ptr -> i64
%88 = arith.subi %85, %87 : i64
%89 = arith.constant 1 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.addi %88, %91 : i64
%92 = func.call @odd_totient(%84, %arg1, %arg2, %arg3) : (i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i64
%93 = arith.muli %90, %92 : i64
%94 = arith.subi %86, %93 : i64
llvm.store %94, %76 : i64, !llvm.ptr
%95 = arith.constant 1 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.addi %85, %97 : i64
llvm.store %96, %80 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%98 = arith.constant 1 : i32
%99 = arith.trunci %98 : i32 to i8
%100 = llvm.getelementptr %arg3[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %99, %100 : i8, !llvm.ptr
%101 = llvm.getelementptr %arg1[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %arg0, %101 : i64, !llvm.ptr
%102 = llvm.load %76 : !llvm.ptr -> i64
%103 = llvm.getelementptr %arg2[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %102, %103 : i64, !llvm.ptr
%104 = llvm.load %76 : !llvm.ptr -> i64
func.return %104 : i64
}
func.func @main() -> i32 {
%106 = llvm.mlir.addressof @CAP : !llvm.ptr
%107 = llvm.load %106 : !llvm.ptr -> i64
%108 = arith.constant 8 : i32
%109 = arith.extsi %108 : i32 to i64
%105 = func.call @calloc(%107, %109) : (i64, i64) -> !llvm.ptr
%111 = llvm.mlir.addressof @CAP : !llvm.ptr
%112 = llvm.load %111 : !llvm.ptr -> i64
%113 = arith.constant 8 : i32
%114 = arith.extsi %113 : i32 to i64
%110 = func.call @calloc(%112, %114) : (i64, i64) -> !llvm.ptr
%116 = llvm.mlir.addressof @CAP : !llvm.ptr
%117 = llvm.load %116 : !llvm.ptr -> i64
%118 = arith.constant 1 : i32
%119 = arith.extsi %118 : i32 to i64
%115 = func.call @calloc(%117, %119) : (i64, i64) -> !llvm.ptr
%120 = llvm.mlir.zero : !llvm.ptr
%121 = llvm.icmp "eq" %105, %120 : !llvm.ptr
%122 = scf.if %121 -> (i1) {
%123 = arith.constant true
scf.yield %123 : i1
} else {
%124 = llvm.mlir.zero : !llvm.ptr
%125 = llvm.icmp "eq" %110, %124 : !llvm.ptr
scf.yield %125 : i1
}
%126 = scf.if %122 -> (i1) {
%127 = arith.constant true
scf.yield %127 : i1
} else {
%128 = llvm.mlir.zero : !llvm.ptr
%129 = llvm.icmp "eq" %115, %128 : !llvm.ptr
scf.yield %129 : i1
}
cf.cond_br %126, ^bb21, ^bb22
^bb21:
%130 = arith.constant 1 : i32
func.return %130 : i32
^bb22:
cf.br ^bb23
^bb23:
%132 = llvm.mlir.addressof @TARGET : !llvm.ptr
%133 = llvm.load %132 : !llvm.ptr -> i64
%131 = func.call @odd_totient(%133, %105, %110, %115) : (i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i64
%134 = llvm.mlir.addressof @str_0 : !llvm.ptr
%135 = llvm.call @printf(%134, %131) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%105) : (!llvm.ptr) -> ()
func.call @free(%110) : (!llvm.ptr) -> ()
func.call @free(%115) : (!llvm.ptr) -> ()
%139 = arith.constant 0 : i32
func.return %139 : i32
}
}