Problem 444
S_20(10^14) = C(N+k,k)*(H_{N+k}-H_k) in scientific notation.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | ? |
| Space complexity | O(1) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 444
# S_20(10^14) = C(N+k,k)*(H_{N+k}-H_k) in scientific notation.
extern {
function log(x: f64) -> f64
function pow(x: f64, y: f64) -> f64
}
function main() -> i32 {
let N: f64 = 100000000000000.0
let K: i32 = 20
let mut log_binom: f64 = 0.0
let mut i: i32 = 1
while i <= K {
log_binom = log_binom + log(N + (i as f64)) - log(i as f64)
i = i + 1
}
let GAMMA: f64 = 0.5772156649015328606
let n1: f64 = N + (K as f64)
let inv: f64 = 1.0 / n1
let inv2: f64 = inv * inv
let inv4: f64 = inv2 * inv2
let inv6: f64 = inv4 * inv2
let inv8: f64 = inv4 * inv4
let inv10: f64 = inv8 * inv2
let Hnk: f64 = log(n1) + GAMMA + inv / 2.0 - inv2 / 12.0 + inv4 / 120.0 - inv6 / 252.0 + inv8 / 240.0 - 5.0 * inv10 / 660.0
let H20: f64 = 3.597739657143682
let diff: f64 = Hnk - H20
let log_ans: f64 = log_binom + log(diff)
let LN10: f64 = 2.302585092994046
let log10_ans: f64 = log_ans / LN10
let mut exp10: i64 = log10_ans as i64
if (exp10 as f64) > log10_ans {
exp10 = exp10 - 1
}
let mant: f64 = pow(10.0, log10_ans - (exp10 as f64))
let scaled: f64 = mant * 1000000000.0 + 0.5
let mut iscaled: i64 = scaled as i64
if iscaled >= 10000000000 {
iscaled = iscaled / 10
exp10 = exp10 + 1
}
let whole: i64 = iscaled / 1000000000
let frac: i64 = iscaled % 1000000000
printf("%lld.%09llde%lld\n", whole, frac, exp10)
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 N = 100000000000000.0;
int32_t K = 20;
double log_binom = 0.0;
int32_t i = 1;
while (i <= K) {
log_binom = ((log_binom + log((N + ((double)(i))))) - log(((double)(i))));
i = (i + 1);
}
double GAMMA = 0.5772156649015328606;
double n1 = (N + ((double)(K)));
double inv = (1.0 / n1);
double inv2 = (inv * inv);
double inv4 = (inv2 * inv2);
double inv6 = (inv4 * inv2);
double inv8 = (inv4 * inv4);
double inv10 = (inv8 * inv2);
double Hnk = (((((((log(n1) + GAMMA) + (inv / 2.0)) - (inv2 / 12.0)) + (inv4 / 120.0)) - (inv6 / 252.0)) + (inv8 / 240.0)) - ((5.0 * inv10) / 660.0));
double H20 = 3.597739657143682;
double diff = (Hnk - H20);
double log_ans = (log_binom + log(diff));
double LN10 = 2.302585092994046;
double log10_ans = (log_ans / LN10);
int64_t exp10 = ((int64_t)(log10_ans));
if (((double)(exp10)) > log10_ans) {
exp10 = (exp10 - 1);
}
double mant = pow(10.0, (log10_ans - ((double)(exp10))));
double scaled = ((mant * 1000000000.0) + 0.5);
int64_t iscaled = ((int64_t)(scaled));
if (iscaled >= 10000000000) {
iscaled = FLOW_CHECKED_DIV((iscaled), (10));
exp10 = (exp10 + 1);
}
int64_t whole = FLOW_CHECKED_DIV((iscaled), (1000000000));
int64_t frac = FLOW_CHECKED_MOD((iscaled), (1000000000));
printf("%lld.%09llde%lld\n", whole, frac, exp10);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld.%09llde%lld\n\00") {addr_space = 0 : i32} : !llvm.array<18 x i8>
func.func private @log(f64) -> f64
func.func private @pow(f64, f64) -> f64
func.func @main() -> i32 {
%0 = arith.constant 100000000000000.0 : f32
%1 = arith.extf %0 : f32 to f64
%2 = arith.constant 20 : i32
%3 = arith.constant 0.0 : f32
%4 = arith.extf %3 : f32 to f64
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x f64 : (i64) -> !llvm.ptr
llvm.store %4, %6 : f64, !llvm.ptr
%7 = arith.constant 1 : i32
%8 = llvm.mlir.constant(1 : i64) : i64
%9 = llvm.alloca %8 x i32 : (i64) -> !llvm.ptr
llvm.store %7, %9 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%10 = llvm.load %9 : !llvm.ptr -> i32
%11 = arith.cmpi sle, %10, %2 : i32
cf.cond_br %11, ^bb1, ^bb2
^bb1:
%12 = llvm.load %6 : !llvm.ptr -> f64
%13 = llvm.load %9 : !llvm.ptr -> i32
%14 = arith.sitofp %13 : i32 to f64
%15 = arith.addf %1, %14 : f64
%16 = math.log %15 : f64
%17 = arith.addf %12, %16 : f64
%18 = llvm.load %9 : !llvm.ptr -> i32
%19 = arith.sitofp %18 : i32 to f64
%20 = math.log %19 : f64
%21 = arith.subf %17, %20 : f64
llvm.store %21, %6 : f64, !llvm.ptr
%22 = llvm.load %9 : !llvm.ptr -> i32
%23 = arith.constant 1 : i32
%24 = arith.addi %22, %23 : i32
llvm.store %24, %9 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
%25 = arith.constant 0.5772156649015328606 : f32
%26 = arith.extf %25 : f32 to f64
%27 = arith.sitofp %2 : i32 to f64
%28 = arith.addf %1, %27 : f64
%29 = arith.constant 1.0 : f32
%31 = arith.extf %29 : f32 to f64
%30 = arith.divf %31, %28 : f64
%32 = arith.mulf %30, %30 : f64
%33 = arith.mulf %32, %32 : f64
%34 = arith.mulf %33, %32 : f64
%35 = arith.mulf %33, %33 : f64
%36 = arith.mulf %35, %32 : f64
%37 = math.log %28 : f64
%38 = arith.addf %37, %26 : f64
%39 = arith.constant 2.0 : f32
%41 = arith.extf %39 : f32 to f64
%40 = arith.divf %30, %41 : f64
%42 = arith.addf %38, %40 : f64
%43 = arith.constant 12.0 : f32
%45 = arith.extf %43 : f32 to f64
%44 = arith.divf %32, %45 : f64
%46 = arith.subf %42, %44 : f64
%47 = arith.constant 120.0 : f32
%49 = arith.extf %47 : f32 to f64
%48 = arith.divf %33, %49 : f64
%50 = arith.addf %46, %48 : f64
%51 = arith.constant 252.0 : f32
%53 = arith.extf %51 : f32 to f64
%52 = arith.divf %34, %53 : f64
%54 = arith.subf %50, %52 : f64
%55 = arith.constant 240.0 : f32
%57 = arith.extf %55 : f32 to f64
%56 = arith.divf %35, %57 : f64
%58 = arith.addf %54, %56 : f64
%59 = arith.constant 5.0 : f32
%61 = arith.extf %59 : f32 to f64
%60 = arith.mulf %61, %36 : f64
%62 = arith.constant 660.0 : f32
%64 = arith.extf %62 : f32 to f64
%63 = arith.divf %60, %64 : f64
%65 = arith.subf %58, %63 : f64
%66 = arith.constant 3.597739657143682 : f32
%67 = arith.extf %66 : f32 to f64
%68 = arith.subf %65, %67 : f64
%69 = llvm.load %6 : !llvm.ptr -> f64
%70 = math.log %68 : f64
%71 = arith.addf %69, %70 : f64
%72 = arith.constant 2.302585092994046 : f32
%73 = arith.extf %72 : f32 to f64
%74 = arith.divf %71, %73 : f64
%75 = arith.fptosi %74 : f64 to i64
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
llvm.store %75, %77 : i64, !llvm.ptr
%78 = llvm.load %77 : !llvm.ptr -> i64
%79 = arith.sitofp %78 : i64 to f64
%80 = arith.cmpf ogt, %79, %74 : f64
cf.cond_br %80, ^bb3, ^bb4
^bb3:
%81 = llvm.load %77 : !llvm.ptr -> i64
%82 = arith.constant 1 : i32
%84 = arith.extsi %82 : i32 to i64
%83 = arith.subi %81, %84 : i64
llvm.store %83, %77 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%86 = arith.constant 10.0 : f32
%87 = llvm.load %77 : !llvm.ptr -> i64
%88 = arith.sitofp %87 : i64 to f64
%89 = arith.subf %74, %88 : f64
%90 = arith.extf %86 : f32 to f64
%85 = func.call @pow(%90, %89) : (f64, f64) -> f64
%91 = arith.constant 1000000000.0 : f32
%93 = arith.extf %91 : f32 to f64
%92 = arith.mulf %85, %93 : f64
%94 = arith.constant 0.5 : f32
%96 = arith.extf %94 : f32 to f64
%95 = arith.addf %92, %96 : f64
%97 = arith.fptosi %95 : f64 to i64
%98 = llvm.mlir.constant(1 : i64) : i64
%99 = llvm.alloca %98 x i64 : (i64) -> !llvm.ptr
llvm.store %97, %99 : i64, !llvm.ptr
%100 = llvm.load %99 : !llvm.ptr -> i64
%101 = arith.constant 5705032704 : i32
%103 = arith.extsi %101 : i32 to i64
%102 = arith.cmpi sge, %100, %103 : i64
cf.cond_br %102, ^bb6, ^bb7
^bb6:
%104 = llvm.load %99 : !llvm.ptr -> i64
%105 = arith.constant 10 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.divsi %104, %107 : i64
llvm.store %106, %99 : i64, !llvm.ptr
%108 = llvm.load %77 : !llvm.ptr -> i64
%109 = arith.constant 1 : i32
%111 = arith.extsi %109 : i32 to i64
%110 = arith.addi %108, %111 : i64
llvm.store %110, %77 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%112 = llvm.load %99 : !llvm.ptr -> i64
%113 = arith.constant 1000000000 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.divsi %112, %115 : i64
%116 = llvm.load %99 : !llvm.ptr -> i64
%117 = arith.constant 1000000000 : i32
%119 = arith.extsi %117 : i32 to i64
%118 = arith.remsi %116, %119 : i64
%120 = llvm.mlir.addressof @str_0 : !llvm.ptr
%121 = llvm.load %77 : !llvm.ptr -> i64
%122 = llvm.call @printf(%120, %114, %118, %121) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64, i64, i64) -> i32
%123 = arith.constant 0 : i32
func.return %123 : i32
}
}