← All problems
Problem 381
sum_p S(p) for 5 ≤ p < 10^8, S(p)=(p-1)!+...+(p-5)! mod p. Closed form: S(p) ≡ -3 * 8^{-1} (mod p).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)?
Space complexity O(n)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 381
# sum_p S(p) for 5 ≤ p < 10^8, S(p)=(p-1)!+...+(p-5)! mod p.
# Closed form: S(p) ≡ -3 * 8^{-1} (mod p).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function modinv(a0: i64, m: i64) -> i64 {
let mut a: i64 = a0 % m
let mut t: i64 = 0
let mut newt: i64 = 1
let mut r: i64 = m
let mut newr: i64 = a
while newr != 0 {
let q: i64 = r / newr
let tmp: i64 = t - q * newt
t = newt
newt = tmp
let tmp2: i64 = r - q * newr
r = newr
newr = tmp2
}
if t < 0 { t = t + m }
return t
}
function main() -> i32 {
let LIMIT: i64 = 100000000
let sieve: ptr<i8> = calloc(LIMIT, 1)
if sieve == null { return 1 }
let mut i: i64 = 0
while i < LIMIT {
sieve[i] = 1
i = i + 1
}
sieve[0] = 0
sieve[1] = 0
let mut p: i64 = 2
while p * p < LIMIT {
if sieve[p] == 1 {
let mut m: i64 = p * p
while m < LIMIT {
sieve[m] = 0
m = m + p
}
}
p = p + 1
}
let mut total: i64 = 0
p = 5
while p < LIMIT {
if sieve[p] == 1 {
let inv8: i64 = modinv(8, p)
let s: i64 = (0 - 3 * inv8) % p
if s < 0 { s = s + p }
total = total + s
}
p = p + 1
}
printf("%lld\n", total)
free(sieve)
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 modinv_i64_i64(int64_t a0, int64_t m);
int32_t main(void);
int64_t modinv_i64_i64(int64_t a0, int64_t m) {
int64_t a = FLOW_CHECKED_MOD((a0), (m));
int64_t t = 0;
int64_t newt = 1;
int64_t r = m;
int64_t newr = a;
while (newr != 0) {
int64_t q = FLOW_CHECKED_DIV((r), (newr));
int64_t tmp = (t - (q * newt));
t = newt;
newt = tmp;
int64_t tmp2 = (r - (q * newr));
r = newr;
newr = tmp2;
}
if (t < 0) {
t = (t + m);
}
return t;
}
int32_t main(void) {
int64_t LIMIT = 100000000;
int8_t* sieve = (int8_t*)(calloc(LIMIT, 1));
if (sieve == NULL) {
return 1;
}
int64_t i = 0;
while (i < LIMIT) {
sieve[i] = 1;
i = (i + 1);
}
sieve[0] = 0;
sieve[1] = 0;
int64_t p = 2;
while ((p * p) < LIMIT) {
if (sieve[p] == 1) {
int64_t m = (p * p);
while (m < LIMIT) {
sieve[m] = 0;
m = (m + p);
}
}
p = (p + 1);
}
int64_t total = 0;
p = 5;
while (p < LIMIT) {
if (sieve[p] == 1) {
int64_t inv8 = modinv_i64_i64(8, p);
int64_t s = FLOW_CHECKED_MOD(((0 - (3 * inv8))), (p));
if (s < 0) {
s = (s + p);
}
total = (total + s);
}
p = (p + 1);
}
printf("%lld\n", total);
free(sieve);
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) -> ()
func.func @modinv(%arg0: i64, %arg1: i64) -> i64 {
%0 = arith.remsi %arg0, %arg1 : 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.constant 0 : i32
%4 = arith.extsi %3 : i32 to 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 = arith.constant 1 : i32
%8 = arith.extsi %7 : i32 to i64
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
llvm.store %8, %10 : i64, !llvm.ptr
%11 = llvm.mlir.constant(1 : i64) : i64
%12 = llvm.alloca %11 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %12 : i64, !llvm.ptr
%13 = llvm.load %2 : !llvm.ptr -> i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%16 = llvm.load %15 : !llvm.ptr -> i64
%17 = arith.constant 0 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi ne, %16, %19 : i64
cf.cond_br %18, ^bb1, ^bb2
^bb1:
%20 = llvm.load %12 : !llvm.ptr -> i64
%21 = llvm.load %15 : !llvm.ptr -> i64
%22 = arith.divsi %20, %21 : i64
%23 = llvm.load %6 : !llvm.ptr -> i64
%24 = llvm.load %10 : !llvm.ptr -> i64
%25 = arith.muli %22, %24 : i64
%26 = arith.subi %23, %25 : i64
%27 = llvm.load %10 : !llvm.ptr -> i64
llvm.store %27, %6 : i64, !llvm.ptr
llvm.store %26, %10 : i64, !llvm.ptr
%28 = llvm.load %12 : !llvm.ptr -> i64
%29 = llvm.load %15 : !llvm.ptr -> i64
%30 = arith.muli %22, %29 : i64
%31 = arith.subi %28, %30 : i64
%32 = llvm.load %15 : !llvm.ptr -> i64
llvm.store %32, %12 : i64, !llvm.ptr
llvm.store %31, %15 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%33 = llvm.load %6 : !llvm.ptr -> i64
%34 = arith.constant 0 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.cmpi slt, %33, %36 : i64
cf.cond_br %35, ^bb3, ^bb4
^bb3:
%37 = llvm.load %6 : !llvm.ptr -> i64
%38 = arith.addi %37, %arg1 : i64
llvm.store %38, %6 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%39 = llvm.load %6 : !llvm.ptr -> i64
func.return %39 : i64
}
func.func @main() -> i32 {
%40 = arith.constant 100000000 : i32
%41 = arith.extsi %40 : i32 to i64
%43 = arith.constant 1 : i32
%44 = arith.extsi %43 : i32 to i64
%42 = func.call @calloc(%41, %44) : (i64, i64) -> !llvm.ptr
%45 = llvm.mlir.zero : !llvm.ptr
%46 = llvm.icmp "eq" %42, %45 : !llvm.ptr
cf.cond_br %46, ^bb6, ^bb7
^bb6:
%47 = arith.constant 1 : i32
func.return %47 : i32
^bb7:
cf.br ^bb8
^bb8:
%48 = arith.constant 0 : i32
%49 = arith.extsi %48 : i32 to i64
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
llvm.store %49, %51 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%52 = llvm.load %51 : !llvm.ptr -> i64
%53 = arith.cmpi slt, %52, %41 : i64
cf.cond_br %53, ^bb10, ^bb11
^bb10:
%54 = arith.constant 1 : i32
%55 = llvm.load %51 : !llvm.ptr -> i64
%56 = arith.trunci %54 : i32 to i8
%57 = llvm.getelementptr %42[%55] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %56, %57 : i8, !llvm.ptr
%58 = llvm.load %51 : !llvm.ptr -> i64
%59 = arith.constant 1 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.addi %58, %61 : i64
llvm.store %60, %51 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%62 = arith.constant 0 : i32
%63 = arith.constant 0 : i32
%64 = arith.trunci %62 : i32 to i8
%65 = arith.extsi %63 : i32 to i64
%66 = llvm.getelementptr %42[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %64, %66 : i8, !llvm.ptr
%67 = arith.constant 0 : i32
%68 = arith.constant 1 : i32
%69 = arith.trunci %67 : i32 to i8
%70 = arith.extsi %68 : i32 to i64
%71 = llvm.getelementptr %42[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %69, %71 : i8, !llvm.ptr
%72 = arith.constant 2 : 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 = llvm.load %75 : !llvm.ptr -> i64
%78 = arith.muli %76, %77 : i64
%79 = arith.cmpi slt, %78, %41 : i64
cf.cond_br %79, ^bb13, ^bb14
^bb13:
%81 = llvm.load %75 : !llvm.ptr -> i64
%82 = llvm.getelementptr %42[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%80 = llvm.load %82 : !llvm.ptr -> i8
%83 = arith.constant 1 : i32
%85 = arith.extsi %80 : i8 to i32
%84 = arith.cmpi eq, %85, %83 : i32
cf.cond_br %84, ^bb15, ^bb16
^bb15:
%86 = llvm.load %75 : !llvm.ptr -> i64
%87 = llvm.load %75 : !llvm.ptr -> i64
%88 = arith.muli %86, %87 : i64
%89 = llvm.mlir.constant(1 : i64) : i64
%90 = llvm.alloca %89 x i64 : (i64) -> !llvm.ptr
llvm.store %88, %90 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%91 = llvm.load %90 : !llvm.ptr -> i64
%92 = arith.cmpi slt, %91, %41 : i64
cf.cond_br %92, ^bb19, ^bb20
^bb19:
%93 = arith.constant 0 : i32
%94 = llvm.load %90 : !llvm.ptr -> i64
%95 = arith.trunci %93 : i32 to i8
%96 = llvm.getelementptr %42[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %95, %96 : i8, !llvm.ptr
%97 = llvm.load %90 : !llvm.ptr -> i64
%98 = llvm.load %75 : !llvm.ptr -> i64
%99 = arith.addi %97, %98 : i64
llvm.store %99, %90 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%100 = llvm.load %75 : !llvm.ptr -> i64
%101 = arith.constant 1 : i32
%103 = arith.extsi %101 : i32 to i64
%102 = arith.addi %100, %103 : i64
llvm.store %102, %75 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%104 = arith.constant 0 : i32
%105 = arith.extsi %104 : i32 to i64
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i64 : (i64) -> !llvm.ptr
llvm.store %105, %107 : i64, !llvm.ptr
%108 = arith.constant 5 : i32
%109 = arith.extsi %108 : i32 to i64
llvm.store %109, %75 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%110 = llvm.load %75 : !llvm.ptr -> i64
%111 = arith.cmpi slt, %110, %41 : i64
cf.cond_br %111, ^bb22, ^bb23
^bb22:
%113 = llvm.load %75 : !llvm.ptr -> i64
%114 = llvm.getelementptr %42[%113] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%112 = llvm.load %114 : !llvm.ptr -> i8
%115 = arith.constant 1 : i32
%117 = arith.extsi %112 : i8 to i32
%116 = arith.cmpi eq, %117, %115 : i32
cf.cond_br %116, ^bb24, ^bb25
^bb24:
%119 = arith.constant 8 : i32
%120 = llvm.load %75 : !llvm.ptr -> i64
%121 = arith.extsi %119 : i32 to i64
%118 = func.call @modinv(%121, %120) : (i64, i64) -> i64
%122 = arith.constant 0 : i32
%123 = arith.constant 3 : i32
%125 = arith.extsi %123 : i32 to i64
%124 = arith.muli %125, %118 : i64
%127 = arith.extsi %122 : i32 to i64
%126 = arith.subi %127, %124 : i64
%128 = llvm.load %75 : !llvm.ptr -> i64
%129 = arith.remsi %126, %128 : i64
%130 = arith.constant 0 : i32
%132 = arith.extsi %130 : i32 to i64
%131 = arith.cmpi slt, %129, %132 : i64
%133 = scf.if %131 -> (i64) {
%134 = llvm.load %75 : !llvm.ptr -> i64
%135 = arith.addi %129, %134 : i64
scf.yield %135 : i64
} else {
scf.yield %129 : i64
}
%136 = llvm.load %107 : !llvm.ptr -> i64
%137 = arith.addi %136, %133 : i64
llvm.store %137, %107 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%138 = llvm.load %75 : !llvm.ptr -> i64
%139 = arith.constant 1 : i32
%141 = arith.extsi %139 : i32 to i64
%140 = arith.addi %138, %141 : i64
llvm.store %140, %75 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%142 = llvm.mlir.addressof @str_0 : !llvm.ptr
%143 = llvm.load %107 : !llvm.ptr -> i64
%144 = llvm.call @printf(%142, %143) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%42) : (!llvm.ptr) -> ()
%146 = arith.constant 0 : i32
func.return %146 : i32
}
}