Problem 503

Compromise or Persist — F(10^6) via backward rank-threshold DP.

Answer3.8694550145
Output3.8694550145
StatusPASS
Native helperno
Runtime10 ms
Peak memory1152 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)?
Space complexityO(1)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 503
# Compromise or Persist — F(10^6) via backward rank-threshold DP.

function main() -> i32 {
    let N: i32 = 1000000
    let mut x_next: f64 = 0.5
    let mut t: i32 = N - 1
    while t >= 1 {
        let tp1: f64 = (t + 1) as f64
        let y: f64 = x_next
        let mut K: i32 = (y * tp1) as i32
        if K > t { K = t }
        while K > 0 && ((K as f64) / tp1) > y + 1.0e-15 {
            K = K - 1
        }
        while K < t && (((K + 1) as f64) / tp1) <= y - 1.0e-15 {
            K = K + 1
        }
        let sumk: f64 = (K as f64) * ((K + 1) as f64) / 2.0
        let x_t: f64 = (sumk / tp1 + ((t - K) as f64) * y) / (t as f64)
        x_next = x_t
        t = t - 1
    }
    let ans: f64 = x_next * ((N + 1) as f64)
    printf("%.10f\n", ans)
    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) {
    int32_t N = 1000000;
    double x_next = 0.5;
    int32_t t = (N - 1);
    while (t >= 1) {
        double tp1 = ((double)((t + 1)));
        double y = x_next;
        int32_t K = ((int32_t)((y * tp1)));
        if (K > t) {
            K = t;
        }
        while ((K > 0 && (((double)(K)) / tp1) > (y + 1.0e-15))) {
            K = (K - 1);
        }
        while ((K < t && (((double)((K + 1))) / tp1) <= (y - 1.0e-15))) {
            K = (K + 1);
        }
        double sumk = ((((double)(K)) * ((double)((K + 1)))) / 2.0);
        double x_t = (((sumk / tp1) + (((double)((t - K))) * y)) / ((double)(t)));
        x_next = x_t;
        t = (t - 1);
    }
    double ans = (x_next * ((double)((N + 1))));
    printf("%.10f\n", ans);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.10f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
  func.func @main() -> i32 {
    %0 = arith.constant 1000000 : i32
    %1 = arith.constant 0.5 : f32
    %2 = arith.extf %1 : f32 to f64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x f64 : (i64) -> !llvm.ptr
    llvm.store %2, %4 : f64, !llvm.ptr
    %5 = arith.constant 1 : i32
    %6 = arith.subi %0, %5 : i32
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i32 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %8 : !llvm.ptr -> i32
    %10 = arith.constant 1 : i32
    %11 = arith.cmpi sge, %9, %10 : i32
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %12 = llvm.load %8 : !llvm.ptr -> i32
      %13 = arith.constant 1 : i32
      %14 = arith.addi %12, %13 : i32
      %15 = arith.sitofp %14 : i32 to f64
      %16 = llvm.load %4 : !llvm.ptr -> f64
      %17 = arith.mulf %16, %15 : f64
      %18 = arith.fptosi %17 : f64 to i32
      %19 = llvm.mlir.constant(1 : i64) : i64
      %20 = llvm.alloca %19 x i32 : (i64) -> !llvm.ptr
      llvm.store %18, %20 : i32, !llvm.ptr
      %21 = llvm.load %20 : !llvm.ptr -> i32
      %22 = llvm.load %8 : !llvm.ptr -> i32
      %23 = arith.cmpi sgt, %21, %22 : i32
      cf.cond_br %23, ^bb3, ^bb4
      ^bb3:
        %24 = llvm.load %8 : !llvm.ptr -> i32
        llvm.store %24, %20 : i32, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      cf.br ^bb6
      ^bb6:
      %25 = llvm.load %20 : !llvm.ptr -> i32
      %26 = arith.constant 0 : i32
      %27 = arith.cmpi sgt, %25, %26 : i32
      %28 = scf.if %27 -> (i1) {
        %29 = llvm.load %20 : !llvm.ptr -> i32
        %30 = arith.sitofp %29 : i32 to f64
        %31 = arith.divf %30, %15 : f64
        %32 = arith.constant 0 : f32
        %34 = arith.extf %32 : f32 to f64
        %33 = arith.addf %16, %34 : f64
        %35 = arith.cmpf ogt, %31, %33 : f64
        scf.yield %35 : i1
      } else {
        %36 = arith.constant false
        scf.yield %36 : i1
      }
      cf.cond_br %28, ^bb7, ^bb8
      ^bb7:
        %37 = llvm.load %20 : !llvm.ptr -> i32
        %38 = arith.constant 1 : i32
        %39 = arith.subi %37, %38 : i32
        llvm.store %39, %20 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      cf.br ^bb9
      ^bb9:
      %40 = llvm.load %20 : !llvm.ptr -> i32
      %41 = llvm.load %8 : !llvm.ptr -> i32
      %42 = arith.cmpi slt, %40, %41 : i32
      %43 = scf.if %42 -> (i1) {
        %44 = llvm.load %20 : !llvm.ptr -> i32
        %45 = arith.constant 1 : i32
        %46 = arith.addi %44, %45 : i32
        %47 = arith.sitofp %46 : i32 to f64
        %48 = arith.divf %47, %15 : f64
        %49 = arith.constant 0 : f32
        %51 = arith.extf %49 : f32 to f64
        %50 = arith.subf %16, %51 : f64
        %52 = arith.cmpf ole, %48, %50 : f64
        scf.yield %52 : i1
      } else {
        %53 = arith.constant false
        scf.yield %53 : i1
      }
      cf.cond_br %43, ^bb10, ^bb11
      ^bb10:
        %54 = llvm.load %20 : !llvm.ptr -> i32
        %55 = arith.constant 1 : i32
        %56 = arith.addi %54, %55 : i32
        llvm.store %56, %20 : i32, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %57 = llvm.load %20 : !llvm.ptr -> i32
      %58 = arith.sitofp %57 : i32 to f64
      %59 = llvm.load %20 : !llvm.ptr -> i32
      %60 = arith.constant 1 : i32
      %61 = arith.addi %59, %60 : i32
      %62 = arith.sitofp %61 : i32 to f64
      %63 = arith.mulf %58, %62 : f64
      %64 = arith.constant 2.0 : f32
      %66 = arith.extf %64 : f32 to f64
      %65 = arith.divf %63, %66 : f64
      %67 = arith.divf %65, %15 : f64
      %68 = llvm.load %8 : !llvm.ptr -> i32
      %69 = llvm.load %20 : !llvm.ptr -> i32
      %70 = arith.subi %68, %69 : i32
      %71 = arith.sitofp %70 : i32 to f64
      %72 = arith.mulf %71, %16 : f64
      %73 = arith.addf %67, %72 : f64
      %74 = llvm.load %8 : !llvm.ptr -> i32
      %75 = arith.sitofp %74 : i32 to f64
      %76 = arith.divf %73, %75 : f64
      llvm.store %76, %4 : f64, !llvm.ptr
      %77 = llvm.load %8 : !llvm.ptr -> i32
      %78 = arith.constant 1 : i32
      %79 = arith.subi %77, %78 : i32
      llvm.store %79, %8 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %80 = llvm.load %4 : !llvm.ptr -> f64
    %81 = arith.constant 1 : i32
    %82 = arith.addi %0, %81 : i32
    %83 = arith.sitofp %82 : i32 to f64
    %84 = arith.mulf %80, %83 : f64
    %85 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %86 = llvm.call @printf(%85, %84) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %87 = arith.constant 0 : i32
    func.return %87 : i32
  }
}