← All problems
Problem 717
Summation of a Modular Formula: G(10^7). For each odd prime p, compute the formula using modular exponentiation with i128 intermediates to avoid overflow.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(log n)
Space complexity O(n)O(1)
Approach Flow solution Modular exponentiation
Verdict Suboptimal
Flow source
# Project Euler 717
# Summation of a Modular Formula: G(10^7).
# For each odd prime p, compute the formula using modular exponentiation
# with i128 intermediates to avoid overflow.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const N: i64 = 10000000
function modpow(b: i64, e: i64, m: i64) -> i64 {
let mut r: i64 = 1 % m
let mut bb: i64 = b % m
let mut ee: i64 = e
while ee > 0 {
if ee % 2 == 1 {
r = ((r as i128) * (bb as i128) % (m as i128)) as i64
}
bb = ((bb as i128) * (bb as i128) % (m as i128)) as i64
ee = ee / 2
}
return r
}
function main() -> i32 {
let comp: ptr<i8> = calloc(N + 1, 1)
if comp == null {
return 1
}
let mut i: i64 = 2
while i * i <= N {
if comp[i] == 0 {
let mut j: i64 = i * i
while j <= N {
comp[j] = 1
j = j + i
}
}
i = i + 1
}
let mut total: i64 = 0
let mut p: i64 = 3
while p <= N {
if comp[p] == 0 {
let x: i64 = modpow(2, p, p - 1)
let r: i64 = modpow(2, x, p)
let n: i64 = r * (p - 1) / (2 * p) + 1
let modpp: i64 = p * p
let pp: i64 = modpow(2, p - 1, modpp)
let va: i128 = (r as i128) * ((p - 1) as i128) % (modpp as i128)
let vb: i128 = 0 - va
let vc: i128 = vb * (pp as i128) % (modpp as i128)
let mut t: i64 = ((vc + 1) % (modpp as i128)) as i64
if t < 0 {
t = t + modpp
}
let fp: i64 = t / p
total = total + (fp + 2 * n) % p
}
p = p + 1
}
free(comp as ptr<void>)
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 b, int64_t e, int64_t m);
int32_t main(void);
static const int64_t N = 10000000;
int64_t modpow_i64_i64_i64(int64_t b, int64_t e, int64_t m) {
int64_t r = FLOW_CHECKED_MOD((1), (m));
int64_t bb = FLOW_CHECKED_MOD((b), (m));
int64_t ee = e;
while (ee > 0) {
if (FLOW_CHECKED_MOD((ee), (2)) == 1) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(bb)))), (((__int128)(m))))));
}
bb = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(bb)) * ((__int128)(bb)))), (((__int128)(m))))));
ee = FLOW_CHECKED_DIV((ee), (2));
}
return r;
}
int32_t main(void) {
int8_t* comp = (int8_t*)(calloc((N + 1), 1));
if (comp == NULL) {
return 1;
}
int64_t i = 2;
while ((i * i) <= N) {
if (comp[i] == 0) {
int64_t j = (i * i);
while (j <= N) {
comp[j] = 1;
j = (j + i);
}
}
i = (i + 1);
}
int64_t total = 0;
int64_t p = 3;
while (p <= N) {
if (comp[p] == 0) {
int64_t x = modpow_i64_i64_i64(2, p, (p - 1));
int64_t r = modpow_i64_i64_i64(2, x, p);
int64_t n = (FLOW_CHECKED_DIV(((r * (p - 1))), ((2 * p))) + 1);
int64_t modpp = (p * p);
int64_t pp = modpow_i64_i64_i64(2, (p - 1), modpp);
__int128 va = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)((p - 1))))), (((__int128)(modpp))));
__int128 vb = (0 - va);
__int128 vc = FLOW_CHECKED_MOD(((vb * ((__int128)(pp)))), (((__int128)(modpp))));
int64_t t = ((int64_t)(FLOW_CHECKED_MOD(((vc + 1)), (((__int128)(modpp))))));
if (t < 0) {
t = (t + modpp);
}
int64_t fp = FLOW_CHECKED_DIV((t), (p));
total = (total + FLOW_CHECKED_MOD(((fp + (2 * n))), (p)));
}
p = (p + 1);
}
free(((void*)(comp)));
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 private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
// Constant: N
llvm.mlir.global internal constant @N(10000000 : i64) : i64
func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 1 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.remsi %2, %arg2 : i64
%3 = llvm.mlir.constant(1 : i64) : i64
%4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %4 : i64, !llvm.ptr
%5 = arith.remsi %arg0, %arg2 : i64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : i64, !llvm.ptr
%8 = llvm.mlir.constant(1 : i64) : i64
%9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %9 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%10 = llvm.load %9 : !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 %9 : !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 %4 : !llvm.ptr -> i64
%22 = arith.extsi %21 : i64 to i128
%23 = llvm.load %7 : !llvm.ptr -> i64
%24 = arith.extsi %23 : i64 to i128
%26 = arith.trunci %22 : i128 to i64
%27 = arith.trunci %24 : i128 to i64
%25 = arith.muli %26, %27 : i64
%28 = arith.extsi %arg2 : i64 to i128
%30 = arith.trunci %28 : i128 to i64
%29 = arith.remsi %25, %30 : i64
llvm.store %29, %4 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%31 = llvm.load %7 : !llvm.ptr -> i64
%32 = arith.extsi %31 : i64 to i128
%33 = llvm.load %7 : !llvm.ptr -> i64
%34 = arith.extsi %33 : i64 to i128
%36 = arith.trunci %32 : i128 to i64
%37 = arith.trunci %34 : i128 to i64
%35 = arith.muli %36, %37 : i64
%38 = arith.extsi %arg2 : i64 to i128
%40 = arith.trunci %38 : i128 to i64
%39 = arith.remsi %35, %40 : i64
llvm.store %39, %7 : i64, !llvm.ptr
%41 = llvm.load %9 : !llvm.ptr -> i64
%42 = arith.constant 2 : i32
%44 = arith.extsi %42 : i32 to i64
%43 = arith.divsi %41, %44 : i64
llvm.store %43, %9 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%45 = llvm.load %4 : !llvm.ptr -> i64
func.return %45 : i64
}
func.func @main() -> i32 {
%47 = llvm.mlir.addressof @N : !llvm.ptr
%48 = llvm.load %47 : !llvm.ptr -> i64
%49 = arith.constant 1 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.addi %48, %51 : i64
%52 = arith.constant 1 : i32
%53 = arith.extsi %52 : i32 to i64
%46 = func.call @calloc(%50, %53) : (i64, i64) -> !llvm.ptr
%54 = llvm.mlir.zero : !llvm.ptr
%55 = llvm.icmp "eq" %46, %54 : !llvm.ptr
cf.cond_br %55, ^bb6, ^bb7
^bb6:
%56 = arith.constant 1 : i32
func.return %56 : i32
^bb7:
cf.br ^bb8
^bb8:
%57 = arith.constant 2 : i32
%58 = arith.extsi %57 : i32 to i64
%59 = llvm.mlir.constant(1 : i64) : i64
%60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
llvm.store %58, %60 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%61 = llvm.load %60 : !llvm.ptr -> i64
%62 = llvm.load %60 : !llvm.ptr -> i64
%63 = arith.muli %61, %62 : i64
%64 = llvm.mlir.addressof @N : !llvm.ptr
%65 = llvm.load %64 : !llvm.ptr -> i64
%66 = arith.cmpi sle, %63, %65 : i64
cf.cond_br %66, ^bb10, ^bb11
^bb10:
%68 = llvm.load %60 : !llvm.ptr -> i64
%69 = llvm.getelementptr %46[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%67 = llvm.load %69 : !llvm.ptr -> i8
%70 = arith.constant 0 : i32
%72 = arith.extsi %67 : i8 to i32
%71 = arith.cmpi eq, %72, %70 : i32
cf.cond_br %71, ^bb12, ^bb13
^bb12:
%73 = llvm.load %60 : !llvm.ptr -> i64
%74 = llvm.load %60 : !llvm.ptr -> i64
%75 = arith.muli %73, %74 : i64
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
llvm.store %75, %77 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%78 = llvm.load %77 : !llvm.ptr -> i64
%79 = llvm.mlir.addressof @N : !llvm.ptr
%80 = llvm.load %79 : !llvm.ptr -> i64
%81 = arith.cmpi sle, %78, %80 : i64
cf.cond_br %81, ^bb16, ^bb17
^bb16:
%82 = arith.constant 1 : i32
%83 = llvm.load %77 : !llvm.ptr -> i64
%84 = arith.trunci %82 : i32 to i8
%85 = llvm.getelementptr %46[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %84, %85 : i8, !llvm.ptr
%86 = llvm.load %77 : !llvm.ptr -> i64
%87 = llvm.load %60 : !llvm.ptr -> i64
%88 = arith.addi %86, %87 : i64
llvm.store %88, %77 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%89 = llvm.load %60 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
llvm.store %91, %60 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%93 = arith.constant 0 : i32
%94 = arith.extsi %93 : i32 to i64
%95 = llvm.mlir.constant(1 : i64) : i64
%96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
llvm.store %94, %96 : i64, !llvm.ptr
%97 = arith.constant 3 : i32
%98 = arith.extsi %97 : i32 to i64
%99 = llvm.mlir.constant(1 : i64) : i64
%100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
llvm.store %98, %100 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%101 = llvm.load %100 : !llvm.ptr -> i64
%102 = llvm.mlir.addressof @N : !llvm.ptr
%103 = llvm.load %102 : !llvm.ptr -> i64
%104 = arith.cmpi sle, %101, %103 : i64
cf.cond_br %104, ^bb19, ^bb20
^bb19:
%106 = llvm.load %100 : !llvm.ptr -> i64
%107 = llvm.getelementptr %46[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%105 = llvm.load %107 : !llvm.ptr -> i8
%108 = arith.constant 0 : i32
%110 = arith.extsi %105 : i8 to i32
%109 = arith.cmpi eq, %110, %108 : i32
cf.cond_br %109, ^bb21, ^bb22
^bb21:
%112 = arith.constant 2 : i32
%113 = llvm.load %100 : !llvm.ptr -> i64
%114 = llvm.load %100 : !llvm.ptr -> i64
%115 = arith.constant 1 : i32
%117 = arith.extsi %115 : i32 to i64
%116 = arith.subi %114, %117 : i64
%118 = arith.extsi %112 : i32 to i64
%111 = func.call @modpow(%118, %113, %116) : (i64, i64, i64) -> i64
%120 = arith.constant 2 : i32
%121 = llvm.load %100 : !llvm.ptr -> i64
%122 = arith.extsi %120 : i32 to i64
%119 = func.call @modpow(%122, %111, %121) : (i64, i64, i64) -> i64
%123 = llvm.load %100 : !llvm.ptr -> i64
%124 = arith.constant 1 : i32
%126 = arith.extsi %124 : i32 to i64
%125 = arith.subi %123, %126 : i64
%127 = arith.muli %119, %125 : i64
%128 = arith.constant 2 : i32
%129 = llvm.load %100 : !llvm.ptr -> i64
%131 = arith.extsi %128 : i32 to i64
%130 = arith.muli %131, %129 : i64
%132 = arith.divsi %127, %130 : i64
%133 = arith.constant 1 : i32
%135 = arith.extsi %133 : i32 to i64
%134 = arith.addi %132, %135 : i64
%136 = llvm.load %100 : !llvm.ptr -> i64
%137 = llvm.load %100 : !llvm.ptr -> i64
%138 = arith.muli %136, %137 : i64
%140 = arith.constant 2 : i32
%141 = llvm.load %100 : !llvm.ptr -> i64
%142 = arith.constant 1 : i32
%144 = arith.extsi %142 : i32 to i64
%143 = arith.subi %141, %144 : i64
%145 = arith.extsi %140 : i32 to i64
%139 = func.call @modpow(%145, %143, %138) : (i64, i64, i64) -> i64
%146 = arith.extsi %119 : i64 to i128
%147 = llvm.load %100 : !llvm.ptr -> i64
%148 = arith.constant 1 : i32
%150 = arith.extsi %148 : i32 to i64
%149 = arith.subi %147, %150 : i64
%151 = arith.extsi %149 : i64 to i128
%153 = arith.trunci %146 : i128 to i64
%154 = arith.trunci %151 : i128 to i64
%152 = arith.muli %153, %154 : i64
%155 = arith.extsi %138 : i64 to i128
%157 = arith.trunci %155 : i128 to i64
%156 = arith.remsi %152, %157 : i64
%158 = arith.extsi %156 : i64 to i128
%159 = arith.constant 0 : i32
%161 = arith.extsi %159 : i32 to i64
%162 = arith.trunci %158 : i128 to i64
%160 = arith.subi %161, %162 : i64
%163 = arith.extsi %160 : i64 to i128
%164 = arith.extsi %139 : i64 to i128
%166 = arith.trunci %163 : i128 to i64
%167 = arith.trunci %164 : i128 to i64
%165 = arith.muli %166, %167 : i64
%168 = arith.extsi %138 : i64 to i128
%170 = arith.trunci %168 : i128 to i64
%169 = arith.remsi %165, %170 : i64
%171 = arith.extsi %169 : i64 to i128
%172 = arith.constant 1 : i32
%174 = arith.trunci %171 : i128 to i64
%175 = arith.extsi %172 : i32 to i64
%173 = arith.addi %174, %175 : i64
%176 = arith.extsi %138 : i64 to i128
%178 = arith.trunci %176 : i128 to i64
%177 = arith.remsi %173, %178 : i64
%179 = llvm.mlir.constant(1 : i64) : i64
%180 = llvm.alloca %179 x i64 : (i64) -> !llvm.ptr
llvm.store %177, %180 : i64, !llvm.ptr
%181 = llvm.load %180 : !llvm.ptr -> i64
%182 = arith.constant 0 : i32
%184 = arith.extsi %182 : i32 to i64
%183 = arith.cmpi slt, %181, %184 : i64
cf.cond_br %183, ^bb24, ^bb25
^bb24:
%185 = llvm.load %180 : !llvm.ptr -> i64
%186 = arith.addi %185, %138 : i64
llvm.store %186, %180 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%187 = llvm.load %180 : !llvm.ptr -> i64
%188 = llvm.load %100 : !llvm.ptr -> i64
%189 = arith.divsi %187, %188 : i64
%190 = llvm.load %96 : !llvm.ptr -> i64
%191 = arith.constant 2 : i32
%193 = arith.extsi %191 : i32 to i64
%192 = arith.muli %193, %134 : i64
%194 = arith.addi %189, %192 : i64
%195 = llvm.load %100 : !llvm.ptr -> i64
%196 = arith.remsi %194, %195 : i64
%197 = arith.addi %190, %196 : i64
llvm.store %197, %96 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%198 = llvm.load %100 : !llvm.ptr -> i64
%199 = arith.constant 1 : i32
%201 = arith.extsi %199 : i32 to i64
%200 = arith.addi %198, %201 : i64
llvm.store %200, %100 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
func.call @free(%46) : (!llvm.ptr) -> ()
%203 = llvm.mlir.addressof @str_0 : !llvm.ptr
%204 = llvm.load %96 : !llvm.ptr -> i64
%205 = llvm.call @printf(%203, %204) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%206 = arith.constant 0 : i32
func.return %206 : i32
}
}