← All problems
Problem 078
Least n such that p(n) is divisible by one million (pentagonal recurrence).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n * m)
Space complexity O(n)O(n)
Approach Flow solution Dynamic programming or generating function
Verdict Unknown
Flow source
# Project Euler 078
# Least n such that p(n) is divisible by one million (pentagonal recurrence).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let mod: i64 = 1000000
let maxn: i32 = 100000
let p: ptr<i64> = calloc((maxn + 1) as i64, 8)
if p == null { return 1 }
p[0] = 1
let mut n: i32 = 1
let mut ans: i64 = 0
while n <= maxn {
let mut s: i64 = 0
let mut k: i32 = 1
while true {
let g1: i32 = k * (3 * k - 1) / 2
let g2: i32 = k * (3 * k + 1) / 2
if g1 > n {
break
}
# generalized pentagonals for ±k share sign (-1)^{k-1}
let mut sign: i64 = 1
if k % 2 == 0 {
sign = 0 - 1
}
s = s + sign * p[n - g1]
if g2 <= n {
s = s + sign * p[n - g2]
}
k = k + 1
}
s = s % mod
if s < 0 {
s = s + mod
}
p[n] = s
if s == 0 {
ans = n as i64
break
}
n = n + 1
}
printf("%lld\n", ans)
free(p)
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; }
int32_t main(void);
int32_t main(void) {
int64_t mod = 1000000;
int32_t maxn = 100000;
int64_t* p = (int64_t*)(calloc(((int64_t)((maxn + 1))), 8));
if (p == NULL) {
return 1;
}
p[0] = 1;
int32_t n = 1;
int64_t ans = 0;
while (n <= maxn) {
int64_t s = 0;
int32_t k = 1;
while (1) {
int32_t g1 = FLOW_CHECKED_DIV(((k * ((3 * k) - 1))), (2));
int32_t g2 = FLOW_CHECKED_DIV(((k * ((3 * k) + 1))), (2));
if (g1 > n) {
break;
}
int64_t sign = 1;
if (FLOW_CHECKED_MOD((k), (2)) == 0) {
sign = (0 - 1);
}
s = (s + (sign * p[(n - g1)]));
if (g2 <= n) {
s = (s + (sign * p[(n - g2)]));
}
k = (k + 1);
}
s = FLOW_CHECKED_MOD((s), (mod));
if (s < 0) {
s = (s + mod);
}
p[n] = s;
if (s == 0) {
ans = ((int64_t)(n));
break;
}
n = (n + 1);
}
printf("%lld\n", ans);
free(p);
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 @main() -> i32 {
%0 = arith.constant 1000000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 100000 : i32
%4 = arith.constant 1 : i32
%5 = arith.addi %2, %4 : i32
%6 = arith.extsi %5 : i32 to i64
%7 = arith.constant 8 : i32
%8 = arith.extsi %7 : i32 to i64
%3 = func.call @calloc(%6, %8) : (i64, i64) -> !llvm.ptr
%9 = llvm.mlir.zero : !llvm.ptr
%10 = llvm.icmp "eq" %3, %9 : !llvm.ptr
cf.cond_br %10, ^bb0, ^bb1
^bb0:
%11 = arith.constant 1 : i32
func.return %11 : i32
^bb1:
cf.br ^bb2
^bb2:
%12 = arith.constant 1 : i32
%13 = arith.constant 0 : i32
%14 = arith.extsi %12 : i32 to i64
%15 = arith.extsi %13 : i32 to i64
%16 = llvm.getelementptr %3[%15] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %14, %16 : i64, !llvm.ptr
%17 = arith.constant 1 : i32
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i32 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i32, !llvm.ptr
%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
cf.br ^bb3
^bb3:
%24 = llvm.load %19 : !llvm.ptr -> i32
%25 = arith.cmpi sle, %24, %2 : i32
cf.cond_br %25, ^bb4, ^bb5
^bb4:
%26 = arith.constant 0 : i32
%27 = arith.extsi %26 : i32 to i64
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
llvm.store %27, %29 : i64, !llvm.ptr
%30 = arith.constant 1 : i32
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i32 : (i64) -> !llvm.ptr
llvm.store %30, %32 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%33 = arith.constant 1 : i1
cf.cond_br %33, ^bb7, ^bb8
^bb7:
%34 = llvm.load %32 : !llvm.ptr -> i32
%35 = arith.constant 3 : i32
%36 = llvm.load %32 : !llvm.ptr -> i32
%37 = arith.muli %35, %36 : i32
%38 = arith.constant 1 : i32
%39 = arith.subi %37, %38 : i32
%40 = arith.muli %34, %39 : i32
%41 = arith.constant 2 : i32
%42 = arith.divsi %40, %41 : i32
%43 = llvm.load %32 : !llvm.ptr -> i32
%44 = arith.constant 3 : i32
%45 = llvm.load %32 : !llvm.ptr -> i32
%46 = arith.muli %44, %45 : i32
%47 = arith.constant 1 : i32
%48 = arith.addi %46, %47 : i32
%49 = arith.muli %43, %48 : i32
%50 = arith.constant 2 : i32
%51 = arith.divsi %49, %50 : i32
%52 = llvm.load %19 : !llvm.ptr -> i32
%53 = arith.cmpi sgt, %42, %52 : i32
cf.cond_br %53, ^bb9, ^bb10
^bb9:
cf.br ^bb8
^bb10:
cf.br ^bb11
^bb11:
%54 = arith.constant 1 : i32
%55 = arith.extsi %54 : i32 to i64
%56 = llvm.mlir.constant(1 : i64) : i64
%57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
llvm.store %55, %57 : i64, !llvm.ptr
%58 = llvm.load %32 : !llvm.ptr -> i32
%59 = arith.constant 2 : i32
%60 = arith.remsi %58, %59 : i32
%61 = arith.constant 0 : i32
%62 = arith.cmpi eq, %60, %61 : i32
cf.cond_br %62, ^bb12, ^bb13
^bb12:
%63 = arith.constant 0 : i32
%64 = arith.constant 1 : i32
%65 = arith.subi %63, %64 : i32
%66 = arith.extsi %65 : i32 to i64
llvm.store %66, %57 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%67 = llvm.load %29 : !llvm.ptr -> i64
%68 = llvm.load %57 : !llvm.ptr -> i64
%70 = llvm.load %19 : !llvm.ptr -> i32
%71 = arith.subi %70, %42 : i32
%72 = arith.extsi %71 : i32 to i64
%73 = llvm.getelementptr %3[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%69 = llvm.load %73 : !llvm.ptr -> i64
%74 = arith.muli %68, %69 : i64
%75 = arith.addi %67, %74 : i64
llvm.store %75, %29 : i64, !llvm.ptr
%76 = llvm.load %19 : !llvm.ptr -> i32
%77 = arith.cmpi sle, %51, %76 : i32
cf.cond_br %77, ^bb15, ^bb16
^bb15:
%78 = llvm.load %29 : !llvm.ptr -> i64
%79 = llvm.load %57 : !llvm.ptr -> i64
%81 = llvm.load %19 : !llvm.ptr -> i32
%82 = arith.subi %81, %51 : i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.getelementptr %3[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%80 = llvm.load %84 : !llvm.ptr -> i64
%85 = arith.muli %79, %80 : i64
%86 = arith.addi %78, %85 : i64
llvm.store %86, %29 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%87 = llvm.load %32 : !llvm.ptr -> i32
%88 = arith.constant 1 : i32
%89 = arith.addi %87, %88 : i32
llvm.store %89, %32 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%90 = llvm.load %29 : !llvm.ptr -> i64
%91 = arith.remsi %90, %1 : i64
llvm.store %91, %29 : i64, !llvm.ptr
%92 = llvm.load %29 : !llvm.ptr -> i64
%93 = arith.constant 0 : i32
%95 = arith.extsi %93 : i32 to i64
%94 = arith.cmpi slt, %92, %95 : i64
cf.cond_br %94, ^bb18, ^bb19
^bb18:
%96 = llvm.load %29 : !llvm.ptr -> i64
%97 = arith.addi %96, %1 : i64
llvm.store %97, %29 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%98 = llvm.load %29 : !llvm.ptr -> i64
%99 = llvm.load %19 : !llvm.ptr -> i32
%100 = arith.extsi %99 : i32 to i64
%101 = llvm.getelementptr %3[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %98, %101 : i64, !llvm.ptr
%102 = llvm.load %29 : !llvm.ptr -> i64
%103 = arith.constant 0 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.cmpi eq, %102, %105 : i64
cf.cond_br %104, ^bb21, ^bb22
^bb21:
%106 = llvm.load %19 : !llvm.ptr -> i32
%107 = arith.extsi %106 : i32 to i64
llvm.store %107, %23 : i64, !llvm.ptr
cf.br ^bb5
^bb22:
cf.br ^bb23
^bb23:
%108 = llvm.load %19 : !llvm.ptr -> i32
%109 = arith.constant 1 : i32
%110 = arith.addi %108, %109 : i32
llvm.store %110, %19 : i32, !llvm.ptr
cf.br ^bb3
^bb5:
%111 = llvm.mlir.addressof @str_0 : !llvm.ptr
%112 = llvm.load %23 : !llvm.ptr -> i64
%113 = llvm.call @printf(%111, %112) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%3) : (!llvm.ptr) -> ()
%115 = arith.constant 0 : i32
func.return %115 : i32
}
}