← All problems
Problem 235
Find r where s(r) = -6e11 for arithmetic-geometric series.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(log n)
Space complexity O(1)O(1)
Approach Flow solution Binary search on series
Verdict Suboptimal
Flow source
# Project Euler 235
# Find r where s(r) = -6e11 for arithmetic-geometric series.
function s(r: f64) -> f64 {
let mut result: f64 = 0.0
let mut x: f64 = 1.0
let mut k: i32 = 1
while k <= 5000 {
result = result + (900.0 - 3.0 * (k as f64)) * x
x = x * r
k = k + 1
}
return result
}
function main() -> i32 {
let mut lower: f64 = 0.0
let mut upper: f64 = 2.0
let mut iter: i32 = 0
while iter < 80 {
let mid: f64 = (upper + lower) / 2.0
if s(mid) < 0.0 - 600000000000.0 {
upper = mid
} else {
lower = mid
}
iter = iter + 1
}
printf("%.12f\n", (upper + lower) / 2.0)
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 s_f64(double r);
int32_t main(void);
double s_f64(double r) {
double result = 0.0;
double x = 1.0;
int32_t k = 1;
while (k <= 5000) {
result = (result + ((900.0 - (3.0 * ((double)(k)))) * x));
x = (x * r);
k = (k + 1);
}
return result;
}
int32_t main(void) {
double lower = 0.0;
double upper = 2.0;
int32_t iter = 0;
while (iter < 80) {
double mid = ((upper + lower) / 2.0);
if (s_f64(mid) < (0.0 - 600000000000.0)) {
upper = mid;
} else {
lower = mid;
}
iter = (iter + 1);
}
printf("%.12f\n", ((upper + lower) / 2.0));
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.12f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
func.func @s(%arg0: f64) -> f64 {
%0 = arith.constant 0.0 : f32
%1 = arith.extf %0 : f32 to f64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x f64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : f64, !llvm.ptr
%4 = arith.constant 1.0 : f32
%5 = arith.extf %4 : f32 to f64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x f64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : f64, !llvm.ptr
%8 = arith.constant 1 : i32
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i32 : (i64) -> !llvm.ptr
llvm.store %8, %10 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%11 = llvm.load %10 : !llvm.ptr -> i32
%12 = arith.constant 5000 : i32
%13 = arith.cmpi sle, %11, %12 : i32
cf.cond_br %13, ^bb1, ^bb2
^bb1:
%14 = llvm.load %3 : !llvm.ptr -> f64
%15 = arith.constant 900.0 : f32
%16 = arith.constant 3.0 : f32
%17 = llvm.load %10 : !llvm.ptr -> i32
%18 = arith.sitofp %17 : i32 to f64
%20 = arith.extf %16 : f32 to f64
%19 = arith.mulf %20, %18 : f64
%22 = arith.extf %15 : f32 to f64
%21 = arith.subf %22, %19 : f64
%23 = llvm.load %7 : !llvm.ptr -> f64
%24 = arith.mulf %21, %23 : f64
%25 = arith.addf %14, %24 : f64
llvm.store %25, %3 : f64, !llvm.ptr
%26 = llvm.load %7 : !llvm.ptr -> f64
%27 = arith.mulf %26, %arg0 : f64
llvm.store %27, %7 : f64, !llvm.ptr
%28 = llvm.load %10 : !llvm.ptr -> i32
%29 = arith.constant 1 : i32
%30 = arith.addi %28, %29 : i32
llvm.store %30, %10 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
%31 = llvm.load %3 : !llvm.ptr -> f64
func.return %31 : f64
}
func.func @main() -> i32 {
%32 = arith.constant 0.0 : f32
%33 = arith.extf %32 : f32 to f64
%34 = llvm.mlir.constant(1 : i64) : i64
%35 = llvm.alloca %34 x f64 : (i64) -> !llvm.ptr
llvm.store %33, %35 : f64, !llvm.ptr
%36 = arith.constant 2.0 : f32
%37 = arith.extf %36 : f32 to f64
%38 = llvm.mlir.constant(1 : i64) : i64
%39 = llvm.alloca %38 x f64 : (i64) -> !llvm.ptr
llvm.store %37, %39 : f64, !llvm.ptr
%40 = arith.constant 0 : i32
%41 = llvm.mlir.constant(1 : i64) : i64
%42 = llvm.alloca %41 x i32 : (i64) -> !llvm.ptr
llvm.store %40, %42 : i32, !llvm.ptr
cf.br ^bb3
^bb3:
%43 = llvm.load %42 : !llvm.ptr -> i32
%44 = arith.constant 80 : i32
%45 = arith.cmpi slt, %43, %44 : i32
cf.cond_br %45, ^bb4, ^bb5
^bb4:
%46 = llvm.load %39 : !llvm.ptr -> f64
%47 = llvm.load %35 : !llvm.ptr -> f64
%48 = arith.addf %46, %47 : f64
%49 = arith.constant 2.0 : f32
%51 = arith.extf %49 : f32 to f64
%50 = arith.divf %48, %51 : f64
%52 = func.call @s(%50) : (f64) -> f64
%53 = arith.constant 0.0 : f32
%54 = arith.constant 600000000000.0 : f32
%55 = arith.subf %53, %54 : f32
%57 = arith.extf %55 : f32 to f64
%56 = arith.cmpf olt, %52, %57 : f64
cf.cond_br %56, ^bb6, ^bb7
^bb6:
llvm.store %50, %39 : f64, !llvm.ptr
cf.br ^bb8
^bb7:
llvm.store %50, %35 : f64, !llvm.ptr
cf.br ^bb8
^bb8:
%58 = llvm.load %42 : !llvm.ptr -> i32
%59 = arith.constant 1 : i32
%60 = arith.addi %58, %59 : i32
llvm.store %60, %42 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%61 = llvm.mlir.addressof @str_0 : !llvm.ptr
%62 = llvm.load %39 : !llvm.ptr -> f64
%63 = llvm.load %35 : !llvm.ptr -> f64
%64 = arith.addf %62, %63 : f64
%65 = arith.constant 2.0 : f32
%67 = arith.extf %65 : f32 to f64
%66 = arith.divf %64, %67 : f64
%68 = llvm.call @printf(%61, %66) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%69 = arith.constant 0 : i32
func.return %69 : i32
}
}