← All problems
Problem 335
Sum_{i=0}^{10^18} M(2^i+1) mod 7^9 via closed form.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 335
# Sum_{i=0}^{10^18} M(2^i+1) mod 7^9 via closed form.
function modpow(base: i64, exp: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = ((base % mod) + mod) % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 { r = (r * b) % mod }
b = (b * b) % mod
e = e / 2
}
return r
}
function modinv(a0: i64, m: i64) -> i64 {
# extended Euclid
let mut a: i64 = ((a0 % m) + m) % m
let mut b: i64 = m
let mut x0: i64 = 1
let mut x1: i64 = 0
while b != 0 {
let q: i64 = a / b
let t: i64 = a - q * b
a = b
b = t
let tx: i64 = x0 - q * x1
x0 = x1
x1 = tx
}
return ((x0 % m) + m) % m
}
function mod_div(a: i64, b: i64, m: i64) -> i64 {
return (((a % m) + m) % m) * modinv(b, m) % m
}
function main() -> i32 {
let limit: i64 = 1000000000000000000
let mod: i64 = 40353607
let t2: i64 = (modpow(2, limit + 2, mod) + mod - 2) % mod
let t3: i64 = mod_div((modpow(3, limit + 1, mod) + mod - 1) % mod, 2, mod)
let t4: i64 = mod_div((modpow(4, limit + 1, mod) + mod - 1) % mod, 3, mod)
let ans: i64 = (t2 - t3 + mod + t4) % 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 modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t modinv_i64_i64(int64_t a0, int64_t m);
int64_t mod_div_i64_i64_i64(int64_t a, int64_t b, int64_t m);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((base), (mod)) + mod)), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * b)), (mod));
}
b = FLOW_CHECKED_MOD(((b * b)), (mod));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t modinv_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((a0), (m)) + m)), (m));
int64_t b = m;
int64_t x0 = 1;
int64_t x1 = 0;
while (b != 0) {
int64_t q = FLOW_CHECKED_DIV((a), (b));
int64_t t = (a - (q * b));
a = b;
b = t;
int64_t tx = (x0 - (q * x1));
x0 = x1;
x1 = tx;
}
return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((x0), (m)) + m)), (m));
}
int64_t mod_div_i64_i64_i64(int64_t a, int64_t b, int64_t m) {
return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((a), (m)) + m)), (m)) * modinv_i64_i64(b, m))), (m));
}
int32_t main(void) {
int64_t limit = 1000000000000000000;
int64_t mod = 40353607;
int64_t t2 = FLOW_CHECKED_MOD((((modpow_i64_i64_i64(2, (limit + 2), mod) + mod) - 2)), (mod));
int64_t t3 = mod_div_i64_i64_i64(FLOW_CHECKED_MOD((((modpow_i64_i64_i64(3, (limit + 1), mod) + mod) - 1)), (mod)), 2, mod);
int64_t t4 = mod_div_i64_i64_i64(FLOW_CHECKED_MOD((((modpow_i64_i64_i64(4, (limit + 1), mod) + mod) - 1)), (mod)), 3, mod);
int64_t ans = FLOW_CHECKED_MOD(((((t2 - t3) + mod) + t4)), (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 @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 = arith.addi %4, %arg2 : i64
%6 = arith.remsi %5, %arg2 : i64
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %6, %8 : i64, !llvm.ptr
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %10 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%11 = llvm.load %10 : !llvm.ptr -> i64
%12 = arith.constant 0 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.cmpi sgt, %11, %14 : i64
cf.cond_br %13, ^bb1, ^bb2
^bb1:
%15 = llvm.load %10 : !llvm.ptr -> i64
%16 = arith.constant 2 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.remsi %15, %18 : i64
%19 = arith.constant 1 : i32
%21 = arith.extsi %19 : i32 to i64
%20 = arith.cmpi eq, %17, %21 : i64
cf.cond_br %20, ^bb3, ^bb4
^bb3:
%22 = llvm.load %3 : !llvm.ptr -> i64
%23 = llvm.load %8 : !llvm.ptr -> i64
%24 = arith.muli %22, %23 : i64
%25 = arith.remsi %24, %arg2 : i64
llvm.store %25, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%26 = llvm.load %8 : !llvm.ptr -> i64
%27 = llvm.load %8 : !llvm.ptr -> i64
%28 = arith.muli %26, %27 : i64
%29 = arith.remsi %28, %arg2 : i64
llvm.store %29, %8 : i64, !llvm.ptr
%30 = llvm.load %10 : !llvm.ptr -> i64
%31 = arith.constant 2 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.divsi %30, %33 : i64
llvm.store %32, %10 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%34 = llvm.load %3 : !llvm.ptr -> i64
func.return %34 : i64
}
func.func @modinv(%arg0: i64, %arg1: i64) -> i64 {
%35 = arith.remsi %arg0, %arg1 : i64
%36 = arith.addi %35, %arg1 : i64
%37 = arith.remsi %36, %arg1 : i64
%38 = llvm.mlir.constant(1 : i64) : i64
%39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
llvm.store %37, %39 : i64, !llvm.ptr
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %41 : i64, !llvm.ptr
%42 = arith.constant 1 : i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i64, !llvm.ptr
%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
cf.br ^bb6
^bb6:
%50 = llvm.load %41 : !llvm.ptr -> i64
%51 = arith.constant 0 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.cmpi ne, %50, %53 : i64
cf.cond_br %52, ^bb7, ^bb8
^bb7:
%54 = llvm.load %39 : !llvm.ptr -> i64
%55 = llvm.load %41 : !llvm.ptr -> i64
%56 = arith.divsi %54, %55 : i64
%57 = llvm.load %39 : !llvm.ptr -> i64
%58 = llvm.load %41 : !llvm.ptr -> i64
%59 = arith.muli %56, %58 : i64
%60 = arith.subi %57, %59 : i64
%61 = llvm.load %41 : !llvm.ptr -> i64
llvm.store %61, %39 : i64, !llvm.ptr
llvm.store %60, %41 : i64, !llvm.ptr
%62 = llvm.load %45 : !llvm.ptr -> i64
%63 = llvm.load %49 : !llvm.ptr -> i64
%64 = arith.muli %56, %63 : i64
%65 = arith.subi %62, %64 : i64
%66 = llvm.load %49 : !llvm.ptr -> i64
llvm.store %66, %45 : i64, !llvm.ptr
llvm.store %65, %49 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%67 = llvm.load %45 : !llvm.ptr -> i64
%68 = arith.remsi %67, %arg1 : i64
%69 = arith.addi %68, %arg1 : i64
%70 = arith.remsi %69, %arg1 : i64
func.return %70 : i64
}
func.func @mod_div(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%71 = arith.remsi %arg0, %arg2 : i64
%72 = arith.addi %71, %arg2 : i64
%73 = arith.remsi %72, %arg2 : i64
%74 = func.call @modinv(%arg1, %arg2) : (i64, i64) -> i64
%75 = arith.muli %73, %74 : i64
%76 = arith.remsi %75, %arg2 : i64
func.return %76 : i64
}
func.func @main() -> i32 {
%77 = arith.constant 999999995705032704 : i32
%78 = arith.extsi %77 : i32 to i64
%79 = arith.constant 40353607 : i32
%80 = arith.extsi %79 : i32 to i64
%82 = arith.constant 2 : i32
%83 = arith.constant 2 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.addi %78, %85 : i64
%86 = arith.extsi %82 : i32 to i64
%81 = func.call @modpow(%86, %84, %80) : (i64, i64, i64) -> i64
%87 = arith.addi %81, %80 : i64
%88 = arith.constant 2 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.subi %87, %90 : i64
%91 = arith.remsi %89, %80 : i64
%94 = arith.constant 3 : i32
%95 = arith.constant 1 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.addi %78, %97 : i64
%98 = arith.extsi %94 : i32 to i64
%93 = func.call @modpow(%98, %96, %80) : (i64, i64, i64) -> i64
%99 = arith.addi %93, %80 : i64
%100 = arith.constant 1 : i32
%102 = arith.extsi %100 : i32 to i64
%101 = arith.subi %99, %102 : i64
%103 = arith.remsi %101, %80 : i64
%104 = arith.constant 2 : i32
%105 = arith.extsi %104 : i32 to i64
%92 = func.call @mod_div(%103, %105, %80) : (i64, i64, i64) -> i64
%108 = arith.constant 4 : i32
%109 = arith.constant 1 : i32
%111 = arith.extsi %109 : i32 to i64
%110 = arith.addi %78, %111 : i64
%112 = arith.extsi %108 : i32 to i64
%107 = func.call @modpow(%112, %110, %80) : (i64, i64, i64) -> i64
%113 = arith.addi %107, %80 : i64
%114 = arith.constant 1 : i32
%116 = arith.extsi %114 : i32 to i64
%115 = arith.subi %113, %116 : i64
%117 = arith.remsi %115, %80 : i64
%118 = arith.constant 3 : i32
%119 = arith.extsi %118 : i32 to i64
%106 = func.call @mod_div(%117, %119, %80) : (i64, i64, i64) -> i64
%120 = arith.subi %91, %92 : i64
%121 = arith.addi %120, %80 : i64
%122 = arith.addi %121, %106 : i64
%123 = arith.remsi %122, %80 : i64
%124 = llvm.mlir.addressof @str_0 : !llvm.ptr
%125 = llvm.call @printf(%124, %123) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%126 = arith.constant 0 : i32
func.return %126 : i32
}
}