← All problems
Problem 820
nth Digit Reciprocals — S(n)=sum_k floor(10*(10^(n-1) mod k)/k).
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 820
# nth Digit Reciprocals — S(n)=sum_k floor(10*(10^(n-1) mod k)/k).
function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
if mod == 1 { return 0 }
let mut r: i64 = 1
let mut b: i64 = base0 % mod
let mut e: i64 = exp0
while e > 0 {
if (e & 1) != 0 {
r = ((r as i128) * (b as i128) % (mod as i128)) as i64
}
b = ((b as i128) * (b as i128) % (mod as i128)) as i64
e = e >> 1
}
return r
}
function d(n: i64, k: i64) -> i64 {
return (10 * modpow(10, n - 1, k)) / k
}
function S(n: i64) -> i64 {
let mut total: i64 = 0
let mut k: i64 = 1
while k <= n {
total = total + d(n, k)
k = k + 1
}
return total
}
function main() -> i32 {
printf("%lld\n", S(10000000))
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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t d_i64_i64(int64_t n, int64_t k);
int64_t S_i64(int64_t n);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
if (mod == 1) {
return 0;
}
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base0), (mod));
int64_t e = exp0;
while (e > 0) {
if ((e & 1) != 0) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
}
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
int64_t d_i64_i64(int64_t n, int64_t k) {
return FLOW_CHECKED_DIV(((10 * modpow_i64_i64_i64(10, (n - 1), k))), (k));
}
int64_t S_i64(int64_t n) {
int64_t total = 0;
int64_t k = 1;
while (k <= n) {
total = (total + d_i64_i64(n, k));
k = (k + 1);
}
return total;
}
int32_t main(void) {
printf("%lld\n", S_i64(10000000));
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 1 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi eq, %arg2, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0 : i32
%4 = arith.extsi %3 : i32 to i64
func.return %4 : i64
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.constant 1 : i32
%6 = arith.extsi %5 : i32 to i64
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %6, %8 : i64, !llvm.ptr
%9 = arith.remsi %arg0, %arg2 : i64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
llvm.store %9, %11 : i64, !llvm.ptr
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %13 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%14 = llvm.load %13 : !llvm.ptr -> i64
%15 = arith.constant 0 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.cmpi sgt, %14, %17 : i64
cf.cond_br %16, ^bb4, ^bb5
^bb4:
%18 = llvm.load %13 : !llvm.ptr -> i64
%19 = arith.constant 1 : i32
%21 = arith.extsi %19 : i32 to i64
%20 = arith.andi %18, %21 : i64
%22 = arith.constant 0 : i32
%24 = arith.extsi %22 : i32 to i64
%23 = arith.cmpi ne, %20, %24 : i64
cf.cond_br %23, ^bb6, ^bb7
^bb6:
%25 = llvm.load %8 : !llvm.ptr -> i64
%26 = arith.extsi %25 : i64 to i128
%27 = llvm.load %11 : !llvm.ptr -> i64
%28 = arith.extsi %27 : i64 to i128
%30 = arith.trunci %26 : i128 to i64
%31 = arith.trunci %28 : i128 to i64
%29 = arith.muli %30, %31 : i64
%32 = arith.extsi %arg2 : i64 to i128
%34 = arith.trunci %32 : i128 to i64
%33 = arith.remsi %29, %34 : i64
llvm.store %33, %8 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%35 = llvm.load %11 : !llvm.ptr -> i64
%36 = arith.extsi %35 : i64 to i128
%37 = llvm.load %11 : !llvm.ptr -> i64
%38 = arith.extsi %37 : i64 to i128
%40 = arith.trunci %36 : i128 to i64
%41 = arith.trunci %38 : i128 to i64
%39 = arith.muli %40, %41 : i64
%42 = arith.extsi %arg2 : i64 to i128
%44 = arith.trunci %42 : i128 to i64
%43 = arith.remsi %39, %44 : i64
llvm.store %43, %11 : i64, !llvm.ptr
%45 = llvm.load %13 : !llvm.ptr -> i64
%46 = arith.constant 1 : i32
%48 = arith.extsi %46 : i32 to i64
%47 = arith.shrsi %45, %48 : i64
llvm.store %47, %13 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%49 = llvm.load %8 : !llvm.ptr -> i64
func.return %49 : i64
}
func.func @d(%arg0: i64, %arg1: i64) -> i64 {
%50 = arith.constant 10 : i32
%52 = arith.constant 10 : i32
%53 = arith.constant 1 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.subi %arg0, %55 : i64
%56 = arith.extsi %52 : i32 to i64
%51 = func.call @modpow(%56, %54, %arg1) : (i64, i64, i64) -> i64
%58 = arith.extsi %50 : i32 to i64
%57 = arith.muli %58, %51 : i64
%59 = arith.divsi %57, %arg1 : i64
func.return %59 : i64
}
func.func @S(%arg0: i64) -> i64 {
%60 = arith.constant 0 : i32
%61 = arith.extsi %60 : i32 to i64
%62 = llvm.mlir.constant(1 : i64) : i64
%63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
llvm.store %61, %63 : i64, !llvm.ptr
%64 = arith.constant 1 : i32
%65 = arith.extsi %64 : i32 to i64
%66 = llvm.mlir.constant(1 : i64) : i64
%67 = llvm.alloca %66 x i64 : (i64) -> !llvm.ptr
llvm.store %65, %67 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%68 = llvm.load %67 : !llvm.ptr -> i64
%69 = arith.cmpi sle, %68, %arg0 : i64
cf.cond_br %69, ^bb10, ^bb11
^bb10:
%70 = llvm.load %63 : !llvm.ptr -> i64
%72 = llvm.load %67 : !llvm.ptr -> i64
%71 = func.call @d(%arg0, %72) : (i64, i64) -> i64
%73 = arith.addi %70, %71 : i64
llvm.store %73, %63 : i64, !llvm.ptr
%74 = llvm.load %67 : !llvm.ptr -> i64
%75 = arith.constant 1 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.addi %74, %77 : i64
llvm.store %76, %67 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%78 = llvm.load %63 : !llvm.ptr -> i64
func.return %78 : i64
}
func.func @main() -> i32 {
%79 = llvm.mlir.addressof @str_0 : !llvm.ptr
%81 = arith.constant 10000000 : i32
%82 = arith.extsi %81 : i32 to i64
%80 = func.call @S(%82) : (i64) -> i64
%83 = llvm.call @printf(%79, %80) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%84 = arith.constant 0 : i32
func.return %84 : i32
}
}