Problem 168
Right-rotations that are multiples; sum last 5 digits of all such numbers with 2..100 digits.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^3) | O(n) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Big-integer arithmetic |
| Verdict | Suboptimal |
Flow source
# Project Euler 168
# Right-rotations that are multiples; sum last 5 digits of all such numbers
# with 2..100 digits.
function search(num_digits0: i32, multiplier: i32, last_digit: i32, modulo: i64) -> i64 {
let mut num_digits: i32 = num_digits0
let mut shift: i64 = 10
let mut carry: i32 = 0
let mut current: i32 = last_digit
let mut result: i64 = last_digit as i64
while num_digits > 1 {
num_digits = num_digits - 1
let next_val: i32 = multiplier * current + carry
carry = next_val / 10
current = next_val % 10
if shift < modulo {
result = result + (current as i64) * shift
shift = shift * 10
}
}
let first: i32 = multiplier * current + carry
if current == 0 || first != last_digit {
return 0
}
return result
}
function main() -> i32 {
let modulo: i64 = 100000
let mut result: i64 = 0
let mut num_digits: i32 = 2
while num_digits <= 100 {
let mut multiplier: i32 = 1
while multiplier <= 9 {
let mut last_digit: i32 = 1
while last_digit <= 9 {
result = result + search(num_digits, multiplier, last_digit, modulo)
last_digit = last_digit + 1
}
multiplier = multiplier + 1
}
num_digits = num_digits + 1
}
printf("%lld\n", result % modulo)
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 search_i32_i32_i32_i64(int32_t num_digits0, int32_t multiplier, int32_t last_digit, int64_t modulo);
int32_t main(void);
int64_t search_i32_i32_i32_i64(int32_t num_digits0, int32_t multiplier, int32_t last_digit, int64_t modulo) {
int32_t num_digits = num_digits0;
int64_t shift = 10;
int32_t carry = 0;
int32_t current = last_digit;
int64_t result = ((int64_t)(last_digit));
while (num_digits > 1) {
num_digits = (num_digits - 1);
int32_t next_val = ((multiplier * current) + carry);
carry = FLOW_CHECKED_DIV((next_val), (10));
current = FLOW_CHECKED_MOD((next_val), (10));
if (shift < modulo) {
result = (result + (((int64_t)(current)) * shift));
shift = (shift * 10);
}
}
int32_t first = ((multiplier * current) + carry);
if ((current == 0 || first != last_digit)) {
return 0;
}
return result;
}
int32_t main(void) {
int64_t modulo = 100000;
int64_t result = 0;
int32_t num_digits = 2;
while (num_digits <= 100) {
int32_t multiplier = 1;
while (multiplier <= 9) {
int32_t last_digit = 1;
while (last_digit <= 9) {
result = (result + search_i32_i32_i32_i64(num_digits, multiplier, last_digit, modulo));
last_digit = (last_digit + 1);
}
multiplier = (multiplier + 1);
}
num_digits = (num_digits + 1);
}
printf("%lld\n", FLOW_CHECKED_MOD((result), (modulo)));
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 @search(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i32, !llvm.ptr
%2 = arith.constant 10 : 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
%6 = arith.constant 0 : i32
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i32 : (i64) -> !llvm.ptr
llvm.store %6, %8 : i32, !llvm.ptr
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i32 : (i64) -> !llvm.ptr
llvm.store %arg2, %10 : i32, !llvm.ptr
%11 = arith.extsi %arg2 : i32 to i64
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
llvm.store %11, %13 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%14 = llvm.load %1 : !llvm.ptr -> i32
%15 = arith.constant 1 : i32
%16 = arith.cmpi sgt, %14, %15 : i32
cf.cond_br %16, ^bb1, ^bb2
^bb1:
%17 = llvm.load %1 : !llvm.ptr -> i32
%18 = arith.constant 1 : i32
%19 = arith.subi %17, %18 : i32
llvm.store %19, %1 : i32, !llvm.ptr
%20 = llvm.load %10 : !llvm.ptr -> i32
%21 = arith.muli %arg1, %20 : i32
%22 = llvm.load %8 : !llvm.ptr -> i32
%23 = arith.addi %21, %22 : i32
%24 = arith.constant 10 : i32
%25 = arith.divsi %23, %24 : i32
llvm.store %25, %8 : i32, !llvm.ptr
%26 = arith.constant 10 : i32
%27 = arith.remsi %23, %26 : i32
llvm.store %27, %10 : i32, !llvm.ptr
%28 = llvm.load %5 : !llvm.ptr -> i64
%29 = arith.cmpi slt, %28, %arg3 : i64
cf.cond_br %29, ^bb3, ^bb4
^bb3:
%30 = llvm.load %13 : !llvm.ptr -> i64
%31 = llvm.load %10 : !llvm.ptr -> i32
%32 = arith.extsi %31 : i32 to i64
%33 = llvm.load %5 : !llvm.ptr -> i64
%34 = arith.muli %32, %33 : i64
%35 = arith.addi %30, %34 : i64
llvm.store %35, %13 : i64, !llvm.ptr
%36 = llvm.load %5 : !llvm.ptr -> i64
%37 = arith.constant 10 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.muli %36, %39 : i64
llvm.store %38, %5 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
cf.br ^bb0
^bb2:
%40 = llvm.load %10 : !llvm.ptr -> i32
%41 = arith.muli %arg1, %40 : i32
%42 = llvm.load %8 : !llvm.ptr -> i32
%43 = arith.addi %41, %42 : i32
%44 = llvm.load %10 : !llvm.ptr -> i32
%45 = arith.constant 0 : i32
%46 = arith.cmpi eq, %44, %45 : i32
%47 = scf.if %46 -> (i1) {
%48 = arith.constant true
scf.yield %48 : i1
} else {
%49 = arith.cmpi ne, %43, %arg2 : i32
scf.yield %49 : i1
}
cf.cond_br %47, ^bb6, ^bb7
^bb6:
%50 = arith.constant 0 : i32
%51 = arith.extsi %50 : i32 to i64
func.return %51 : i64
^bb7:
cf.br ^bb8
^bb8:
%52 = llvm.load %13 : !llvm.ptr -> i64
func.return %52 : i64
}
func.func @main() -> i32 {
%53 = arith.constant 100000 : i32
%54 = arith.extsi %53 : i32 to i64
%55 = arith.constant 0 : i32
%56 = arith.extsi %55 : i32 to i64
%57 = llvm.mlir.constant(1 : i64) : i64
%58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
llvm.store %56, %58 : i64, !llvm.ptr
%59 = arith.constant 2 : i32
%60 = llvm.mlir.constant(1 : i64) : i64
%61 = llvm.alloca %60 x i32 : (i64) -> !llvm.ptr
llvm.store %59, %61 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%62 = llvm.load %61 : !llvm.ptr -> i32
%63 = arith.constant 100 : i32
%64 = arith.cmpi sle, %62, %63 : i32
cf.cond_br %64, ^bb10, ^bb11
^bb10:
%65 = arith.constant 1 : i32
%66 = llvm.mlir.constant(1 : i64) : i64
%67 = llvm.alloca %66 x i32 : (i64) -> !llvm.ptr
llvm.store %65, %67 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%68 = llvm.load %67 : !llvm.ptr -> i32
%69 = arith.constant 9 : i32
%70 = arith.cmpi sle, %68, %69 : i32
cf.cond_br %70, ^bb13, ^bb14
^bb13:
%71 = arith.constant 1 : i32
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x i32 : (i64) -> !llvm.ptr
llvm.store %71, %73 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%74 = llvm.load %73 : !llvm.ptr -> i32
%75 = arith.constant 9 : i32
%76 = arith.cmpi sle, %74, %75 : i32
cf.cond_br %76, ^bb16, ^bb17
^bb16:
%77 = llvm.load %58 : !llvm.ptr -> i64
%79 = llvm.load %61 : !llvm.ptr -> i32
%80 = llvm.load %67 : !llvm.ptr -> i32
%81 = llvm.load %73 : !llvm.ptr -> i32
%78 = func.call @search(%79, %80, %81, %54) : (i32, i32, i32, i64) -> i64
%82 = arith.addi %77, %78 : i64
llvm.store %82, %58 : i64, !llvm.ptr
%83 = llvm.load %73 : !llvm.ptr -> i32
%84 = arith.constant 1 : i32
%85 = arith.addi %83, %84 : i32
llvm.store %85, %73 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%86 = llvm.load %67 : !llvm.ptr -> i32
%87 = arith.constant 1 : i32
%88 = arith.addi %86, %87 : i32
llvm.store %88, %67 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%89 = llvm.load %61 : !llvm.ptr -> i32
%90 = arith.constant 1 : i32
%91 = arith.addi %89, %90 : i32
llvm.store %91, %61 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%92 = llvm.mlir.addressof @str_0 : !llvm.ptr
%93 = llvm.load %58 : !llvm.ptr -> i64
%94 = arith.remsi %93, %54 : i64
%95 = llvm.call @printf(%92, %94) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%96 = arith.constant 0 : i32
func.return %96 : i32
}
}