← All problems
Problem 048
Last ten digits of 1^1 + 2^2 + … + 1000^1000. Use mulmod to avoid i64 overflow when reducing mod 10^10.
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(1)
Approach Flow solution Modular exponentiation
Verdict Optimal
Flow source
# Project Euler 048
# Last ten digits of 1^1 + 2^2 + … + 1000^1000.
# Use mulmod to avoid i64 overflow when reducing mod 10^10.
function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
let mut a: i64 = a0 % mod
let mut b: i64 = b0 % mod
let mut result: i64 = 0
while b > 0 {
if b % 2 == 1 {
result = (result + a) % mod
}
a = (a * 2) % mod
b = b / 2
}
return result
}
function mod_pow(base: i64, exp: i64, mod: i64) -> i64 {
let mut result: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 {
result = mulmod(result, b, mod)
}
b = mulmod(b, b, mod)
e = e / 2
}
return result
}
function main() -> i32 {
let mod: i64 = 10000000000
let mut total: i64 = 0
let mut n: i64 = 1
while n <= 1000 {
total = (total + mod_pow(n, n, mod)) % mod
n = n + 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 mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int32_t main(void);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
int32_t main(void) {
int64_t mod = 10000000000;
int64_t total = 0;
int64_t n = 1;
while (n <= 1000) {
total = FLOW_CHECKED_MOD(((total + mod_pow_i64_i64_i64(n, n, mod))), (mod));
n = (n + 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 @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.remsi %arg0, %arg2 : i64
%1 = llvm.mlir.constant(1 : i64) : i64
%2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
llvm.store %0, %2 : i64, !llvm.ptr
%3 = arith.remsi %arg1, %arg2 : 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 = arith.extsi %6 : i32 to i64
%8 = llvm.mlir.constant(1 : i64) : i64
%9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
llvm.store %7, %9 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%10 = llvm.load %5 : !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 %5 : !llvm.ptr -> i64
%15 = arith.constant 2 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.remsi %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 %9 : !llvm.ptr -> i64
%22 = llvm.load %2 : !llvm.ptr -> i64
%23 = arith.addi %21, %22 : i64
%24 = arith.remsi %23, %arg2 : i64
llvm.store %24, %9 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%25 = llvm.load %2 : !llvm.ptr -> i64
%26 = arith.constant 2 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.muli %25, %28 : i64
%29 = arith.remsi %27, %arg2 : i64
llvm.store %29, %2 : i64, !llvm.ptr
%30 = llvm.load %5 : !llvm.ptr -> i64
%31 = arith.constant 2 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.divsi %30, %33 : i64
llvm.store %32, %5 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%34 = llvm.load %9 : !llvm.ptr -> i64
func.return %34 : i64
}
func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%35 = arith.constant 1 : i32
%36 = arith.extsi %35 : i32 to i64
%37 = llvm.mlir.constant(1 : i64) : i64
%38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
llvm.store %36, %38 : i64, !llvm.ptr
%39 = arith.remsi %arg0, %arg2 : i64
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i64, !llvm.ptr
%42 = llvm.mlir.constant(1 : i64) : i64
%43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %43 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%44 = llvm.load %43 : !llvm.ptr -> i64
%45 = arith.constant 0 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.cmpi sgt, %44, %47 : i64
cf.cond_br %46, ^bb7, ^bb8
^bb7:
%48 = llvm.load %43 : !llvm.ptr -> i64
%49 = arith.constant 2 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.remsi %48, %51 : i64
%52 = arith.constant 1 : i32
%54 = arith.extsi %52 : i32 to i64
%53 = arith.cmpi eq, %50, %54 : i64
cf.cond_br %53, ^bb9, ^bb10
^bb9:
%56 = llvm.load %38 : !llvm.ptr -> i64
%57 = llvm.load %41 : !llvm.ptr -> i64
%55 = func.call @mulmod(%56, %57, %arg2) : (i64, i64, i64) -> i64
llvm.store %55, %38 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%59 = llvm.load %41 : !llvm.ptr -> i64
%60 = llvm.load %41 : !llvm.ptr -> i64
%58 = func.call @mulmod(%59, %60, %arg2) : (i64, i64, i64) -> i64
llvm.store %58, %41 : i64, !llvm.ptr
%61 = llvm.load %43 : !llvm.ptr -> i64
%62 = arith.constant 2 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.divsi %61, %64 : i64
llvm.store %63, %43 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%65 = llvm.load %38 : !llvm.ptr -> i64
func.return %65 : i64
}
func.func @main() -> i32 {
%66 = arith.constant 5705032704 : i32
%67 = arith.extsi %66 : i32 to i64
%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
%72 = arith.constant 1 : 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 ^bb12
^bb12:
%76 = llvm.load %75 : !llvm.ptr -> i64
%77 = arith.constant 1000 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.cmpi sle, %76, %79 : i64
cf.cond_br %78, ^bb13, ^bb14
^bb13:
%80 = llvm.load %71 : !llvm.ptr -> i64
%82 = llvm.load %75 : !llvm.ptr -> i64
%83 = llvm.load %75 : !llvm.ptr -> i64
%81 = func.call @mod_pow(%82, %83, %67) : (i64, i64, i64) -> i64
%84 = arith.addi %80, %81 : i64
%85 = arith.remsi %84, %67 : i64
llvm.store %85, %71 : i64, !llvm.ptr
%86 = llvm.load %75 : !llvm.ptr -> i64
%87 = arith.constant 1 : i32
%89 = arith.extsi %87 : i32 to i64
%88 = arith.addi %86, %89 : i64
llvm.store %88, %75 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%90 = llvm.mlir.addressof @str_0 : !llvm.ptr
%91 = llvm.load %71 : !llvm.ptr -> i64
%92 = llvm.call @printf(%90, %91) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%93 = arith.constant 0 : i32
func.return %93 : i32
}
}