Problem 719
Number Splitting — T(10^12). Ported from native C to pure Flow.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n log n) | O(log n) |
| Space complexity | O(1) | O(n^2) |
| Approach | Flow solution | Matrix exponentiation |
| Verdict | Suboptimal |
Flow source
# Project Euler 719
# Number Splitting — T(10^12).
# Ported from native C to pure Flow.
function digit_sum(n0: i64) -> i32 {
let mut n: i64 = n0
let mut s: i64 = 0
while n != 0 {
s = s + n % 10
n = n / 10
}
return s as i32
}
function dfs(num0: i64, target0: i64) -> i32 {
let mut num: i64 = num0
let mut target: i64 = target0
if target < 0 || target > num {
return 0
}
if num == target {
return 1
}
if num == 0 {
if target == 0 {
return 1
}
return 0
}
if (num - target) % 9 != 0 {
return 0
}
if target < 120 && (digit_sum(num) as i64) > target {
return 0
}
let mut pow10: i64 = 10
while pow10 <= num {
let suffix: i64 = num % pow10
if suffix > target {
break
}
let prefix: i64 = num / pow10
if prefix == 0 {
break
}
if dfs(prefix, target - suffix) == 1 {
return 1
}
pow10 = pow10 * 10
}
return 0
}
function is_s_root(root: i64) -> i32 {
let sq: i64 = root * root
if sq < 10 {
return 0
}
let mut pow10: i64 = 10
while pow10 <= sq {
let suffix: i64 = sq % pow10
if suffix > root {
break
}
let prefix: i64 = sq / pow10
if prefix == 0 {
break
}
if dfs(prefix, root - suffix) == 1 {
return 1
}
pow10 = pow10 * 10
}
return 0
}
function main() -> i32 {
let limit: i64 = 1000000000000
let max_root: i64 = 1000000
let mut total: i64 = 0
for base in 0..2 {
let start: i64 = if base == 0 { 9 } else { 1 }
let mut r: i64 = start
while r <= max_root {
if r >= 4 {
let sq: i64 = r * r
if sq <= limit {
if is_s_root(r) == 1 {
total = total + sq
}
}
}
r = r + 9
}
}
printf("%lld\n", total)
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 digit_sum_i64(int64_t n0);
int32_t dfs_i64_i64(int64_t num0, int64_t target0);
int32_t is_s_root_i64(int64_t root);
int32_t main(void);
int32_t digit_sum_i64(int64_t n0) {
int64_t n = n0;
int64_t s = 0;
while (n != 0) {
s = (s + FLOW_CHECKED_MOD((n), (10)));
n = FLOW_CHECKED_DIV((n), (10));
}
return ((int32_t)(s));
}
int32_t dfs_i64_i64(int64_t num0, int64_t target0) {
int64_t num = num0;
int64_t target = target0;
if ((target < 0 || target > num)) {
return 0;
}
if (num == target) {
return 1;
}
if (num == 0) {
if (target == 0) {
return 1;
}
return 0;
}
if (FLOW_CHECKED_MOD(((num - target)), (9)) != 0) {
return 0;
}
if ((target < 120 && ((int64_t)(digit_sum_i64(num))) > target)) {
return 0;
}
int64_t pow10 = 10;
while (pow10 <= num) {
int64_t suffix = FLOW_CHECKED_MOD((num), (pow10));
if (suffix > target) {
break;
}
int64_t prefix = FLOW_CHECKED_DIV((num), (pow10));
if (prefix == 0) {
break;
}
if (dfs_i64_i64(prefix, (target - suffix)) == 1) {
return 1;
}
pow10 = (pow10 * 10);
}
return 0;
}
int32_t is_s_root_i64(int64_t root) {
int64_t sq = (root * root);
if (sq < 10) {
return 0;
}
int64_t pow10 = 10;
while (pow10 <= sq) {
int64_t suffix = FLOW_CHECKED_MOD((sq), (pow10));
if (suffix > root) {
break;
}
int64_t prefix = FLOW_CHECKED_DIV((sq), (pow10));
if (prefix == 0) {
break;
}
if (dfs_i64_i64(prefix, (root - suffix)) == 1) {
return 1;
}
pow10 = (pow10 * 10);
}
return 0;
}
int32_t main(void) {
int64_t limit = 1000000000000;
int64_t max_root = 1000000;
int64_t total = 0;
int32_t __flow_step_1 = 1;
for (int32_t base = 0; (0 <= 2) ? base < 2 : base > 2; base += (0 <= 2) ? 1 : -1) {
int64_t start = ((base == 0) ? (9) : (1));
int64_t r = start;
while (r <= max_root) {
if (r >= 4) {
int64_t sq = (r * r);
if (sq <= limit) {
if (is_s_root_i64(r) == 1) {
total = (total + sq);
}
}
}
r = (r + 9);
}
}
printf("%lld\n", total);
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 @digit_sum(%arg0: i64) -> i32 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = arith.constant 0 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
llvm.store %3, %5 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%6 = llvm.load %1 : !llvm.ptr -> i64
%7 = arith.constant 0 : i32
%9 = arith.extsi %7 : i32 to i64
%8 = arith.cmpi ne, %6, %9 : i64
cf.cond_br %8, ^bb1, ^bb2
^bb1:
%10 = llvm.load %5 : !llvm.ptr -> i64
%11 = llvm.load %1 : !llvm.ptr -> i64
%12 = arith.constant 10 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.remsi %11, %14 : i64
%15 = arith.addi %10, %13 : i64
llvm.store %15, %5 : i64, !llvm.ptr
%16 = llvm.load %1 : !llvm.ptr -> i64
%17 = arith.constant 10 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.divsi %16, %19 : i64
llvm.store %18, %1 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%20 = llvm.load %5 : !llvm.ptr -> i64
%21 = arith.trunci %20 : i64 to i32
func.return %21 : i32
}
func.func @dfs(%arg0: i64, %arg1: i64) -> i32 {
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %23 : i64, !llvm.ptr
%24 = llvm.mlir.constant(1 : i64) : i64
%25 = llvm.alloca %24 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %25 : i64, !llvm.ptr
%26 = llvm.load %25 : !llvm.ptr -> i64
%27 = arith.constant 0 : i32
%29 = arith.extsi %27 : i32 to i64
%28 = arith.cmpi slt, %26, %29 : i64
%30 = scf.if %28 -> (i1) {
%31 = arith.constant true
scf.yield %31 : i1
} else {
%32 = llvm.load %25 : !llvm.ptr -> i64
%33 = llvm.load %23 : !llvm.ptr -> i64
%34 = arith.cmpi sgt, %32, %33 : i64
scf.yield %34 : i1
}
cf.cond_br %30, ^bb3, ^bb4
^bb3:
%35 = arith.constant 0 : i32
func.return %35 : i32
^bb4:
cf.br ^bb5
^bb5:
%36 = llvm.load %23 : !llvm.ptr -> i64
%37 = llvm.load %25 : !llvm.ptr -> i64
%38 = arith.cmpi eq, %36, %37 : i64
cf.cond_br %38, ^bb6, ^bb7
^bb6:
%39 = arith.constant 1 : i32
func.return %39 : i32
^bb7:
cf.br ^bb8
^bb8:
%40 = llvm.load %23 : !llvm.ptr -> i64
%41 = arith.constant 0 : i32
%43 = arith.extsi %41 : i32 to i64
%42 = arith.cmpi eq, %40, %43 : i64
cf.cond_br %42, ^bb9, ^bb10
^bb9:
%44 = llvm.load %25 : !llvm.ptr -> i64
%45 = arith.constant 0 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.cmpi eq, %44, %47 : i64
cf.cond_br %46, ^bb12, ^bb13
^bb12:
%48 = arith.constant 1 : i32
func.return %48 : i32
^bb13:
cf.br ^bb14
^bb14:
%49 = arith.constant 0 : i32
func.return %49 : i32
^bb10:
cf.br ^bb11
^bb11:
%50 = llvm.load %23 : !llvm.ptr -> i64
%51 = llvm.load %25 : !llvm.ptr -> i64
%52 = arith.subi %50, %51 : i64
%53 = arith.constant 9 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.remsi %52, %55 : i64
%56 = arith.constant 0 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.cmpi ne, %54, %58 : i64
cf.cond_br %57, ^bb15, ^bb16
^bb15:
%59 = arith.constant 0 : i32
func.return %59 : i32
^bb16:
cf.br ^bb17
^bb17:
%60 = llvm.load %25 : !llvm.ptr -> i64
%61 = arith.constant 120 : i32
%63 = arith.extsi %61 : i32 to i64
%62 = arith.cmpi slt, %60, %63 : i64
%64 = scf.if %62 -> (i1) {
%66 = llvm.load %23 : !llvm.ptr -> i64
%65 = func.call @digit_sum(%66) : (i64) -> i32
%67 = arith.extsi %65 : i32 to i64
%68 = llvm.load %25 : !llvm.ptr -> i64
%69 = arith.cmpi sgt, %67, %68 : i64
scf.yield %69 : i1
} else {
%70 = arith.constant false
scf.yield %70 : i1
}
cf.cond_br %64, ^bb18, ^bb19
^bb18:
%71 = arith.constant 0 : i32
func.return %71 : i32
^bb19:
cf.br ^bb20
^bb20:
%72 = arith.constant 10 : i32
%73 = arith.extsi %72 : i32 to i64
%74 = llvm.mlir.constant(1 : i64) : i64
%75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
llvm.store %73, %75 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%76 = llvm.load %75 : !llvm.ptr -> i64
%77 = llvm.load %23 : !llvm.ptr -> i64
%78 = arith.cmpi sle, %76, %77 : i64
cf.cond_br %78, ^bb22, ^bb23
^bb22:
%79 = llvm.load %23 : !llvm.ptr -> i64
%80 = llvm.load %75 : !llvm.ptr -> i64
%81 = arith.remsi %79, %80 : i64
%82 = llvm.load %25 : !llvm.ptr -> i64
%83 = arith.cmpi sgt, %81, %82 : i64
cf.cond_br %83, ^bb24, ^bb25
^bb24:
cf.br ^bb23
^bb25:
cf.br ^bb26
^bb26:
%84 = llvm.load %23 : !llvm.ptr -> i64
%85 = llvm.load %75 : !llvm.ptr -> i64
%86 = arith.divsi %84, %85 : i64
%87 = arith.constant 0 : i32
%89 = arith.extsi %87 : i32 to i64
%88 = arith.cmpi eq, %86, %89 : i64
cf.cond_br %88, ^bb27, ^bb28
^bb27:
cf.br ^bb23
^bb28:
cf.br ^bb29
^bb29:
%91 = llvm.load %25 : !llvm.ptr -> i64
%92 = arith.subi %91, %81 : i64
%90 = func.call @dfs(%86, %92) : (i64, i64) -> i32
%93 = arith.constant 1 : i32
%94 = arith.cmpi eq, %90, %93 : i32
cf.cond_br %94, ^bb30, ^bb31
^bb30:
%95 = arith.constant 1 : i32
func.return %95 : i32
^bb31:
cf.br ^bb32
^bb32:
%96 = llvm.load %75 : !llvm.ptr -> i64
%97 = arith.constant 10 : i32
%99 = arith.extsi %97 : i32 to i64
%98 = arith.muli %96, %99 : i64
llvm.store %98, %75 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%100 = arith.constant 0 : i32
func.return %100 : i32
}
func.func @is_s_root(%arg0: i64) -> i32 {
%101 = arith.muli %arg0, %arg0 : i64
%102 = arith.constant 10 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.cmpi slt, %101, %104 : i64
cf.cond_br %103, ^bb33, ^bb34
^bb33:
%105 = arith.constant 0 : i32
func.return %105 : i32
^bb34:
cf.br ^bb35
^bb35:
%106 = arith.constant 10 : i32
%107 = arith.extsi %106 : i32 to i64
%108 = llvm.mlir.constant(1 : i64) : i64
%109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
llvm.store %107, %109 : i64, !llvm.ptr
cf.br ^bb36
^bb36:
%110 = llvm.load %109 : !llvm.ptr -> i64
%111 = arith.cmpi sle, %110, %101 : i64
cf.cond_br %111, ^bb37, ^bb38
^bb37:
%112 = llvm.load %109 : !llvm.ptr -> i64
%113 = arith.remsi %101, %112 : i64
%114 = arith.cmpi sgt, %113, %arg0 : i64
cf.cond_br %114, ^bb39, ^bb40
^bb39:
cf.br ^bb38
^bb40:
cf.br ^bb41
^bb41:
%115 = llvm.load %109 : !llvm.ptr -> i64
%116 = arith.divsi %101, %115 : i64
%117 = arith.constant 0 : i32
%119 = arith.extsi %117 : i32 to i64
%118 = arith.cmpi eq, %116, %119 : i64
cf.cond_br %118, ^bb42, ^bb43
^bb42:
cf.br ^bb38
^bb43:
cf.br ^bb44
^bb44:
%121 = arith.subi %arg0, %113 : i64
%120 = func.call @dfs(%116, %121) : (i64, i64) -> i32
%122 = arith.constant 1 : i32
%123 = arith.cmpi eq, %120, %122 : i32
cf.cond_br %123, ^bb45, ^bb46
^bb45:
%124 = arith.constant 1 : i32
func.return %124 : i32
^bb46:
cf.br ^bb47
^bb47:
%125 = llvm.load %109 : !llvm.ptr -> i64
%126 = arith.constant 10 : i32
%128 = arith.extsi %126 : i32 to i64
%127 = arith.muli %125, %128 : i64
llvm.store %127, %109 : i64, !llvm.ptr
cf.br ^bb36
^bb38:
%129 = arith.constant 0 : i32
func.return %129 : i32
}
func.func @main() -> i32 {
%130 = arith.constant 995705032704 : i32
%131 = arith.extsi %130 : i32 to i64
%132 = arith.constant 1000000 : i32
%133 = arith.extsi %132 : i32 to i64
%134 = arith.constant 0 : i32
%135 = arith.extsi %134 : i32 to i64
%136 = llvm.mlir.constant(1 : i64) : i64
%137 = llvm.alloca %136 x i64 : (i64) -> !llvm.ptr
llvm.store %135, %137 : i64, !llvm.ptr
%138 = arith.constant 0 : i32
%139 = arith.constant 2 : i32
%140 = arith.index_cast %138 : i32 to index
%141 = arith.index_cast %139 : i32 to index
%143 = arith.constant 1 : index
%144 = arith.constant -1 : index
%145 = arith.cmpi sle, %140, %141 : index
%142 = arith.select %145, %143, %144 : index
cf.br ^bb48(%140 : index)
^bb48(%146: index):
%147 = arith.cmpi slt, %146, %141 : index
%148 = arith.cmpi sgt, %146, %141 : index
%149 = arith.select %145, %147, %148 : i1
cf.cond_br %149, ^bb49(%146 : index), ^bb50(%146 : index)
^bb49(%150: index):
%151 = arith.constant 0 : i32
%153 = arith.index_cast %150 : index to i32
%152 = arith.cmpi eq, %153, %151 : i32
%154 = scf.if %152 -> (i32) {
%155 = arith.constant 9 : i32
scf.yield %155 : i32
} else {
%156 = arith.constant 1 : i32
scf.yield %156 : i32
}
%157 = arith.extsi %154 : i32 to i64
%158 = llvm.mlir.constant(1 : i64) : i64
%159 = llvm.alloca %158 x i64 : (i64) -> !llvm.ptr
llvm.store %157, %159 : i64, !llvm.ptr
cf.br ^bb51
^bb51:
%160 = llvm.load %159 : !llvm.ptr -> i64
%161 = arith.cmpi sle, %160, %133 : i64
cf.cond_br %161, ^bb52, ^bb53
^bb52:
%162 = llvm.load %159 : !llvm.ptr -> i64
%163 = arith.constant 4 : i32
%165 = arith.extsi %163 : i32 to i64
%164 = arith.cmpi sge, %162, %165 : i64
cf.cond_br %164, ^bb54, ^bb55
^bb54:
%166 = llvm.load %159 : !llvm.ptr -> i64
%167 = llvm.load %159 : !llvm.ptr -> i64
%168 = arith.muli %166, %167 : i64
%169 = arith.cmpi sle, %168, %131 : i64
cf.cond_br %169, ^bb57, ^bb58
^bb57:
%171 = llvm.load %159 : !llvm.ptr -> i64
%170 = func.call @is_s_root(%171) : (i64) -> i32
%172 = arith.constant 1 : i32
%173 = arith.cmpi eq, %170, %172 : i32
cf.cond_br %173, ^bb60, ^bb61
^bb60:
%174 = llvm.load %137 : !llvm.ptr -> i64
%175 = arith.addi %174, %168 : i64
llvm.store %175, %137 : i64, !llvm.ptr
cf.br ^bb62
^bb61:
cf.br ^bb62
^bb62:
cf.br ^bb59
^bb58:
cf.br ^bb59
^bb59:
cf.br ^bb56
^bb55:
cf.br ^bb56
^bb56:
%176 = llvm.load %159 : !llvm.ptr -> i64
%177 = arith.constant 9 : i32
%179 = arith.extsi %177 : i32 to i64
%178 = arith.addi %176, %179 : i64
llvm.store %178, %159 : i64, !llvm.ptr
cf.br ^bb51
^bb53:
%180 = arith.addi %150, %142 : index
cf.br ^bb48(%180 : index)
^bb50(%181: index):
%182 = llvm.mlir.addressof @str_0 : !llvm.ptr
%183 = llvm.load %137 : !llvm.ptr -> i64
%184 = llvm.call @printf(%182, %183) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%185 = arith.constant 0 : i32
func.return %185 : i32
}
}