# Project Euler 406
# Sum C(10^12, sqrt(k), sqrt(F_k)) for k=1..30, 8 decimals.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
}
function max_numbers(t: f64, a: f64, b: f64, cap: i64, memo: ptr<i64>, stride: i64, maxi: i64, maxj: i64) -> i64 {
# iterative DP bottom-up: dp[i][j] for i=0..maxi, j=0..maxj
# Fill from large i+j downward
let mut i: i64 = maxi
while i >= 0 {
let mut j: i64 = maxj
while j >= 0 {
let rem: f64 = t - (i as f64) * a - (j as f64) * b
if rem < 0.0 {
memo[i * stride + j] = 0
} else {
let mut n: i64 = 1
if i + 1 <= maxi {
n = n + memo[(i + 1) * stride + j]
}
if j + 1 <= maxj {
n = n + memo[i * stride + (j + 1)]
}
if n > cap { n = cap }
memo[i * stride + j] = n
}
j = j - 1
}
i = i - 1
}
return memo[0]
}
function min_cost(n: i64, a: f64, b: f64) -> f64 {
if n <= 1 { return 0.0 }
let mut hi: f64 = 1.0
let stride: i64 = 5000
let memo: ptr<i64> = calloc(5000 * 5000, 8)
if memo == null { return -1.0 }
while true {
let mut maxi: i64 = (hi / a) as i64 + 2
let mut maxj: i64 = (hi / b) as i64 + 2
if maxi >= 4990 { maxi = 4990 }
if maxj >= 4990 { maxj = 4990 }
if max_numbers(hi, a, b, n, memo, stride, maxi, maxj) >= n {
break
}
hi = hi * 2.0
if hi > 1.0e7 {
break
}
}
let mut lo: f64 = 0.0
let mut it: i32 = 0
while it < 80 {
let mid: f64 = (lo + hi) / 2.0
let mut maxi: i64 = (mid / a) as i64 + 2
let mut maxj: i64 = (mid / b) as i64 + 2
if maxi >= 4990 { maxi = 4990 }
if maxj >= 4990 { maxj = 4990 }
if max_numbers(mid, a, b, n, memo, stride, maxi, maxj) >= n {
hi = mid
} else {
lo = mid
}
it = it + 1
}
free(memo)
return hi
}
function main() -> i32 {
let fib: array<i64, 31> = [
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025,
121393, 196418, 317811, 514229, 832040
]
let N: i64 = 1000000000000
let mut total: f64 = 0.0
let mut k: i32 = 1
while k <= 30 {
let a: f64 = sqrt(k as f64)
let b: f64 = sqrt(fib[k] as f64)
total = total + min_cost(N, a, b)
k = k + 1
}
printf("%.8f\n", total)
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 max_numbers_f64_f64_f64_i64_ptr_i64_i64_i64_i64(double t, double a, double b, int64_t cap, int64_t* memo, int64_t stride, int64_t maxi, int64_t maxj);
double min_cost_i64_f64_f64(int64_t n, double a, double b);
int32_t main(void);
int64_t max_numbers_f64_f64_f64_i64_ptr_i64_i64_i64_i64(double t, double a, double b, int64_t cap, int64_t* memo, int64_t stride, int64_t maxi, int64_t maxj) {
int64_t i = maxi;
while (i >= 0) {
int64_t j = maxj;
while (j >= 0) {
double rem = ((t - (((double)(i)) * a)) - (((double)(j)) * b));
if (rem < 0.0) {
memo[((i * stride) + j)] = 0;
} else {
int64_t n = 1;
if ((i + 1) <= maxi) {
n = (n + memo[(((i + 1) * stride) + j)]);
}
if ((j + 1) <= maxj) {
n = (n + memo[((i * stride) + (j + 1))]);
}
if (n > cap) {
n = cap;
}
memo[((i * stride) + j)] = n;
}
j = (j - 1);
}
i = (i - 1);
}
return memo[0];
}
double min_cost_i64_f64_f64(int64_t n, double a, double b) {
if (n <= 1) {
return 0.0;
}
double hi = 1.0;
int64_t stride = 5000;
int64_t* memo = (int64_t*)(calloc((5000 * 5000), 8));
if (memo == NULL) {
return (-1.0);
}
while (1) {
int64_t maxi = (((int64_t)((hi / a))) + 2);
int64_t maxj = (((int64_t)((hi / b))) + 2);
if (maxi >= 4990) {
maxi = 4990;
}
if (maxj >= 4990) {
maxj = 4990;
}
if (max_numbers_f64_f64_f64_i64_ptr_i64_i64_i64_i64(hi, a, b, n, memo, stride, maxi, maxj) >= n) {
break;
}
hi = (hi * 2.0);
if (hi > 1.0e7) {
break;
}
}
double lo = 0.0;
int32_t it = 0;
while (it < 80) {
double mid = ((lo + hi) / 2.0);
int64_t maxi = (((int64_t)((mid / a))) + 2);
int64_t maxj = (((int64_t)((mid / b))) + 2);
if (maxi >= 4990) {
maxi = 4990;
}
if (maxj >= 4990) {
maxj = 4990;
}
if (max_numbers_f64_f64_f64_i64_ptr_i64_i64_i64_i64(mid, a, b, n, memo, stride, maxi, maxj) >= n) {
hi = mid;
} else {
lo = mid;
}
it = (it + 1);
}
free(memo);
return hi;
}
int32_t main(void) {
int64_t fib[31] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040 };
int64_t N = 1000000000000;
double total = 0.0;
int32_t k = 1;
while (k <= 30) {
double a = sqrt(((double)(k)));
double b = sqrt(((double)((((unsigned)(k) < 31) ? fib[k] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(k), 31), flow_fault_handler("array index out of bounds"), fib[0])))));
total = (total + min_cost_i64_f64_f64(N, a, b));
k = (k + 1);
}
printf("%.8f\n", total);
return 0;
}