← All problems
Problem 132
Sum of first forty prime factors of R(10^9).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(sqrt(n))
Space complexity O(1)O(1)
Approach Flow solution Trial division or Pollard rho
Verdict Suboptimal
Flow source
# Project Euler 132
# Sum of first forty prime factors of R(10^9).
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 = (r * b) % mod }
b = (b * b) % mod
e = e / 2
}
return r
}
function main() -> i32 {
let digits: i64 = 1000000000
let mut total: i64 = 0
let mut left: i32 = 40
let mut n: i64 = 7
while left > 0 {
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 mod_pow(10, digits, 9 * n) == 1 {
total = total + n
left = left - 1
}
}
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 mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int32_t main(void);
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 = FLOW_CHECKED_MOD(((r * b)), (mod));
}
b = FLOW_CHECKED_MOD(((b * b)), (mod));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int32_t main(void) {
int64_t digits = 1000000000;
int64_t total = 0;
int32_t left = 40;
int64_t n = 7;
while (left > 0) {
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 (mod_pow_i64_i64_i64(10, digits, (9 * n)) == 1) {
total = (total + n);
left = (left - 1);
}
}
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 @mod_pow(%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 = 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
cf.br ^bb0
^bb0:
%9 = llvm.load %8 : !llvm.ptr -> i64
%10 = arith.constant 0 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.cmpi sgt, %9, %12 : i64
cf.cond_br %11, ^bb1, ^bb2
^bb1:
%13 = llvm.load %8 : !llvm.ptr -> i64
%14 = arith.constant 2 : i32
%16 = arith.extsi %14 : i32 to i64
%15 = arith.remsi %13, %16 : i64
%17 = arith.constant 1 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi eq, %15, %19 : i64
cf.cond_br %18, ^bb3, ^bb4
^bb3:
%20 = llvm.load %3 : !llvm.ptr -> i64
%21 = llvm.load %6 : !llvm.ptr -> i64
%22 = arith.muli %20, %21 : i64
%23 = arith.remsi %22, %arg2 : i64
llvm.store %23, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%24 = llvm.load %6 : !llvm.ptr -> i64
%25 = llvm.load %6 : !llvm.ptr -> i64
%26 = arith.muli %24, %25 : i64
%27 = arith.remsi %26, %arg2 : i64
llvm.store %27, %6 : i64, !llvm.ptr
%28 = llvm.load %8 : !llvm.ptr -> i64
%29 = arith.constant 2 : i32
%31 = arith.extsi %29 : i32 to i64
%30 = arith.divsi %28, %31 : i64
llvm.store %30, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%32 = llvm.load %3 : !llvm.ptr -> i64
func.return %32 : i64
}
func.func @main() -> i32 {
%33 = arith.constant 1000000000 : i32
%34 = arith.extsi %33 : i32 to i64
%35 = arith.constant 0 : i32
%36 = arith.extsi %35 : i32 to i64
%37 = llvm.mlir.constant(1 : i64) : i64
%38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
llvm.store %36, %38 : i64, !llvm.ptr
%39 = arith.constant 40 : i32
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i32 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i32, !llvm.ptr
%42 = arith.constant 7 : 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
cf.br ^bb6
^bb6:
%46 = llvm.load %41 : !llvm.ptr -> i32
%47 = arith.constant 0 : i32
%48 = arith.cmpi sgt, %46, %47 : i32
cf.cond_br %48, ^bb7, ^bb8
^bb7:
%49 = arith.constant 1 : i1
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x i1 : (i64) -> !llvm.ptr
llvm.store %49, %51 : i1, !llvm.ptr
%52 = arith.constant 3 : i32
%53 = arith.extsi %52 : i32 to i64
%54 = llvm.mlir.constant(1 : i64) : i64
%55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
llvm.store %53, %55 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%56 = llvm.load %55 : !llvm.ptr -> i64
%57 = llvm.load %55 : !llvm.ptr -> i64
%58 = arith.muli %56, %57 : i64
%59 = llvm.load %45 : !llvm.ptr -> i64
%60 = arith.cmpi sle, %58, %59 : i64
cf.cond_br %60, ^bb10, ^bb11
^bb10:
%61 = llvm.load %45 : !llvm.ptr -> i64
%62 = llvm.load %55 : !llvm.ptr -> i64
%63 = arith.remsi %61, %62 : i64
%64 = arith.constant 0 : i32
%66 = arith.extsi %64 : i32 to i64
%65 = arith.cmpi eq, %63, %66 : i64
cf.cond_br %65, ^bb12, ^bb13
^bb12:
%67 = arith.constant 0 : i1
llvm.store %67, %51 : i1, !llvm.ptr
cf.br ^bb11
^bb13:
cf.br ^bb14
^bb14:
%68 = llvm.load %55 : !llvm.ptr -> i64
%69 = arith.constant 2 : i32
%71 = arith.extsi %69 : i32 to i64
%70 = arith.addi %68, %71 : i64
llvm.store %70, %55 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%72 = llvm.load %51 : !llvm.ptr -> i1
cf.cond_br %72, ^bb15, ^bb16
^bb15:
%74 = arith.constant 10 : i32
%75 = arith.constant 9 : i32
%76 = llvm.load %45 : !llvm.ptr -> i64
%78 = arith.extsi %75 : i32 to i64
%77 = arith.muli %78, %76 : i64
%79 = arith.extsi %74 : i32 to i64
%73 = func.call @mod_pow(%79, %34, %77) : (i64, i64, i64) -> i64
%80 = arith.constant 1 : i32
%82 = arith.extsi %80 : i32 to i64
%81 = arith.cmpi eq, %73, %82 : i64
cf.cond_br %81, ^bb18, ^bb19
^bb18:
%83 = llvm.load %38 : !llvm.ptr -> i64
%84 = llvm.load %45 : !llvm.ptr -> i64
%85 = arith.addi %83, %84 : i64
llvm.store %85, %38 : i64, !llvm.ptr
%86 = llvm.load %41 : !llvm.ptr -> i32
%87 = arith.constant 1 : i32
%88 = arith.subi %86, %87 : i32
llvm.store %88, %41 : i32, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%89 = llvm.load %45 : !llvm.ptr -> i64
%90 = arith.constant 2 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
llvm.store %91, %45 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%93 = llvm.mlir.addressof @str_0 : !llvm.ptr
%94 = llvm.load %38 : !llvm.ptr -> i64
%95 = llvm.call @printf(%93, %94) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%96 = arith.constant 0 : i32
func.return %96 : i32
}
}