← All problems
Problem 134
Sum of S(p1,p2) for consecutive primes with 5 ≤ p1 ≤ 10^6.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log log n)
Space complexity O(1)O(n)
Approach Flow solution Sieve of Eratosthenes
Verdict Suboptimal
Flow source
# Project Euler 134
# Sum of S(p1,p2) for consecutive primes with 5 ≤ p1 ≤ 10^6.
function tens(x: i64) -> i64 {
let mut r: i64 = 1
while r <= x { r = r * 10 }
return r
}
# return (x * inv(m1)) % m2 style via extended gcd: find x with x*m1 ≡ small (mod m2)?
# S ends with digits of p1 and divisible by p2: S = k*m2 + 0, S ≡ p1 (mod m1) where m1=tens(p1)
# So k*p2 ≡ p1 (mod m1) => k ≡ p1 * inv(p2) (mod m1)
function mod_inverse(a0: i64, mod: i64) -> i64 {
let mut a: i64 = a0 % mod
if a < 0 { a = a + mod }
let mut t: i64 = 0
let mut newt: i64 = 1
let mut r: i64 = mod
let mut newr: i64 = a
while newr != 0 {
let q: i64 = r / newr
let tmp: i64 = newt
newt = t - q * newt
t = tmp
let tmp2: i64 = newr
newr = r - q * newr
r = tmp2
}
if t < 0 { t = t + mod }
return t
}
function S(p1: i64, p2: i64) -> i64 {
let m1: i64 = tens(p1)
let inv: i64 = mod_inverse(p2 % m1, m1)
let k: i64 = (p1 * inv) % m1
return k * p2
}
function main() -> i32 {
let limit: i64 = 1000000
# sieve primes up to limit + some
let slim: i64 = limit + 1000
# collect primes
let mut primes_cap: i64 = 80000
# simple trial generation
let mut prev: i64 = 3
let mut total: i64 = 0
let mut n: i64 = 5
while true {
let mut prime: bool = true
let mut d: i64 = 3
while d * d <= n {
if n % d == 0 { prime = false; break }
d = d + 2
}
if prime {
if prev >= 5 && prev <= limit {
total = total + S(prev, n)
}
if n > limit { break }
prev = n
}
n = n + 2
}
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 tens_i64(int64_t x);
int64_t mod_inverse_i64_i64(int64_t a0, int64_t mod);
int64_t S_i64_i64(int64_t p1, int64_t p2);
int32_t main(void);
int64_t tens_i64(int64_t x) {
int64_t r = 1;
while (r <= x) {
r = (r * 10);
}
return r;
}
int64_t mod_inverse_i64_i64(int64_t a0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
if (a < 0) {
a = (a + mod);
}
int64_t t = 0;
int64_t newt = 1;
int64_t r = mod;
int64_t newr = a;
while (newr != 0) {
int64_t q = FLOW_CHECKED_DIV((r), (newr));
int64_t tmp = newt;
newt = (t - (q * newt));
t = tmp;
int64_t tmp2 = newr;
newr = (r - (q * newr));
r = tmp2;
}
if (t < 0) {
t = (t + mod);
}
return t;
}
int64_t S_i64_i64(int64_t p1, int64_t p2) {
int64_t m1 = tens_i64(p1);
int64_t inv = mod_inverse_i64_i64(FLOW_CHECKED_MOD((p2), (m1)), m1);
int64_t k = FLOW_CHECKED_MOD(((p1 * inv)), (m1));
return (k * p2);
}
int32_t main(void) {
int64_t limit = 1000000;
int64_t slim = (limit + 1000);
int64_t primes_cap = 80000;
int64_t prev = 3;
int64_t total = 0;
int64_t n = 5;
while (1) {
bool prime = 1;
int64_t d = 3;
while ((d * d) <= n) {
if (FLOW_CHECKED_MOD((n), (d)) == 0) {
prime = 0;
break;
}
d = (d + 2);
}
if (prime) {
if ((prev >= 5 && prev <= limit)) {
total = (total + S_i64_i64(prev, n));
}
if (n > limit) {
break;
}
prev = n;
}
n = (n + 2);
}
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 @tens(%arg0: 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
cf.br ^bb0
^bb0:
%4 = llvm.load %3 : !llvm.ptr -> i64
%5 = arith.cmpi sle, %4, %arg0 : i64
cf.cond_br %5, ^bb1, ^bb2
^bb1:
%6 = llvm.load %3 : !llvm.ptr -> i64
%7 = arith.constant 10 : i32
%9 = arith.extsi %7 : i32 to i64
%8 = arith.muli %6, %9 : i64
llvm.store %8, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%10 = llvm.load %3 : !llvm.ptr -> i64
func.return %10 : i64
}
func.func @mod_inverse(%arg0: i64, %arg1: i64) -> i64 {
%11 = arith.remsi %arg0, %arg1 : i64
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
llvm.store %11, %13 : i64, !llvm.ptr
%14 = llvm.load %13 : !llvm.ptr -> i64
%15 = arith.constant 0 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.cmpi slt, %14, %17 : i64
cf.cond_br %16, ^bb3, ^bb4
^bb3:
%18 = llvm.load %13 : !llvm.ptr -> i64
%19 = arith.addi %18, %arg1 : i64
llvm.store %19, %13 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%20 = arith.constant 0 : i32
%21 = arith.extsi %20 : i32 to i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i64, !llvm.ptr
%24 = arith.constant 1 : i32
%25 = arith.extsi %24 : i32 to i64
%26 = llvm.mlir.constant(1 : i64) : i64
%27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
llvm.store %25, %27 : i64, !llvm.ptr
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %29 : i64, !llvm.ptr
%30 = llvm.load %13 : !llvm.ptr -> i64
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i64 : (i64) -> !llvm.ptr
llvm.store %30, %32 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%33 = llvm.load %32 : !llvm.ptr -> i64
%34 = arith.constant 0 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.cmpi ne, %33, %36 : i64
cf.cond_br %35, ^bb7, ^bb8
^bb7:
%37 = llvm.load %29 : !llvm.ptr -> i64
%38 = llvm.load %32 : !llvm.ptr -> i64
%39 = arith.divsi %37, %38 : i64
%40 = llvm.load %27 : !llvm.ptr -> i64
%41 = llvm.load %23 : !llvm.ptr -> i64
%42 = llvm.load %27 : !llvm.ptr -> i64
%43 = arith.muli %39, %42 : i64
%44 = arith.subi %41, %43 : i64
llvm.store %44, %27 : i64, !llvm.ptr
llvm.store %40, %23 : i64, !llvm.ptr
%45 = llvm.load %32 : !llvm.ptr -> i64
%46 = llvm.load %29 : !llvm.ptr -> i64
%47 = llvm.load %32 : !llvm.ptr -> i64
%48 = arith.muli %39, %47 : i64
%49 = arith.subi %46, %48 : i64
llvm.store %49, %32 : i64, !llvm.ptr
llvm.store %45, %29 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%50 = llvm.load %23 : !llvm.ptr -> i64
%51 = arith.constant 0 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.cmpi slt, %50, %53 : i64
cf.cond_br %52, ^bb9, ^bb10
^bb9:
%54 = llvm.load %23 : !llvm.ptr -> i64
%55 = arith.addi %54, %arg1 : i64
llvm.store %55, %23 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%56 = llvm.load %23 : !llvm.ptr -> i64
func.return %56 : i64
}
func.func @S(%arg0: i64, %arg1: i64) -> i64 {
%57 = func.call @tens(%arg0) : (i64) -> i64
%59 = arith.remsi %arg1, %57 : i64
%58 = func.call @mod_inverse(%59, %57) : (i64, i64) -> i64
%60 = arith.muli %arg0, %58 : i64
%61 = arith.remsi %60, %57 : i64
%62 = arith.muli %61, %arg1 : i64
func.return %62 : i64
}
func.func @main() -> i32 {
%63 = arith.constant 1000000 : i32
%64 = arith.extsi %63 : i32 to i64
%65 = arith.constant 1000 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.addi %64, %67 : i64
%68 = arith.constant 80000 : 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 3 : 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
%76 = arith.constant 0 : i32
%77 = arith.extsi %76 : i32 to i64
%78 = llvm.mlir.constant(1 : i64) : i64
%79 = llvm.alloca %78 x i64 : (i64) -> !llvm.ptr
llvm.store %77, %79 : i64, !llvm.ptr
%80 = arith.constant 5 : i32
%81 = arith.extsi %80 : i32 to i64
%82 = llvm.mlir.constant(1 : i64) : i64
%83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
llvm.store %81, %83 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%84 = arith.constant 1 : i1
cf.cond_br %84, ^bb13, ^bb14
^bb13:
%85 = arith.constant 1 : i1
%86 = llvm.mlir.constant(1 : i64) : i64
%87 = llvm.alloca %86 x i1 : (i64) -> !llvm.ptr
llvm.store %85, %87 : i1, !llvm.ptr
%88 = arith.constant 3 : i32
%89 = arith.extsi %88 : i32 to i64
%90 = llvm.mlir.constant(1 : i64) : i64
%91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
llvm.store %89, %91 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%92 = llvm.load %91 : !llvm.ptr -> i64
%93 = llvm.load %91 : !llvm.ptr -> i64
%94 = arith.muli %92, %93 : i64
%95 = llvm.load %83 : !llvm.ptr -> i64
%96 = arith.cmpi sle, %94, %95 : i64
cf.cond_br %96, ^bb16, ^bb17
^bb16:
%97 = llvm.load %83 : !llvm.ptr -> i64
%98 = llvm.load %91 : !llvm.ptr -> i64
%99 = arith.remsi %97, %98 : i64
%100 = arith.constant 0 : i32
%102 = arith.extsi %100 : i32 to i64
%101 = arith.cmpi eq, %99, %102 : i64
cf.cond_br %101, ^bb18, ^bb19
^bb18:
%103 = arith.constant 0 : i1
llvm.store %103, %87 : i1, !llvm.ptr
cf.br ^bb17
^bb19:
cf.br ^bb20
^bb20:
%104 = llvm.load %91 : !llvm.ptr -> i64
%105 = arith.constant 2 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.addi %104, %107 : i64
llvm.store %106, %91 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%108 = llvm.load %87 : !llvm.ptr -> i1
cf.cond_br %108, ^bb21, ^bb22
^bb21:
%109 = llvm.load %75 : !llvm.ptr -> i64
%110 = arith.constant 5 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.cmpi sge, %109, %112 : i64
%113 = scf.if %111 -> (i1) {
%114 = llvm.load %75 : !llvm.ptr -> i64
%115 = arith.cmpi sle, %114, %64 : i64
scf.yield %115 : i1
} else {
%116 = arith.constant false
scf.yield %116 : i1
}
cf.cond_br %113, ^bb24, ^bb25
^bb24:
%117 = llvm.load %79 : !llvm.ptr -> i64
%119 = llvm.load %75 : !llvm.ptr -> i64
%120 = llvm.load %83 : !llvm.ptr -> i64
%118 = func.call @S(%119, %120) : (i64, i64) -> i64
%121 = arith.addi %117, %118 : i64
llvm.store %121, %79 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%122 = llvm.load %83 : !llvm.ptr -> i64
%123 = arith.cmpi sgt, %122, %64 : i64
cf.cond_br %123, ^bb27, ^bb28
^bb27:
cf.br ^bb14
^bb28:
cf.br ^bb29
^bb29:
%124 = llvm.load %83 : !llvm.ptr -> i64
llvm.store %124, %75 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%125 = llvm.load %83 : !llvm.ptr -> i64
%126 = arith.constant 2 : i32
%128 = arith.extsi %126 : i32 to i64
%127 = arith.addi %125, %128 : i64
llvm.store %127, %83 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%129 = llvm.mlir.addressof @str_0 : !llvm.ptr
%130 = llvm.load %79 : !llvm.ptr -> i64
%131 = llvm.call @printf(%129, %130) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%132 = arith.constant 0 : i32
func.return %132 : i32
}
}