← All problems
Problem 097
Last ten digits of 28433 × 2^7830457 + 1.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(log n)
Space complexity O(1)O(1)
Approach Flow solution Modular exponentiation
Verdict Suboptimal
Flow source
# Project Euler 097
# Last ten digits of 28433 × 2^7830457 + 1.
function mod_mul(a: i64, b: i64, mod: i64) -> i64 {
# overflow-safe multiply via addition
let mut res: i64 = 0
let mut x: i64 = a % mod
let mut y: i64 = b
if x < 0 { x = x + mod }
while y > 0 {
if y % 2 == 1 {
res = res + x
if res >= mod { res = res - mod }
}
x = x + x
if x >= mod { x = x - mod }
y = y / 2
}
return res
}
function mod_pow(base: i64, exp: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 {
r = mod_mul(r, b, mod)
}
b = mod_mul(b, b, mod)
e = e / 2
}
return r
}
function main() -> i32 {
let mod: i64 = 10000000000
let p: i64 = mod_pow(2, 7830457, mod)
let ans: i64 = (mod_mul(28433, p, mod) + 1) % mod
printf("%lld\n", ans)
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 mod_mul_i64_i64_i64(int64_t a, int64_t b, 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 mod_mul_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
int64_t res = 0;
int64_t x = FLOW_CHECKED_MOD((a), (mod));
int64_t y = b;
if (x < 0) {
x = (x + mod);
}
while (y > 0) {
if (FLOW_CHECKED_MOD((y), (2)) == 1) {
res = (res + x);
if (res >= mod) {
res = (res - mod);
}
}
x = (x + x);
if (x >= mod) {
x = (x - mod);
}
y = FLOW_CHECKED_DIV((y), (2));
}
return res;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = mod_mul_i64_i64_i64(r, b, mod);
}
b = mod_mul_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int32_t main(void) {
int64_t mod = 10000000000;
int64_t p = mod_pow_i64_i64_i64(2, 7830457, mod);
int64_t ans = FLOW_CHECKED_MOD(((mod_mul_i64_i64_i64(28433, p, mod) + 1)), (mod));
printf("%lld\n", ans);
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 @mod_mul(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 0 : 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
%9 = llvm.load %6 : !llvm.ptr -> i64
%10 = arith.constant 0 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.cmpi slt, %9, %12 : i64
cf.cond_br %11, ^bb0, ^bb1
^bb0:
%13 = llvm.load %6 : !llvm.ptr -> i64
%14 = arith.addi %13, %arg2 : i64
llvm.store %14, %6 : i64, !llvm.ptr
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
cf.br ^bb3
^bb3:
%15 = llvm.load %8 : !llvm.ptr -> i64
%16 = arith.constant 0 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.cmpi sgt, %15, %18 : i64
cf.cond_br %17, ^bb4, ^bb5
^bb4:
%19 = llvm.load %8 : !llvm.ptr -> i64
%20 = arith.constant 2 : i32
%22 = arith.extsi %20 : i32 to i64
%21 = arith.remsi %19, %22 : i64
%23 = arith.constant 1 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.cmpi eq, %21, %25 : i64
cf.cond_br %24, ^bb6, ^bb7
^bb6:
%26 = llvm.load %3 : !llvm.ptr -> i64
%27 = llvm.load %6 : !llvm.ptr -> i64
%28 = arith.addi %26, %27 : i64
llvm.store %28, %3 : i64, !llvm.ptr
%29 = llvm.load %3 : !llvm.ptr -> i64
%30 = arith.cmpi sge, %29, %arg2 : i64
cf.cond_br %30, ^bb9, ^bb10
^bb9:
%31 = llvm.load %3 : !llvm.ptr -> i64
%32 = arith.subi %31, %arg2 : i64
llvm.store %32, %3 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%33 = llvm.load %6 : !llvm.ptr -> i64
%34 = llvm.load %6 : !llvm.ptr -> i64
%35 = arith.addi %33, %34 : i64
llvm.store %35, %6 : i64, !llvm.ptr
%36 = llvm.load %6 : !llvm.ptr -> i64
%37 = arith.cmpi sge, %36, %arg2 : i64
cf.cond_br %37, ^bb12, ^bb13
^bb12:
%38 = llvm.load %6 : !llvm.ptr -> i64
%39 = arith.subi %38, %arg2 : i64
llvm.store %39, %6 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%40 = llvm.load %8 : !llvm.ptr -> i64
%41 = arith.constant 2 : i32
%43 = arith.extsi %41 : i32 to i64
%42 = arith.divsi %40, %43 : i64
llvm.store %42, %8 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%44 = llvm.load %3 : !llvm.ptr -> i64
func.return %44 : i64
}
func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%45 = arith.constant 1 : i32
%46 = arith.extsi %45 : i32 to i64
%47 = llvm.mlir.constant(1 : i64) : i64
%48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
llvm.store %46, %48 : i64, !llvm.ptr
%49 = arith.remsi %arg0, %arg2 : i64
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
llvm.store %49, %51 : i64, !llvm.ptr
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %53 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%54 = llvm.load %53 : !llvm.ptr -> i64
%55 = arith.constant 0 : i32
%57 = arith.extsi %55 : i32 to i64
%56 = arith.cmpi sgt, %54, %57 : i64
cf.cond_br %56, ^bb16, ^bb17
^bb16:
%58 = llvm.load %53 : !llvm.ptr -> i64
%59 = arith.constant 2 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.remsi %58, %61 : i64
%62 = arith.constant 1 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.cmpi eq, %60, %64 : i64
cf.cond_br %63, ^bb18, ^bb19
^bb18:
%66 = llvm.load %48 : !llvm.ptr -> i64
%67 = llvm.load %51 : !llvm.ptr -> i64
%65 = func.call @mod_mul(%66, %67, %arg2) : (i64, i64, i64) -> i64
llvm.store %65, %48 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%69 = llvm.load %51 : !llvm.ptr -> i64
%70 = llvm.load %51 : !llvm.ptr -> i64
%68 = func.call @mod_mul(%69, %70, %arg2) : (i64, i64, i64) -> i64
llvm.store %68, %51 : i64, !llvm.ptr
%71 = llvm.load %53 : !llvm.ptr -> i64
%72 = arith.constant 2 : i32
%74 = arith.extsi %72 : i32 to i64
%73 = arith.divsi %71, %74 : i64
llvm.store %73, %53 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%75 = llvm.load %48 : !llvm.ptr -> i64
func.return %75 : i64
}
func.func @main() -> i32 {
%76 = arith.constant 5705032704 : i32
%77 = arith.extsi %76 : i32 to i64
%79 = arith.constant 2 : i32
%80 = arith.constant 7830457 : i32
%81 = arith.extsi %79 : i32 to i64
%82 = arith.extsi %80 : i32 to i64
%78 = func.call @mod_pow(%81, %82, %77) : (i64, i64, i64) -> i64
%84 = arith.constant 28433 : i32
%85 = arith.extsi %84 : i32 to i64
%83 = func.call @mod_mul(%85, %78, %77) : (i64, i64, i64) -> i64
%86 = arith.constant 1 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.addi %83, %88 : i64
%89 = arith.remsi %87, %77 : i64
%90 = llvm.mlir.addressof @str_0 : !llvm.ptr
%91 = llvm.call @printf(%90, %89) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%92 = arith.constant 0 : i32
func.return %92 : i32
}
}