Problem 731

Stoneham Number: 10 digits after the 10^16-th place. Sum of (10^(n-3^i-1) mod 3^i) / 3^i for i=1..limit, take fractional part.

Answer6086371427
Output6086371427
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(1)O(n)
ApproachFlow solutionSieve or enumeration
VerdictSuboptimal

Flow source

# Project Euler 731
# Stoneham Number: 10 digits after the 10^16-th place.
# Sum of (10^(n-3^i-1) mod 3^i) / 3^i for i=1..limit, take fractional part.

extern {
    function log(x: f64) -> f64
    function floor(x: f64) -> f64
}

function modpow(b: i64, e: i64, m: i64) -> i64 {
    let mut r: i64 = 1 % m
    let mut bb: i64 = b % m
    let mut ee: i64 = e
    while ee > 0 {
        if (ee & 1) == 1 {
            r = ((r as i128) * (bb as i128) % (m as i128)) as i64
        }
        bb = ((bb as i128) * (bb as i128) % (m as i128)) as i64
        ee = ee >> 1
    }
    return r
}

function main() -> i32 {
    let n: i64 = 10000000000000000
    let limit: i64 = (log(n as f64) / log(3.0)) as i64
    let mut total: f64 = 0.0
    let mut i: i64 = 1
    while i <= limit {
        let mut div: i64 = 1
        let mut j: i64 = 0
        while j < i {
            div = div * 3
            j = j + 1
        }
        let exp: i64 = n - div - 1
        let temp: i64 = modpow(10, exp, div)
        total = total + (temp as f64) / (div as f64)
        i = i + 1
    }
    let frac: f64 = total - floor(total)
    let digits: i64 = (frac * 10000000000.0) as i64
    printf("%010lld\n", digits)
    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 b, int64_t e, int64_t m);
int32_t main(void);



int64_t modpow_i64_i64_i64(int64_t b, int64_t e, int64_t m) {
    int64_t r = FLOW_CHECKED_MOD((1), (m));
    int64_t bb = FLOW_CHECKED_MOD((b), (m));
    int64_t ee = e;
    while (ee > 0) {
        if ((ee & 1) == 1) {
            r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(bb)))), (((__int128)(m))))));
        }
        bb = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(bb)) * ((__int128)(bb)))), (((__int128)(m))))));
        ee = FLOW_CHECKED_SHR((ee), (1));
    }
    return r;
}

