# Project Euler 080
# Digital sum of the first one hundred decimal digits of irrational sqrts.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memcpy(dst: ptr<void>, src: ptr<void>, n: i64) -> ptr<void>
}
function bi_zero(a: ptr<i32>, cap: i32) -> void {
let mut i: i32 = 0
while i < cap {
a[i] = 0
i = i + 1
}
}
function bi_from_int(a: ptr<i32>, cap: i32, v: i64) -> void {
bi_zero(a, cap)
let mut x: i64 = v
let mut i: i32 = 0
while x > 0 && i < cap {
a[i] = (x % 10) as i32
x = x / 10
i = i + 1
}
}
function bi_cmp(a: ptr<i32>, b: ptr<i32>, cap: i32) -> i32 {
let mut i: i32 = cap - 1
while i >= 0 {
if a[i] != b[i] {
if a[i] > b[i] { return 1 }
return 0 - 1
}
i = i - 1
}
return 0
}
function bi_add(a: ptr<i32>, b: ptr<i32>, cap: i32) -> void {
let mut carry: i32 = 0
let mut i: i32 = 0
while i < cap {
let v: i32 = a[i] + b[i] + carry
a[i] = v % 10
carry = v / 10
i = i + 1
}
}
function bi_sub(a: ptr<i32>, b: ptr<i32>, cap: i32) -> void {
let mut borrow: i32 = 0
let mut i: i32 = 0
while i < cap {
let v: i32 = a[i] - b[i] - borrow
if v < 0 {
a[i] = v + 10
borrow = 1
} else {
a[i] = v
borrow = 0
}
i = i + 1
}
}
function bi_mul_small(a: ptr<i32>, cap: i32, m: i32) -> void {
let mut carry: i32 = 0
let mut i: i32 = 0
while i < cap {
let v: i32 = a[i] * m + carry
a[i] = v % 10
carry = v / 10
i = i + 1
}
}
function bi_mul10(a: ptr<i32>, cap: i32) -> void {
let mut i: i32 = cap - 1
while i > 0 {
a[i] = a[i - 1]
i = i - 1
}
a[0] = 0
}
function is_square(n: i32) -> bool {
let mut r: i32 = 1
while r * r < n {
r = r + 1
}
return r * r == n
}
function digit_step(rem: ptr<i32>, root: ptr<i32>, tmp: ptr<i32>, trial: ptr<i32>, cap: i32) -> i32 {
# find largest x in 0..9: (20*root+x)*x <= rem; update rem/root; return x
let mut x: i32 = 9
while x >= 0 {
memcpy(tmp, root, (cap as i64) * 4)
bi_mul_small(tmp, cap, 20)
bi_from_int(trial, cap, x as i64)
bi_add(tmp, trial, cap)
bi_mul_small(tmp, cap, x)
if bi_cmp(tmp, rem, cap) <= 0 {
break
}
x = x - 1
}
memcpy(tmp, root, (cap as i64) * 4)
bi_mul_small(tmp, cap, 20)
bi_from_int(trial, cap, x as i64)
bi_add(tmp, trial, cap)
bi_mul_small(tmp, cap, x)
bi_sub(rem, tmp, cap)
bi_mul10(root, cap)
bi_from_int(trial, cap, x as i64)
bi_add(root, trial, cap)
return x
}
function main() -> i32 {
let digits: i32 = 100
let cap: i32 = digits + 5
let rem: ptr<i32> = calloc(cap as i64, 4)
let root: ptr<i32> = calloc(cap as i64, 4)
let tmp: ptr<i32> = calloc(cap as i64, 4)
let trial: ptr<i32> = calloc(cap as i64, 4)
if rem == null || root == null || tmp == null || trial == null { return 1 }
let mut total: i64 = 0
let mut n: i32 = 2
while n <= 100 {
if !is_square(n) {
bi_zero(rem, cap)
bi_zero(root, cap)
# first pair is n itself (n < 100 ⇒ one integer pair)
bi_from_int(rem, cap, n as i64)
let mut sum: i64 = 0
let mut d: i32 = 0
while d < digits {
let x: i32 = digit_step(rem, root, tmp, trial, cap)
sum = sum + (x as i64)
d = d + 1
if d < digits {
bi_mul_small(rem, cap, 100)
}
}
total = total + sum
}
n = n + 1
}
printf("%lld\n", total)
free(rem)
free(root)
free(tmp)
free(trial)
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; }
void bi_zero_ptr_i32_i32(int32_t* a, int32_t cap);
void bi_from_int_ptr_i32_i32_i64(int32_t* a, int32_t cap, int64_t v);
int32_t bi_cmp_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap);
void bi_add_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap);
void bi_sub_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap);
void bi_mul_small_ptr_i32_i32_i32(int32_t* a, int32_t cap, int32_t m);
void bi_mul10_ptr_i32_i32(int32_t* a, int32_t cap);
bool is_square_i32(int32_t n);
int32_t digit_step_ptr_i32_ptr_i32_ptr_i32_ptr_i32_i32(int32_t* rem, int32_t* root, int32_t* tmp, int32_t* trial, int32_t cap);
int32_t main(void);
void bi_zero_ptr_i32_i32(int32_t* a, int32_t cap) {
int32_t i = 0;
while (i < cap) {
a[i] = 0;
i = (i + 1);
}
}
void bi_from_int_ptr_i32_i32_i64(int32_t* a, int32_t cap, int64_t v) {
bi_zero_ptr_i32_i32(a, cap);
int64_t x = v;
int32_t i = 0;
while ((x > 0 && i < cap)) {
a[i] = ((int32_t)(FLOW_CHECKED_MOD((x), (10))));
x = FLOW_CHECKED_DIV((x), (10));
i = (i + 1);
}
}
int32_t bi_cmp_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap) {
int32_t i = (cap - 1);
while (i >= 0) {
if (a[i] != b[i]) {
if (a[i] > b[i]) {
return 1;
}
return (0 - 1);
}
i = (i - 1);
}
return 0;
}
void bi_add_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap) {
int32_t carry = 0;
int32_t i = 0;
while (i < cap) {
int32_t v = ((a[i] + b[i]) + carry);
a[i] = FLOW_CHECKED_MOD((v), (10));
carry = FLOW_CHECKED_DIV((v), (10));
i = (i + 1);
}
}
void bi_sub_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap) {
int32_t borrow = 0;
int32_t i = 0;
while (i < cap) {
int32_t v = ((a[i] - b[i]) - borrow);
if (v < 0) {
a[i] = (v + 10);
borrow = 1;
} else {
a[i] = v;
borrow = 0;
}
i = (i + 1);
}
}
void bi_mul_small_ptr_i32_i32_i32(int32_t* a, int32_t cap, int32_t m) {
int32_t carry = 0;
int32_t i = 0;
while (i < cap) {
int32_t v = ((a[i] * m) + carry);
a[i] = FLOW_CHECKED_MOD((v), (10));
carry = FLOW_CHECKED_DIV((v), (10));
i = (i + 1);
}
}
void bi_mul10_ptr_i32_i32(int32_t* a, int32_t cap) {
int32_t i = (cap - 1);
while (i > 0) {
a[i] = a[(i - 1)];
i = (i - 1);
}
a[0] = 0;
}
bool is_square_i32(int32_t n) {
int32_t r = 1;
while ((r * r) < n) {
r = (r + 1);
}
return (r * r) == n;
}
int32_t digit_step_ptr_i32_ptr_i32_ptr_i32_ptr_i32_i32(int32_t* rem, int32_t* root, int32_t* tmp, int32_t* trial, int32_t cap) {
int32_t x = 9;
while (x >= 0) {
memcpy(tmp, root, (((int64_t)(cap)) * 4));
bi_mul_small_ptr_i32_i32_i32(tmp, cap, 20);
bi_from_int_ptr_i32_i32_i64(trial, cap, ((int64_t)(x)));
bi_add_ptr_i32_ptr_i32_i32(tmp, trial, cap);
bi_mul_small_ptr_i32_i32_i32(tmp, cap, x);
if (bi_cmp_ptr_i32_ptr_i32_i32(tmp, rem, cap) <= 0) {
break;
}
x = (x - 1);
}
memcpy(tmp, root, (((int64_t)(cap)) * 4));
bi_mul_small_ptr_i32_i32_i32(tmp, cap, 20);
bi_from_int_ptr_i32_i32_i64(trial, cap, ((int64_t)(x)));
bi_add_ptr_i32_ptr_i32_i32(tmp, trial, cap);
bi_mul_small_ptr_i32_i32_i32(tmp, cap, x);
bi_sub_ptr_i32_ptr_i32_i32(rem, tmp, cap);
bi_mul10_ptr_i32_i32(root, cap);
bi_from_int_ptr_i32_i32_i64(trial, cap, ((int64_t)(x)));
bi_add_ptr_i32_ptr_i32_i32(root, trial, cap);
return x;
}
int32_t main(void) {
int32_t digits = 100;
int32_t cap = (digits + 5);
int32_t* rem = (int32_t*)(calloc(((int64_t)(cap)), 4));
int32_t* root = (int32_t*)(calloc(((int64_t)(cap)), 4));
int32_t* tmp = (int32_t*)(calloc(((int64_t)(cap)), 4));
int32_t* trial = (int32_t*)(calloc(((int64_t)(cap)), 4));
if ((((rem == NULL || root == NULL) || tmp == NULL) || trial == NULL)) {
return 1;
}
int64_t total = 0;
int32_t n = 2;
while (n <= 100) {
if ((!(is_square_i32(n)))) {
bi_zero_ptr_i32_i32(rem, cap);
bi_zero_ptr_i32_i32(root, cap);
bi_from_int_ptr_i32_i32_i64(rem, cap, ((int64_t)(n)));
int64_t sum = 0;
int32_t d = 0;
while (d < digits) {
int32_t x = digit_step_ptr_i32_ptr_i32_ptr_i32_ptr_i32_i32(rem, root, tmp, trial, cap);
sum = (sum + ((int64_t)(x)));
d = (d + 1);
if (d < digits) {
bi_mul_small_ptr_i32_i32_i32(rem, cap, 100);
}
}
total = (total + sum);
}
n = (n + 1);
}
printf("%lld\n", total);
free(rem);
free(root);
free(tmp);
free(trial);
return 0;
}