← All problems
Problem 455
Powers with Trailing Digits — sum f(n) for n=2..10^6, f = fixed point of x↦n^x mod 10^9.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n log n)
Space complexity O(1)O(n)
Approach Flow solution Modular DP or matrix exponentiation
Verdict Optimal
Flow source
# Project Euler 455
# Powers with Trailing Digits — sum f(n) for n=2..10^6, f = fixed point of x↦n^x mod 10^9.
function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0 % mod
let mut e: i64 = exp0
while e > 0 {
if e % 2 == 1 {
let t: i128 = (r as i128) * (b as i128) % (mod as i128)
r = t as i64
}
let t2: i128 = (b as i128) * (b as i128) % (mod as i128)
b = t2 as i64
e = e / 2
}
return r
}
function f_n(n: i64, modulo: i64) -> i64 {
let mut exponent: i64 = n
let mut guard: i32 = 0
while guard < 64 {
let next_value: i64 = modpow(n, exponent, modulo)
if next_value == 0 || next_value == exponent {
return next_value
}
exponent = next_value
guard = guard + 1
}
return 0
}
function main() -> i32 {
let limit: i64 = 1000000
let modulo: i64 = 1000000000
let mut total: i64 = 0
let mut i: i64 = 2
while i <= limit {
total = total + f_n(i, modulo)
i = i + 1
}
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; }
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t f_n_i64_i64(int64_t n, int64_t modulo);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base0), (mod));
int64_t e = exp0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
__int128 t = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))));
r = ((int64_t)(t));
}
__int128 t2 = FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))));
b = ((int64_t)(t2));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t f_n_i64_i64(int64_t n, int64_t modulo) {
int64_t exponent = n;
int32_t guard = 0;
while (guard < 64) {
int64_t next_value = modpow_i64_i64_i64(n, exponent, modulo);
if ((next_value == 0 || next_value == exponent)) {
return next_value;
}
exponent = next_value;
guard = (guard + 1);
}
return 0;
}
int32_t main(void) {
int64_t limit = 1000000;
int64_t modulo = 1000000000;
int64_t total = 0;
int64_t i = 2;
while (i <= limit) {
total = (total + f_n_i64_i64(i, modulo));
i = (i + 1);
}
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 @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
%4 = arith.remsi %arg0, %arg2 : i64
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
llvm.store %4, %6 : i64, !llvm.ptr
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%9 = llvm.load %8 : !llvm.ptr -> i64
%10 = arith.constant 0 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.cmpi sgt, %9, %12 : i64
cf.cond_br %11, ^bb1, ^bb2
^bb1:
%13 = llvm.load %8 : !llvm.ptr -> i64
%14 = arith.constant 2 : i32
%16 = arith.extsi %14 : i32 to i64
%15 = arith.remsi %13, %16 : i64
%17 = arith.constant 1 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi eq, %15, %19 : i64
cf.cond_br %18, ^bb3, ^bb4
^bb3:
%20 = llvm.load %3 : !llvm.ptr -> i64
%21 = arith.extsi %20 : i64 to i128
%22 = llvm.load %6 : !llvm.ptr -> i64
%23 = arith.extsi %22 : i64 to i128
%25 = arith.trunci %21 : i128 to i64
%26 = arith.trunci %23 : i128 to i64
%24 = arith.muli %25, %26 : i64
%27 = arith.extsi %arg2 : i64 to i128
%29 = arith.trunci %27 : i128 to i64
%28 = arith.remsi %24, %29 : i64
%30 = arith.extsi %28 : i64 to i128
%31 = arith.trunci %30 : i128 to i64
llvm.store %31, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%32 = llvm.load %6 : !llvm.ptr -> i64
%33 = arith.extsi %32 : i64 to i128
%34 = llvm.load %6 : !llvm.ptr -> i64
%35 = arith.extsi %34 : i64 to i128
%37 = arith.trunci %33 : i128 to i64
%38 = arith.trunci %35 : i128 to i64
%36 = arith.muli %37, %38 : i64
%39 = arith.extsi %arg2 : i64 to i128
%41 = arith.trunci %39 : i128 to i64
%40 = arith.remsi %36, %41 : i64
%42 = arith.extsi %40 : i64 to i128
%43 = arith.trunci %42 : i128 to i64
llvm.store %43, %6 : i64, !llvm.ptr
%44 = llvm.load %8 : !llvm.ptr -> i64
%45 = arith.constant 2 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.divsi %44, %47 : i64
llvm.store %46, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%48 = llvm.load %3 : !llvm.ptr -> i64
func.return %48 : i64
}
func.func @f_n(%arg0: i64, %arg1: i64) -> i64 {
%49 = llvm.mlir.constant(1 : i64) : i64
%50 = llvm.alloca %49 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %50 : i64, !llvm.ptr
%51 = arith.constant 0 : i32
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x i32 : (i64) -> !llvm.ptr
llvm.store %51, %53 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%54 = llvm.load %53 : !llvm.ptr -> i32
%55 = arith.constant 64 : i32
%56 = arith.cmpi slt, %54, %55 : i32
cf.cond_br %56, ^bb7, ^bb8
^bb7:
%58 = llvm.load %50 : !llvm.ptr -> i64
%57 = func.call @modpow(%arg0, %58, %arg1) : (i64, i64, i64) -> i64
%59 = arith.constant 0 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.cmpi eq, %57, %61 : i64
%62 = scf.if %60 -> (i1) {
%63 = arith.constant true
scf.yield %63 : i1
} else {
%64 = llvm.load %50 : !llvm.ptr -> i64
%65 = arith.cmpi eq, %57, %64 : i64
scf.yield %65 : i1
}
cf.cond_br %62, ^bb9, ^bb10
^bb9:
func.return %57 : i64
^bb10:
cf.br ^bb11
^bb11:
llvm.store %57, %50 : i64, !llvm.ptr
%66 = llvm.load %53 : !llvm.ptr -> i32
%67 = arith.constant 1 : i32
%68 = arith.addi %66, %67 : i32
llvm.store %68, %53 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%69 = arith.constant 0 : i32
%70 = arith.extsi %69 : i32 to i64
func.return %70 : i64
}
func.func @main() -> i32 {
%71 = arith.constant 1000000 : i32
%72 = arith.extsi %71 : i32 to i64
%73 = arith.constant 1000000000 : i32
%74 = arith.extsi %73 : i32 to i64
%75 = arith.constant 0 : i32
%76 = arith.extsi %75 : i32 to i64
%77 = llvm.mlir.constant(1 : i64) : i64
%78 = llvm.alloca %77 x i64 : (i64) -> !llvm.ptr
llvm.store %76, %78 : i64, !llvm.ptr
%79 = arith.constant 2 : i32
%80 = arith.extsi %79 : i32 to i64
%81 = llvm.mlir.constant(1 : i64) : i64
%82 = llvm.alloca %81 x i64 : (i64) -> !llvm.ptr
llvm.store %80, %82 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%83 = llvm.load %82 : !llvm.ptr -> i64
%84 = arith.cmpi sle, %83, %72 : i64
cf.cond_br %84, ^bb13, ^bb14
^bb13:
%85 = llvm.load %78 : !llvm.ptr -> i64
%87 = llvm.load %82 : !llvm.ptr -> i64
%86 = func.call @f_n(%87, %74) : (i64, i64) -> i64
%88 = arith.addi %85, %86 : i64
llvm.store %88, %78 : i64, !llvm.ptr
%89 = llvm.load %82 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
llvm.store %91, %82 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%93 = llvm.mlir.addressof @str_0 : !llvm.ptr
%94 = llvm.load %78 : !llvm.ptr -> i64
%95 = llvm.call @printf(%93, %94) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%96 = arith.constant 0 : i32
func.return %96 : i32
}
}