int32_t main(void) {
    int64_t n = 10000000000000000;
    int64_t limit = ((int64_t)((log(((double)(n))) / log(3.0))));
    double total = 0.0;
    int64_t i = 1;
    while (i <= limit) {
        int64_t div = 1;
        int64_t j = 0;
        while (j < i) {
            div = (div * 3);
            j = (j + 1);
        }
        int64_t exp = ((n - div) - 1);
        int64_t temp = modpow_i64_i64_i64(10, exp, div);
        total = (total + (((double)(temp)) / ((double)(div))));
        i = (i + 1);
    }
    double frac = (total - floor(total));
    int64_t digits = ((int64_t)((frac * 10000000000.0)));
    printf("%010lld\n", digits);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%010lld\n\00") {addr_space = 0 : i32} : !llvm.array<9 x i8>
  func.func private @log(f64) -> f64
  func.func private @floor(f64) -> f64
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.remsi %2, %arg2 : i64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %4 : i64, !llvm.ptr
    %5 = arith.remsi %arg0, %arg2 : i64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i64, !llvm.ptr
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %9 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %10 = llvm.load %9 : !llvm.ptr -> i64
    %11 = arith.constant 0 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.cmpi sgt, %10, %13 : i64
    cf.cond_br %12, ^bb1, ^bb2
    ^bb1:
      %14 = llvm.load %9 : !llvm.ptr -> i64
      %15 = arith.constant 1 : i32
      %17 = arith.extsi %15 : i32 to i64
      %16 = arith.andi %14, %17 : i64
      %18 = arith.constant 1 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.cmpi eq, %16, %20 : i64
      cf.cond_br %19, ^bb3, ^bb4
      ^bb3:
        %21 = llvm.load %4 : !llvm.ptr -> i64
        %22 = arith.extsi %21 : i64 to i128
        %23 = llvm.load %7 : !llvm.ptr -> i64
        %24 = arith.extsi %23 : i64 to i128
        %26 = arith.trunci %22 : i128 to i64
        %27 = arith.trunci %24 : i128 to i64
        %25 = arith.muli %26, %27 : i64
        %28 = arith.extsi %arg2 : i64 to i128
        %30 = arith.trunci %28 : i128 to i64
        %29 = arith.remsi %25, %30 : i64
        llvm.store %29, %4 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %31 = llvm.load %7 : !llvm.ptr -> i64
      %32 = arith.extsi %31 : i64 to i128
      %33 = llvm.load %7 : !llvm.ptr -> i64
      %34 = arith.extsi %33 : i64 to i128
      %36 = arith.trunci %32 : i128 to i64
      %37 = arith.trunci %34 : i128 to i64
      %35 = arith.muli %36, %37 : i64
      %38 = arith.extsi %arg2 : i64 to i128
      %40 = arith.trunci %38 : i128 to i64
      %39 = arith.remsi %35, %40 : i64
      llvm.store %39, %7 : i64, !llvm.ptr
      %41 = llvm.load %9 : !llvm.ptr -> i64
      %42 = arith.constant 1 : i32
      %44 = arith.extsi %42 : i32 to i64
      %43 = arith.shrsi %41, %44 : i64
      llvm.store %43, %9 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %45 = llvm.load %4 : !llvm.ptr -> i64
    func.return %45 : i64
  }
  func.func @main() -> i32 {
    %46 = arith.constant 9999995705032704 : i32
    %47 = arith.extsi %46 : i32 to i64
    %48 = arith.sitofp %47 : i64 to f64
    %49 = math.log %48 : f64
    %50 = arith.constant 3.0 : f32
    %51 = math.log %50 : f32
    %52 = arith.divf %49, %51 : f64
    %53 = arith.fptosi %52 : f64 to i64
    %54 = arith.constant 0.0 : f32
    %55 = arith.extf %54 : f32 to f64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x f64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : f64, !llvm.ptr
    %58 = arith.constant 1 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %62 = llvm.load %61 : !llvm.ptr -> i64
    %63 = arith.cmpi sle, %62, %53 : i64
    cf.cond_br %63, ^bb7, ^bb8
    ^bb7:
      %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
      %68 = arith.constant 0 : i32
      %69 = arith.extsi %68 : i32 to i64
      %70 = llvm.mlir.constant(1 : i64) : i64
      %71 = llvm.alloca %70 x i64 : (i64) -> !llvm.ptr
      llvm.store %69, %71 : i64, !llvm.ptr
      cf.br ^bb9
      ^bb9:
      %72 = llvm.load %71 : !llvm.ptr -> i64
      %73 = llvm.load %61 : !llvm.ptr -> i64
      %74 = arith.cmpi slt, %72, %73 : i64
      cf.cond_br %74, ^bb10, ^bb11
      ^bb10:
        %75 = llvm.load %67 : !llvm.ptr -> i64
        %76 = arith.constant 3 : i32
        %78 = arith.extsi %76 : i32 to i64
        %77 = arith.muli %75, %78 : i64
        llvm.store %77, %67 : i64, !llvm.ptr
        %79 = llvm.load %71 : !llvm.ptr -> i64
        %80 = arith.constant 1 : i32
        %82 = arith.extsi %80 : i32 to i64
        %81 = arith.addi %79, %82 : i64
        llvm.store %81, %71 : i64, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %83 = llvm.load %67 : !llvm.ptr -> i64
      %84 = arith.subi %47, %83 : i64
      %85 = arith.constant 1 : i32
      %87 = arith.extsi %85 : i32 to i64
      %86 = arith.subi %84, %87 : i64
      %89 = arith.constant 10 : i32
      %90 = llvm.load %67 : !llvm.ptr -> i64
      %91 = arith.extsi %89 : i32 to i64
      %88 = func.call @modpow(%91, %86, %90) : (i64, i64, i64) -> i64
      %92 = llvm.load %57 : !llvm.ptr -> f64
      %93 = arith.sitofp %88 : i64 to f64
      %94 = llvm.load %67 : !llvm.ptr -> i64
      %95 = arith.sitofp %94 : i64 to f64
      %96 = arith.divf %93, %95 : f64
      %97 = arith.addf %92, %96 : f64
      llvm.store %97, %57 : f64, !llvm.ptr
      %98 = llvm.load %61 : !llvm.ptr -> i64
      %99 = arith.constant 1 : i32
      %101 = arith.extsi %99 : i32 to i64
      %100 = arith.addi %98, %101 : i64
      llvm.store %100, %61 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %102 = llvm.load %57 : !llvm.ptr -> f64
    %104 = llvm.load %57 : !llvm.ptr -> f64
    %103 = func.call @floor(%104) : (f64) -> f64
    %105 = arith.subf %102, %103 : f64
    %106 = arith.constant 10000000000.0 : f32
    %108 = arith.extf %106 : f32 to f64
    %107 = arith.mulf %105, %108 : f64
    %109 = arith.fptosi %107 : f64 to i64
    %110 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %111 = llvm.call @printf(%110, %109) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %112 = arith.constant 0 : i32
    func.return %112 : i32
  }
}