Problem 290
Count n < 10^18 with digit_sum(n) = digit_sum(137*n).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n log n) | O(n log log n) |
| Space complexity | O(n) | O(n) |
| Approach | Flow solution | Sieve or enumeration |
| Verdict | Suboptimal |
Flow source
# Project Euler 290
# Count n < 10^18 with digit_sum(n) = digit_sum(137*n).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function digit_sum(x0: i64) -> i64 {
let mut x: i64 = x0
let mut result: i64 = 0
while x > 0 {
result = result + x % 10
x = x / 10
}
return result
}
function search(num_digits: i64, u: i64, v: i64, cache: ptr<i64>) -> i64 {
let SIGNATURE: i64 = 137
let idx: i64 = ((num_digits * 137 + u) * 240) + (v + 120)
if cache[idx] != -1 { return cache[idx] }
if num_digits == 1 {
let mut result: i64 = 0
let mut digit: i64 = 0
while digit < 10 {
if digit_sum(SIGNATURE * digit + u) + v == digit {
result = result + 1
}
digit = digit + 1
}
cache[idx] = result
return result
}
let mut result: i64 = 0
let mut digit: i64 = 0
while digit < 10 {
let next_u: i64 = (SIGNATURE * digit + u) / 10
let next_v: i64 = (SIGNATURE * digit + u) % 10 + v - digit
result = result + search(num_digits - 1, next_u, next_v, cache)
digit = digit + 1
}
cache[idx] = result
return result
}
function main() -> i32 {
let size: i64 = (18 + 1) * 137 * 240
let cache: ptr<i64> = calloc(size, 8)
if cache == null { return 1 }
let mut i: i64 = 0
while i < size {
cache[i] = -1
i = i + 1
}
printf("%lld\n", search(18, 0, 0, cache))
free(cache)
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 digit_sum_i64(int64_t x0);
int64_t search_i64_i64_i64_ptr_i64(int64_t num_digits, int64_t u, int64_t v, int64_t* cache);
int32_t main(void);
int64_t digit_sum_i64(int64_t x0) {
int64_t x = x0;
int64_t result = 0;
while (x > 0) {
result = (result + FLOW_CHECKED_MOD((x), (10)));
x = FLOW_CHECKED_DIV((x), (10));
}
return result;
}
int64_t search_i64_i64_i64_ptr_i64(int64_t num_digits, int64_t u, int64_t v, int64_t* cache) {
int64_t SIGNATURE = 137;
int64_t idx = ((((num_digits * 137) + u) * 240) + (v + 120));
if (cache[idx] != (-1)) {
return cache[idx];
}
if (num_digits == 1) {
int64_t result = 0;
int64_t digit = 0;
while (digit < 10) {
if ((digit_sum_i64(((SIGNATURE * digit) + u)) + v) == digit) {
result = (result + 1);
}
digit = (digit + 1);
}
cache[idx] = result;
return result;
}
int64_t result = 0;
int64_t digit = 0;
while (digit < 10) {
int64_t next_u = FLOW_CHECKED_DIV((((SIGNATURE * digit) + u)), (10));
int64_t next_v = ((FLOW_CHECKED_MOD((((SIGNATURE * digit) + u)), (10)) + v) - digit);
result = (result + search_i64_i64_i64_ptr_i64((num_digits - 1), next_u, next_v, cache));
digit = (digit + 1);
}
cache[idx] = result;
return result;
}
int32_t main(void) {
int64_t size = (((18 + 1) * 137) * 240);
int64_t* cache = (int64_t*)(calloc(size, 8));
if (cache == NULL) {
return 1;
}
int64_t i = 0;
while (i < size) {
cache[i] = (-1);
i = (i + 1);
}
printf("%lld\n", search_i64_i64_i64_ptr_i64(18, 0, 0, cache));
free(cache);
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 private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @digit_sum(%arg0: i64) -> i64 {
%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 sgt, %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
func.return %20 : i64
}
func.func @search(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: !llvm.ptr) -> i64 {
%21 = arith.constant 137 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = arith.constant 137 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.muli %arg0, %25 : i64
%26 = arith.addi %24, %arg1 : i64
%27 = arith.constant 240 : i32
%29 = arith.extsi %27 : i32 to i64
%28 = arith.muli %26, %29 : i64
%30 = arith.constant 120 : i32
%32 = arith.extsi %30 : i32 to i64
%31 = arith.addi %arg2, %32 : i64
%33 = arith.addi %28, %31 : i64
%35 = llvm.getelementptr %arg3[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%34 = llvm.load %35 : !llvm.ptr -> i64
%36 = arith.constant 1 : i32
%38 = arith.constant 0 : i32
%37 = arith.subi %38, %36 : i32
%40 = arith.extsi %37 : i32 to i64
%39 = arith.cmpi ne, %34, %40 : i64
cf.cond_br %39, ^bb3, ^bb4
^bb3:
%42 = llvm.getelementptr %arg3[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%41 = llvm.load %42 : !llvm.ptr -> i64
func.return %41 : i64
^bb4:
cf.br ^bb5
^bb5:
%43 = arith.constant 1 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.cmpi eq, %arg0, %45 : i64
cf.cond_br %44, ^bb6, ^bb7
^bb6:
%46 = arith.constant 0 : i32
%47 = arith.extsi %46 : i32 to i64
%48 = llvm.mlir.constant(1 : i64) : i64
%49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
llvm.store %47, %49 : i64, !llvm.ptr
%50 = arith.constant 0 : i32
%51 = arith.extsi %50 : i32 to i64
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
llvm.store %51, %53 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%54 = llvm.load %53 : !llvm.ptr -> i64
%55 = arith.constant 10 : i32
%57 = arith.extsi %55 : i32 to i64
%56 = arith.cmpi slt, %54, %57 : i64
cf.cond_br %56, ^bb10, ^bb11
^bb10:
%59 = llvm.load %53 : !llvm.ptr -> i64
%60 = arith.muli %22, %59 : i64
%61 = arith.addi %60, %arg1 : i64
%58 = func.call @digit_sum(%61) : (i64) -> i64
%62 = arith.addi %58, %arg2 : i64
%63 = llvm.load %53 : !llvm.ptr -> i64
%64 = arith.cmpi eq, %62, %63 : i64
cf.cond_br %64, ^bb12, ^bb13
^bb12:
%65 = llvm.load %49 : !llvm.ptr -> i64
%66 = arith.constant 1 : i32
%68 = arith.extsi %66 : i32 to i64
%67 = arith.addi %65, %68 : i64
llvm.store %67, %49 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%69 = llvm.load %53 : !llvm.ptr -> i64
%70 = arith.constant 1 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.addi %69, %72 : i64
llvm.store %71, %53 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%73 = llvm.load %49 : !llvm.ptr -> i64
%74 = llvm.getelementptr %arg3[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %73, %74 : i64, !llvm.ptr
%75 = llvm.load %49 : !llvm.ptr -> i64
func.return %75 : i64
^bb7:
cf.br ^bb8
^bb8:
%76 = arith.constant 0 : i32
%77 = arith.extsi %76 : i32 to i64
%78 = llvm.mlir.constant(1 : i64) : i64
%79 = llvm.alloca %78 x i64 : (i64) -> !llvm.ptr
llvm.store %77, %79 : i64, !llvm.ptr
%80 = arith.constant 0 : i32
%81 = arith.extsi %80 : i32 to i64
%82 = llvm.mlir.constant(1 : i64) : i64
%83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
llvm.store %81, %83 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%84 = llvm.load %83 : !llvm.ptr -> i64
%85 = arith.constant 10 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.cmpi slt, %84, %87 : i64
cf.cond_br %86, ^bb16, ^bb17
^bb16:
%88 = llvm.load %83 : !llvm.ptr -> i64
%89 = arith.muli %22, %88 : i64
%90 = arith.addi %89, %arg1 : i64
%91 = arith.constant 10 : i32
%93 = arith.extsi %91 : i32 to i64
%92 = arith.divsi %90, %93 : i64
%94 = llvm.load %83 : !llvm.ptr -> i64
%95 = arith.muli %22, %94 : i64
%96 = arith.addi %95, %arg1 : i64
%97 = arith.constant 10 : i32
%99 = arith.extsi %97 : i32 to i64
%98 = arith.remsi %96, %99 : i64
%100 = arith.addi %98, %arg2 : i64
%101 = llvm.load %83 : !llvm.ptr -> i64
%102 = arith.subi %100, %101 : i64
%103 = llvm.load %79 : !llvm.ptr -> i64
%105 = arith.constant 1 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.subi %arg0, %107 : i64
%104 = func.call @search(%106, %92, %102, %arg3) : (i64, i64, i64, !llvm.ptr) -> i64
%108 = arith.addi %103, %104 : i64
llvm.store %108, %79 : i64, !llvm.ptr
%109 = llvm.load %83 : !llvm.ptr -> i64
%110 = arith.constant 1 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.addi %109, %112 : i64
llvm.store %111, %83 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%113 = llvm.load %79 : !llvm.ptr -> i64
%114 = llvm.getelementptr %arg3[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %113, %114 : i64, !llvm.ptr
%115 = llvm.load %79 : !llvm.ptr -> i64
func.return %115 : i64
}
func.func @main() -> i32 {
%116 = arith.constant 18 : i32
%117 = arith.constant 1 : i32
%118 = arith.addi %116, %117 : i32
%119 = arith.constant 137 : i32
%120 = arith.muli %118, %119 : i32
%121 = arith.constant 240 : i32
%122 = arith.muli %120, %121 : i32
%123 = arith.extsi %122 : i32 to i64
%125 = arith.constant 8 : i32
%126 = arith.extsi %125 : i32 to i64
%124 = func.call @calloc(%123, %126) : (i64, i64) -> !llvm.ptr
%127 = llvm.mlir.zero : !llvm.ptr
%128 = llvm.icmp "eq" %124, %127 : !llvm.ptr
cf.cond_br %128, ^bb18, ^bb19
^bb18:
%129 = arith.constant 1 : i32
func.return %129 : i32
^bb19:
cf.br ^bb20
^bb20:
%130 = arith.constant 0 : i32
%131 = arith.extsi %130 : i32 to i64
%132 = llvm.mlir.constant(1 : i64) : i64
%133 = llvm.alloca %132 x i64 : (i64) -> !llvm.ptr
llvm.store %131, %133 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%134 = llvm.load %133 : !llvm.ptr -> i64
%135 = arith.cmpi slt, %134, %123 : i64
cf.cond_br %135, ^bb22, ^bb23
^bb22:
%136 = arith.constant 1 : i32
%138 = arith.constant 0 : i32
%137 = arith.subi %138, %136 : i32
%139 = llvm.load %133 : !llvm.ptr -> i64
%140 = arith.extsi %137 : i32 to i64
%141 = llvm.getelementptr %124[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %140, %141 : i64, !llvm.ptr
%142 = llvm.load %133 : !llvm.ptr -> i64
%143 = arith.constant 1 : i32
%145 = arith.extsi %143 : i32 to i64
%144 = arith.addi %142, %145 : i64
llvm.store %144, %133 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%146 = llvm.mlir.addressof @str_0 : !llvm.ptr
%148 = arith.constant 18 : i32
%149 = arith.constant 0 : i32
%150 = arith.constant 0 : i32
%151 = arith.extsi %148 : i32 to i64
%152 = arith.extsi %149 : i32 to i64
%153 = arith.extsi %150 : i32 to i64
%147 = func.call @search(%151, %152, %153, %124) : (i64, i64, i64, !llvm.ptr) -> i64
%154 = llvm.call @printf(%146, %147) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%124) : (!llvm.ptr) -> ()
%156 = arith.constant 0 : i32
func.return %156 : i32
}
}