← All problems
Problem 722
Slowly Converging Series: evaluate E_15(q) = sum sigma_15(n) q^n at q = 1 - 2^-25. Mellin inversion of sum sigma_k(n) e^{-nt} against Gamma(s) zeta(s) zeta(s-k) t^{-s} leaves only the residues at s = k+1, s = 1 and s = 0 (all others sit on trivial zeros of zeta); the remainder is exponentially small, of order e^{-4 pi^2 / t}. For k = 15: E_15(e^{-t}) = 15! zeta(16) / t^16 + zeta(0) zeta(-15) = 15! zeta(16) / t^16 - 3617/16320. With t = -ln(1 - 2^-25) write t = 2^-25 * u, u = 1 + x/2 + x^2/3 + ... so t^16 = u^16 * 2^-400 and everything stays inside f64 range. The closed form reproduces the three check values E_1(1-1/2^4), E_3(1-1/2^8) and E_7(1-1/2^15) from the statement.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n log log n)
Space complexity O(1)O(n)
Approach Flow solution Sieve-based divisor sums
Verdict Optimal
Flow source
# Project Euler 722
# Slowly Converging Series: evaluate E_15(q) = sum sigma_15(n) q^n at
# q = 1 - 2^-25.
#
# Mellin inversion of sum sigma_k(n) e^{-nt} against Gamma(s) zeta(s)
# zeta(s-k) t^{-s} leaves only the residues at s = k+1, s = 1 and s = 0
# (all others sit on trivial zeros of zeta); the remainder is
# exponentially small, of order e^{-4 pi^2 / t}. For k = 15:
# E_15(e^{-t}) = 15! zeta(16) / t^16 + zeta(0) zeta(-15)
# = 15! zeta(16) / t^16 - 3617/16320.
# With t = -ln(1 - 2^-25) write t = 2^-25 * u, u = 1 + x/2 + x^2/3 + ...
# so t^16 = u^16 * 2^-400 and everything stays inside f64 range.
# The closed form reproduces the three check values E_1(1-1/2^4),
# E_3(1-1/2^8) and E_7(1-1/2^15) from the statement.
function main() -> i32 {
let x: f64 = 1.0 / 33554432.0
let u: f64 = 1.0 + x / 2.0 + x * x / 3.0 + x * x * x / 4.0
# zeta(16) by direct summation, smallest terms first
let mut z16: f64 = 0.0
let mut n: i64 = 60
while n >= 1 {
let nf: f64 = n as f64
let n2: f64 = nf * nf
let n4: f64 = n2 * n2
let n8: f64 = n4 * n4
z16 = z16 + 1.0 / (n8 * n8)
n = n - 1
}
let mut fact15: f64 = 1.0
let mut i: i64 = 2
while i <= 15 {
fact15 = fact15 * (i as f64)
i = i + 1
}
let mut p2_400: f64 = 1.0
let mut j: i64 = 0
while j < 400 {
p2_400 = p2_400 * 2.0
j = j + 1
}
# u^16 by repeated squaring
let mut upow: f64 = u
let mut s: i64 = 0
while s < 4 {
upow = upow * upow
s = s + 1
}
let e15: f64 = fact15 * z16 * p2_400 / upow - 3617.0 / 16320.0
# normalize to mantissa in [1, 10) with a decimal exponent
let mut mant: f64 = e15
let mut ex: i64 = 0
let tenp22: f64 = 10000000000000000000000.0
while mant >= tenp22 {
mant = mant / tenp22
ex = ex + 22
}
while mant >= 10.0 {
mant = mant / 10.0
ex = ex + 1
}
while mant < 1.0 {
mant = mant * 10.0
ex = ex - 1
}
printf("%.12fe%lld\n", mant, ex)
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) {
double x = (1.0 / 33554432.0);
double u = (((1.0 + (x / 2.0)) + ((x * x) / 3.0)) + (((x * x) * x) / 4.0));
double z16 = 0.0;
int64_t n = 60;
while (n >= 1) {
double nf = ((double)(n));
double n2 = (nf * nf);
double n4 = (n2 * n2);
double n8 = (n4 * n4);
z16 = (z16 + (1.0 / (n8 * n8)));
n = (n - 1);
}
double fact15 = 1.0;
int64_t i = 2;
while (i <= 15) {
fact15 = (fact15 * ((double)(i)));
i = (i + 1);
}
double p2_400 = 1.0;
int64_t j = 0;
while (j < 400) {
p2_400 = (p2_400 * 2.0);
j = (j + 1);
}
double upow = u;
int64_t s = 0;
while (s < 4) {
upow = (upow * upow);
s = (s + 1);
}
double e15 = ((((fact15 * z16) * p2_400) / upow) - (3617.0 / 16320.0));
double mant = e15;
int64_t ex = 0;
double tenp22 = 10000000000000000000000.0;
while (mant >= tenp22) {
mant = (mant / tenp22);
ex = (ex + 22);
}
while (mant >= 10.0) {
mant = (mant / 10.0);
ex = (ex + 1);
}
while (mant < 1.0) {
mant = (mant * 10.0);
ex = (ex - 1);
}
printf("%.12fe%lld\n", mant, ex);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.12fe%lld\n\00") {addr_space = 0 : i32} : !llvm.array<12 x i8>
func.func @main() -> i32 {
%0 = arith.constant 1.0 : f32
%1 = arith.constant 33554432.0 : f32
%2 = arith.divf %0, %1 : f32
%3 = arith.extf %2 : f32 to f64
%4 = arith.constant 1.0 : f32
%5 = arith.constant 2.0 : f32
%7 = arith.extf %5 : f32 to f64
%6 = arith.divf %3, %7 : f64
%9 = arith.extf %4 : f32 to f64
%8 = arith.addf %9, %6 : f64
%10 = arith.mulf %3, %3 : f64
%11 = arith.constant 3.0 : f32
%13 = arith.extf %11 : f32 to f64
%12 = arith.divf %10, %13 : f64
%14 = arith.addf %8, %12 : f64
%15 = arith.mulf %3, %3 : f64
%16 = arith.mulf %15, %3 : f64
%17 = arith.constant 4.0 : f32
%19 = arith.extf %17 : f32 to f64
%18 = arith.divf %16, %19 : f64
%20 = arith.addf %14, %18 : f64
%21 = arith.constant 0.0 : f32
%22 = arith.extf %21 : f32 to f64
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x f64 : (i64) -> !llvm.ptr
llvm.store %22, %24 : f64, !llvm.ptr
%25 = arith.constant 60 : i32
%26 = arith.extsi %25 : i32 to i64
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
llvm.store %26, %28 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%29 = llvm.load %28 : !llvm.ptr -> i64
%30 = arith.constant 1 : i32
%32 = arith.extsi %30 : i32 to i64
%31 = arith.cmpi sge, %29, %32 : i64
cf.cond_br %31, ^bb1, ^bb2
^bb1:
%33 = llvm.load %28 : !llvm.ptr -> i64
%34 = arith.sitofp %33 : i64 to f64
%35 = arith.mulf %34, %34 : f64
%36 = arith.mulf %35, %35 : f64
%37 = arith.mulf %36, %36 : f64
%38 = llvm.load %24 : !llvm.ptr -> f64
%39 = arith.constant 1.0 : f32
%40 = arith.mulf %37, %37 : f64
%42 = arith.extf %39 : f32 to f64
%41 = arith.divf %42, %40 : f64
%43 = arith.addf %38, %41 : f64
llvm.store %43, %24 : f64, !llvm.ptr
%44 = llvm.load %28 : !llvm.ptr -> i64
%45 = arith.constant 1 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.subi %44, %47 : i64
llvm.store %46, %28 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%48 = arith.constant 1.0 : f32
%49 = arith.extf %48 : f32 to f64
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x f64 : (i64) -> !llvm.ptr
llvm.store %49, %51 : f64, !llvm.ptr
%52 = arith.constant 2 : i32
%53 = arith.extsi %52 : i32 to i64
%54 = llvm.mlir.constant(1 : i64) : i64
%55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
llvm.store %53, %55 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%56 = llvm.load %55 : !llvm.ptr -> i64
%57 = arith.constant 15 : i32
%59 = arith.extsi %57 : i32 to i64
%58 = arith.cmpi sle, %56, %59 : i64
cf.cond_br %58, ^bb4, ^bb5
^bb4:
%60 = llvm.load %51 : !llvm.ptr -> f64
%61 = llvm.load %55 : !llvm.ptr -> i64
%62 = arith.sitofp %61 : i64 to f64
%63 = arith.mulf %60, %62 : f64
llvm.store %63, %51 : f64, !llvm.ptr
%64 = llvm.load %55 : !llvm.ptr -> i64
%65 = arith.constant 1 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.addi %64, %67 : i64
llvm.store %66, %55 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%68 = arith.constant 1.0 : f32
%69 = arith.extf %68 : f32 to f64
%70 = llvm.mlir.constant(1 : i64) : i64
%71 = llvm.alloca %70 x f64 : (i64) -> !llvm.ptr
llvm.store %69, %71 : f64, !llvm.ptr
%72 = arith.constant 0 : i32
%73 = arith.extsi %72 : i32 to i64
%74 = llvm.mlir.constant(1 : i64) : i64
%75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
llvm.store %73, %75 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%76 = llvm.load %75 : !llvm.ptr -> i64
%77 = arith.constant 400 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.cmpi slt, %76, %79 : i64
cf.cond_br %78, ^bb7, ^bb8
^bb7:
%80 = llvm.load %71 : !llvm.ptr -> f64
%81 = arith.constant 2.0 : f32
%83 = arith.extf %81 : f32 to f64
%82 = arith.mulf %80, %83 : f64
llvm.store %82, %71 : f64, !llvm.ptr
%84 = llvm.load %75 : !llvm.ptr -> i64
%85 = arith.constant 1 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.addi %84, %87 : i64
llvm.store %86, %75 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%88 = llvm.mlir.constant(1 : i64) : i64
%89 = llvm.alloca %88 x f64 : (i64) -> !llvm.ptr
llvm.store %20, %89 : f64, !llvm.ptr
%90 = arith.constant 0 : i32
%91 = arith.extsi %90 : i32 to i64
%92 = llvm.mlir.constant(1 : i64) : i64
%93 = llvm.alloca %92 x i64 : (i64) -> !llvm.ptr
llvm.store %91, %93 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%94 = llvm.load %93 : !llvm.ptr -> i64
%95 = arith.constant 4 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.cmpi slt, %94, %97 : i64
cf.cond_br %96, ^bb10, ^bb11
^bb10:
%98 = llvm.load %89 : !llvm.ptr -> f64
%99 = llvm.load %89 : !llvm.ptr -> f64
%100 = arith.mulf %98, %99 : f64
llvm.store %100, %89 : f64, !llvm.ptr
%101 = llvm.load %93 : !llvm.ptr -> i64
%102 = arith.constant 1 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.addi %101, %104 : i64
llvm.store %103, %93 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%105 = llvm.load %51 : !llvm.ptr -> f64
%106 = llvm.load %24 : !llvm.ptr -> f64
%107 = arith.mulf %105, %106 : f64
%108 = llvm.load %71 : !llvm.ptr -> f64
%109 = arith.mulf %107, %108 : f64
%110 = llvm.load %89 : !llvm.ptr -> f64
%111 = arith.divf %109, %110 : f64
%112 = arith.constant 3617.0 : f32
%113 = arith.constant 16320.0 : f32
%114 = arith.divf %112, %113 : f32
%116 = arith.extf %114 : f32 to f64
%115 = arith.subf %111, %116 : f64
%117 = llvm.mlir.constant(1 : i64) : i64
%118 = llvm.alloca %117 x f64 : (i64) -> !llvm.ptr
llvm.store %115, %118 : f64, !llvm.ptr
%119 = arith.constant 0 : i32
%120 = arith.extsi %119 : i32 to i64
%121 = llvm.mlir.constant(1 : i64) : i64
%122 = llvm.alloca %121 x i64 : (i64) -> !llvm.ptr
llvm.store %120, %122 : i64, !llvm.ptr
%123 = arith.constant 10000000000000000000000.0 : f32
%124 = arith.extf %123 : f32 to f64
cf.br ^bb12
^bb12:
%125 = llvm.load %118 : !llvm.ptr -> f64
%126 = arith.cmpf oge, %125, %124 : f64
cf.cond_br %126, ^bb13, ^bb14
^bb13:
%127 = llvm.load %118 : !llvm.ptr -> f64
%128 = arith.divf %127, %124 : f64
llvm.store %128, %118 : f64, !llvm.ptr
%129 = llvm.load %122 : !llvm.ptr -> i64
%130 = arith.constant 22 : i32
%132 = arith.extsi %130 : i32 to i64
%131 = arith.addi %129, %132 : i64
llvm.store %131, %122 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
cf.br ^bb15
^bb15:
%133 = llvm.load %118 : !llvm.ptr -> f64
%134 = arith.constant 10.0 : f32
%136 = arith.extf %134 : f32 to f64
%135 = arith.cmpf oge, %133, %136 : f64
cf.cond_br %135, ^bb16, ^bb17
^bb16:
%137 = llvm.load %118 : !llvm.ptr -> f64
%138 = arith.constant 10.0 : f32
%140 = arith.extf %138 : f32 to f64
%139 = arith.divf %137, %140 : f64
llvm.store %139, %118 : f64, !llvm.ptr
%141 = llvm.load %122 : !llvm.ptr -> i64
%142 = arith.constant 1 : i32
%144 = arith.extsi %142 : i32 to i64
%143 = arith.addi %141, %144 : i64
llvm.store %143, %122 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
cf.br ^bb18
^bb18:
%145 = llvm.load %118 : !llvm.ptr -> f64
%146 = arith.constant 1.0 : f32
%148 = arith.extf %146 : f32 to f64
%147 = arith.cmpf olt, %145, %148 : f64
cf.cond_br %147, ^bb19, ^bb20
^bb19:
%149 = llvm.load %118 : !llvm.ptr -> f64
%150 = arith.constant 10.0 : f32
%152 = arith.extf %150 : f32 to f64
%151 = arith.mulf %149, %152 : f64
llvm.store %151, %118 : f64, !llvm.ptr
%153 = llvm.load %122 : !llvm.ptr -> i64
%154 = arith.constant 1 : i32
%156 = arith.extsi %154 : i32 to i64
%155 = arith.subi %153, %156 : i64
llvm.store %155, %122 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%157 = llvm.mlir.addressof @str_0 : !llvm.ptr
%158 = llvm.load %118 : !llvm.ptr -> f64
%159 = llvm.load %122 : !llvm.ptr -> i64
%160 = llvm.call @printf(%157, %158, %159) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64, i64) -> i32
%161 = arith.constant 0 : i32
func.return %161 : i32
}
}