Problem 724
Drone Delivery — E(10^8) rounded to nearest integer. E(n) = (n/2) * (H_n^2 + H_n^(2)) Uses asymptotic expansions for harmonic numbers.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(1) | ? |
| Space complexity | O(1) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 724
# Drone Delivery — E(10^8) rounded to nearest integer.
#
# E(n) = (n/2) * (H_n^2 + H_n^(2))
# Uses asymptotic expansions for harmonic numbers.
extern {
function log(x: f64) -> f64
function floor(x: f64) -> f64
}
const EULER_GAMMA: f64 = 0.5772156649015328606065120900824024310421
const PI: f64 = 3.1415926535897932384626433832795028841971
function harmonic_asymptotic(n: f64) -> f64 {
let inv: f64 = 1.0 / n
let inv2: f64 = inv * inv
let inv4: f64 = inv2 * inv2
let inv6: f64 = inv4 * inv2
return log(n)
+ EULER_GAMMA
+ 0.5 * inv
- (1.0 / 12.0) * inv2
+ (1.0 / 120.0) * inv4
- (1.0 / 252.0) * inv6
}
function harmonic2_asymptotic(n: f64) -> f64 {
let inv: f64 = 1.0 / n
let inv2: f64 = inv * inv
let inv3: f64 = inv2 * inv
let inv5: f64 = inv3 * inv2
let inv7: f64 = inv5 * inv2
return (PI * PI) / 6.0
- inv
+ 0.5 * inv2
- (1.0 / 6.0) * inv3
+ (1.0 / 30.0) * inv5
- (1.0 / 42.0) * inv7
}
function main() -> i32 {
let n: f64 = 1e8
let h: f64 = harmonic_asymptotic(n)
let h2: f64 = harmonic2_asymptotic(n)
let e: f64 = 0.5 * n * (h * h + h2)
printf("%lld\n", (floor(e + 0.5) as i64))
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; }
double harmonic_asymptotic_f64(double n);
double harmonic2_asymptotic_f64(double n);
int32_t main(void);
static const double EULER_GAMMA = 0.5772156649015328606065120900824024310421;
static const double PI = 3.1415926535897932384626433832795028841971;
double harmonic_asymptotic_f64(double n) {
double inv = (1.0 / n);
double inv2 = (inv * inv);
double inv4 = (inv2 * inv2);
double inv6 = (inv4 * inv2);
return (((((log(n) + EULER_GAMMA) + (0.5 * inv)) - ((1.0 / 12.0) * inv2)) + ((1.0 / 120.0) * inv4)) - ((1.0 / 252.0) * inv6));
}
double harmonic2_asymptotic_f64(double n) {
double inv = (1.0 / n);
double inv2 = (inv * inv);
double inv3 = (inv2 * inv);
double inv5 = (inv3 * inv2);
double inv7 = (inv5 * inv2);
return (((((((PI * PI) / 6.0) - inv) + (0.5 * inv2)) - ((1.0 / 6.0) * inv3)) + ((1.0 / 30.0) * inv5)) - ((1.0 / 42.0) * inv7));
}
int32_t main(void) {
double n = 1e8;
double h = harmonic_asymptotic_f64(n);
double h2 = harmonic2_asymptotic_f64(n);
double e = ((0.5 * n) * ((h * h) + h2));
printf("%lld\n", ((int64_t)(floor((e + 0.5)))));
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 @log(f64) -> f64
func.func private @floor(f64) -> f64
// Constant: EULER_GAMMA
llvm.mlir.global internal constant @EULER_GAMMA(0.5772156649015328606065120900824024310421 : f64) : f64
// Constant: PI
llvm.mlir.global internal constant @PI(3.1415926535897932384626433832795028841971 : f64) : f64
func.func @harmonic_asymptotic(%arg0: f64) -> f64 {
%0 = arith.constant 1.0 : f32
%2 = arith.extf %0 : f32 to f64
%1 = arith.divf %2, %arg0 : f64
%3 = arith.mulf %1, %1 : f64
%4 = arith.mulf %3, %3 : f64
%5 = arith.mulf %4, %3 : f64
%6 = math.log %arg0 : f64
%7 = llvm.mlir.addressof @EULER_GAMMA : !llvm.ptr
%8 = llvm.load %7 : !llvm.ptr -> f64
%9 = arith.addf %6, %8 : f64
%10 = arith.constant 0.5 : f32
%12 = arith.extf %10 : f32 to f64
%11 = arith.mulf %12, %1 : f64
%13 = arith.addf %9, %11 : f64
%14 = arith.constant 1.0 : f32
%15 = arith.constant 12.0 : f32
%16 = arith.divf %14, %15 : f32
%18 = arith.extf %16 : f32 to f64
%17 = arith.mulf %18, %3 : f64
%19 = arith.subf %13, %17 : f64
%20 = arith.constant 1.0 : f32
%21 = arith.constant 120.0 : f32
%22 = arith.divf %20, %21 : f32
%24 = arith.extf %22 : f32 to f64
%23 = arith.mulf %24, %4 : f64
%25 = arith.addf %19, %23 : f64
%26 = arith.constant 1.0 : f32
%27 = arith.constant 252.0 : f32
%28 = arith.divf %26, %27 : f32
%30 = arith.extf %28 : f32 to f64
%29 = arith.mulf %30, %5 : f64
%31 = arith.subf %25, %29 : f64
func.return %31 : f64
}
func.func @harmonic2_asymptotic(%arg0: f64) -> f64 {
%32 = arith.constant 1.0 : f32
%34 = arith.extf %32 : f32 to f64
%33 = arith.divf %34, %arg0 : f64
%35 = arith.mulf %33, %33 : f64
%36 = arith.mulf %35, %33 : f64
%37 = arith.mulf %36, %35 : f64
%38 = arith.mulf %37, %35 : f64
%39 = llvm.mlir.addressof @PI : !llvm.ptr
%40 = llvm.load %39 : !llvm.ptr -> f64
%41 = llvm.mlir.addressof @PI : !llvm.ptr
%42 = llvm.load %41 : !llvm.ptr -> f64
%43 = arith.mulf %40, %42 : f64
%44 = arith.constant 6.0 : f32
%46 = arith.extf %44 : f32 to f64
%45 = arith.divf %43, %46 : f64
%47 = arith.subf %45, %33 : f64
%48 = arith.constant 0.5 : f32
%50 = arith.extf %48 : f32 to f64
%49 = arith.mulf %50, %35 : f64
%51 = arith.addf %47, %49 : f64
%52 = arith.constant 1.0 : f32
%53 = arith.constant 6.0 : f32
%54 = arith.divf %52, %53 : f32
%56 = arith.extf %54 : f32 to f64
%55 = arith.mulf %56, %36 : f64
%57 = arith.subf %51, %55 : f64
%58 = arith.constant 1.0 : f32
%59 = arith.constant 30.0 : f32
%60 = arith.divf %58, %59 : f32
%62 = arith.extf %60 : f32 to f64
%61 = arith.mulf %62, %37 : f64
%63 = arith.addf %57, %61 : f64
%64 = arith.constant 1.0 : f32
%65 = arith.constant 42.0 : f32
%66 = arith.divf %64, %65 : f32
%68 = arith.extf %66 : f32 to f64
%67 = arith.mulf %68, %38 : f64
%69 = arith.subf %63, %67 : f64
func.return %69 : f64
}
func.func @main() -> i32 {
%70 = arith.constant 100000000 : f32
%71 = arith.extf %70 : f32 to f64
%72 = func.call @harmonic_asymptotic(%71) : (f64) -> f64
%73 = func.call @harmonic2_asymptotic(%71) : (f64) -> f64
%74 = arith.constant 0.5 : f32
%76 = arith.extf %74 : f32 to f64
%75 = arith.mulf %76, %71 : f64
%77 = arith.mulf %72, %72 : f64
%78 = arith.addf %77, %73 : f64
%79 = arith.mulf %75, %78 : f64
%80 = llvm.mlir.addressof @str_0 : !llvm.ptr
%82 = arith.constant 0.5 : f32
%84 = arith.extf %82 : f32 to f64
%83 = arith.addf %79, %84 : f64
%81 = func.call @floor(%83) : (f64) -> f64
%85 = arith.fptosi %81 : f64 to i64
%86 = llvm.call @printf(%80, %85) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%87 = arith.constant 0 : i32
func.return %87 : i32
}
}