# Project Euler 065
# Find the sum of digits in the numerator of the 100th convergent of e.
# e = [2; 1,2,1, 1,4,1, 1,6,1, ..., 1,2k,1, ...]
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function max2i(a: i32, b: i32) -> i32 {
if a > b { return a }
return b
}
function add(a: ptr<i32>, alen: i32, b: ptr<i32>, blen: i32, out: ptr<i32>) -> i32 {
let mut carry: i32 = 0
let mut i: i32 = 0
let maxlen: i32 = max2i(alen, blen)
while i < maxlen || carry > 0 {
let mut v: i32 = carry
if i < alen { v = v + a[i] }
if i < blen { v = v + b[i] }
out[i] = v % 10
carry = v / 10
i = i + 1
}
return i
}
function mul_small(digits: ptr<i32>, len: i32, m: i64, out: ptr<i32>) -> i32 {
let mut carry: i64 = 0
let mut i: i32 = 0
while i < len {
let v: i64 = (digits[i] as i64) * m + carry
out[i] = (v % 10) as i32
carry = v / 10
i = i + 1
}
while carry > 0 {
out[i] = (carry % 10) as i32
carry = carry / 10
i = i + 1
}
return i
}
function copy_digits(src: ptr<i32>, dst: ptr<i32>, len: i32) -> void {
let mut i: i32 = 0
while i < len {
dst[i] = src[i]
i = i + 1
}
}
function e_term(k: i32) -> i64 {
# 0-indexed terms: a0=2, then 1,2,1, 1,4,1, ...
if k == 0 { return 2 }
if k % 3 == 2 {
return 2 * ((k / 3) as i64 + 1)
}
return 1
}
function main() -> i32 {
let width: i32 = 100
let n0: ptr<i32> = calloc(width as i64, 4)
let n1: ptr<i32> = calloc(width as i64, 4)
let n2: ptr<i32> = calloc(width as i64, 4)
let tmp: ptr<i32> = calloc(width as i64, 4)
if n0 == null || n1 == null || n2 == null || tmp == null { return 1 }
# convergents: h_{-1}=1,h_0=a0; h_n = a_n*h_{n-1} + h_{n-2}
# Start with h-2=0,h-1=1 then iterate — for numerator only:
# n_{-2}=0, n_{-1}=1
# Actually: n-1=1, n0=a0
n0[0] = 1 # n_{-1}
let mut len0: i32 = 1
n1[0] = 2 # n_0 = a0
let mut len1: i32 = 1
let mut k: i32 = 1
while k < 100 {
let a: i64 = e_term(k)
let mlen: i32 = mul_small(n1, len1, a, tmp)
let len2: i32 = add(tmp, mlen, n0, len0, n2)
copy_digits(n1, n0, len1)
len0 = len1
copy_digits(n2, n1, len2)
len1 = len2
k = k + 1
}
let mut total: i64 = 0
let mut i: i32 = 0
while i < len1 {
total = total + (n1[i] as i64)
i = i + 1
}
printf("%lld\n", total)
free(tmp)
free(n2)
free(n1)
free(n0)
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 max2i_i32_i32(int32_t a, int32_t b);
int32_t add_ptr_i32_i32_ptr_i32_i32_ptr_i32(int32_t* a, int32_t alen, int32_t* b, int32_t blen, int32_t* out);
int32_t mul_small_ptr_i32_i32_i64_ptr_i32(int32_t* digits, int32_t len, int64_t m, int32_t* out);
void copy_digits_ptr_i32_ptr_i32_i32(int32_t* src, int32_t* dst, int32_t len);
int64_t e_term_i32(int32_t k);
int32_t main(void);
int32_t max2i_i32_i32(int32_t a, int32_t b) {
if (a > b) {
return a;
}
return b;
}
int32_t add_ptr_i32_i32_ptr_i32_i32_ptr_i32(int32_t* a, int32_t alen, int32_t* b, int32_t blen, int32_t* out) {
int32_t carry = 0;
int32_t i = 0;
int32_t maxlen = max2i_i32_i32(alen, blen);
while ((i < maxlen || carry > 0)) {
int32_t v = carry;
if (i < alen) {
v = (v + a[i]);
}
if (i < blen) {
v = (v + b[i]);
}
out[i] = FLOW_CHECKED_MOD((v), (10));
carry = FLOW_CHECKED_DIV((v), (10));
i = (i + 1);
}
return i;
}
int32_t mul_small_ptr_i32_i32_i64_ptr_i32(int32_t* digits, int32_t len, int64_t m, int32_t* out) {
int64_t carry = 0;
int32_t i = 0;
while (i < len) {
int64_t v = ((((int64_t)(digits[i])) * m) + carry);
out[i] = ((int32_t)(FLOW_CHECKED_MOD((v), (10))));
carry = FLOW_CHECKED_DIV((v), (10));
i = (i + 1);
}
while (carry > 0) {
out[i] = ((int32_t)(FLOW_CHECKED_MOD((carry), (10))));
carry = FLOW_CHECKED_DIV((carry), (10));
i = (i + 1);
}
return i;
}
void copy_digits_ptr_i32_ptr_i32_i32(int32_t* src, int32_t* dst, int32_t len) {
int32_t i = 0;
while (i < len) {
dst[i] = src[i];
i = (i + 1);
}
}
int64_t e_term_i32(int32_t k) {
if (k == 0) {
return 2;
}
if (FLOW_CHECKED_MOD((k), (3)) == 2) {
return (2 * (((int64_t)(FLOW_CHECKED_DIV((k), (3)))) + 1));
}
return 1;
}
int32_t main(void) {
int32_t width = 100;
int32_t* n0 = (int32_t*)(calloc(((int64_t)(width)), 4));
int32_t* n1 = (int32_t*)(calloc(((int64_t)(width)), 4));
int32_t* n2 = (int32_t*)(calloc(((int64_t)(width)), 4));
int32_t* tmp = (int32_t*)(calloc(((int64_t)(width)), 4));
if ((((n0 == NULL || n1 == NULL) || n2 == NULL) || tmp == NULL)) {
return 1;
}
n0[0] = 1;
int32_t len0 = 1;
n1[0] = 2;
int32_t len1 = 1;
int32_t k = 1;
while (k < 100) {
int64_t a = e_term_i32(k);
int32_t mlen = mul_small_ptr_i32_i32_i64_ptr_i32(n1, len1, a, tmp);
int32_t len2 = add_ptr_i32_i32_ptr_i32_i32_ptr_i32(tmp, mlen, n0, len0, n2);
copy_digits_ptr_i32_ptr_i32_i32(n1, n0, len1);
len0 = len1;
copy_digits_ptr_i32_ptr_i32_i32(n2, n1, len2);
len1 = len2;
k = (k + 1);
}
int64_t total = 0;
int32_t i = 0;
while (i < len1) {
total = (total + ((int64_t)(n1[i])));
i = (i + 1);
}
printf("%lld\n", total);
free(tmp);
free(n2);
free(n1);
free(n0);
return 0;
}