← All problems
Problem 154
Entries in Pascal's pyramid layer n=200000 divisible by 10^12.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2)
Space complexity O(n)O(n)
Approach Flow solution Pascal triangle computation
Verdict Optimal
Flow source
# Project Euler 154
# Entries in Pascal's pyramid layer n=200000 divisible by 10^12.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let N: i64 = 200000
let ae: i32 = 12
let be: i32 = 12
let pfa: ptr<i32> = calloc(N + 1, 4)
let pfb: ptr<i32> = calloc(N + 1, 4)
if pfa == null || pfb == null { return 1 }
let mut n: i64 = 2
while n <= N {
let mut x: i64 = n
let mut c: i32 = 0
while x % 2 == 0 {
x = x / 2
c = c + 1
}
pfa[n] = c
n = n + 2
}
n = 5
while n <= N {
let mut x2: i64 = n
let mut c2: i32 = 0
while x2 % 5 == 0 {
x2 = x2 / 5
c2 = c2 + 1
}
pfb[n] = c2
n = n + 5
}
n = 1
while n <= N {
pfa[n] = pfa[n] + pfa[n - 1]
pfb[n] = pfb[n] + pfb[n - 1]
n = n + 1
}
let mut total: i64 = 0
let mut i: i64 = 0
while i <= N / 3 {
let mut j: i64 = i
while j <= (N - i) / 2 {
let k: i64 = N - i - j
if pfb[N] - pfb[i] - pfb[j] - pfb[k] >= be {
if pfa[N] - pfa[i] - pfa[j] - pfa[k] >= ae {
if i == j && j == k {
total = total + 1
} elif i == j || j == k {
total = total + 3
} else {
total = total + 6
}
}
}
j = j + 1
}
i = i + 1
}
printf("%lld\n", total)
free(pfa)
free(pfb)
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 N = 200000;
int32_t ae = 12;
int32_t be = 12;
int32_t* pfa = (int32_t*)(calloc((N + 1), 4));
int32_t* pfb = (int32_t*)(calloc((N + 1), 4));
if ((pfa == NULL || pfb == NULL)) {
return 1;
}
int64_t n = 2;
while (n <= N) {
int64_t x = n;
int32_t c = 0;
while (FLOW_CHECKED_MOD((x), (2)) == 0) {
x = FLOW_CHECKED_DIV((x), (2));
c = (c + 1);
}
pfa[n] = c;
n = (n + 2);
}
n = 5;
while (n <= N) {
int64_t x2 = n;
int32_t c2 = 0;
while (FLOW_CHECKED_MOD((x2), (5)) == 0) {
x2 = FLOW_CHECKED_DIV((x2), (5));
c2 = (c2 + 1);
}
pfb[n] = c2;
n = (n + 5);
}
n = 1;
while (n <= N) {
pfa[n] = (pfa[n] + pfa[(n - 1)]);
pfb[n] = (pfb[n] + pfb[(n - 1)]);
n = (n + 1);
}
int64_t total = 0;
int64_t i = 0;
while (i <= FLOW_CHECKED_DIV((N), (3))) {
int64_t j = i;
while (j <= FLOW_CHECKED_DIV(((N - i)), (2))) {
int64_t k = ((N - i) - j);
if ((((pfb[N] - pfb[i]) - pfb[j]) - pfb[k]) >= be) {
if ((((pfa[N] - pfa[i]) - pfa[j]) - pfa[k]) >= ae) {
if ((i == j && j == k)) {
total = (total + 1);
} else if ((i == j || j == k)) {
total = (total + 3);
} else {
total = (total + 6);
}
}
}
j = (j + 1);
}
i = (i + 1);
}
printf("%lld\n", total);
free(pfa);
free(pfb);
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 200000 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 12 : i32
%3 = arith.constant 12 : i32
%5 = arith.constant 1 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.addi %1, %7 : i64
%8 = arith.constant 4 : i32
%9 = arith.extsi %8 : i32 to i64
%4 = func.call @calloc(%6, %9) : (i64, i64) -> !llvm.ptr
%11 = arith.constant 1 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.addi %1, %13 : i64
%14 = arith.constant 4 : i32
%15 = arith.extsi %14 : i32 to i64
%10 = func.call @calloc(%12, %15) : (i64, i64) -> !llvm.ptr
%16 = llvm.mlir.zero : !llvm.ptr
%17 = llvm.icmp "eq" %4, %16 : !llvm.ptr
%18 = scf.if %17 -> (i1) {
%19 = arith.constant true
scf.yield %19 : i1
} else {
%20 = llvm.mlir.zero : !llvm.ptr
%21 = llvm.icmp "eq" %10, %20 : !llvm.ptr
scf.yield %21 : i1
}
cf.cond_br %18, ^bb0, ^bb1
^bb0:
%22 = arith.constant 1 : i32
func.return %22 : i32
^bb1:
cf.br ^bb2
^bb2:
%23 = arith.constant 2 : i32
%24 = arith.extsi %23 : i32 to i64
%25 = llvm.mlir.constant(1 : i64) : i64
%26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
llvm.store %24, %26 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%27 = llvm.load %26 : !llvm.ptr -> i64
%28 = arith.cmpi sle, %27, %1 : i64
cf.cond_br %28, ^bb4, ^bb5
^bb4:
%29 = llvm.load %26 : !llvm.ptr -> i64
%30 = llvm.mlir.constant(1 : i64) : i64
%31 = llvm.alloca %30 x i64 : (i64) -> !llvm.ptr
llvm.store %29, %31 : i64, !llvm.ptr
%32 = arith.constant 0 : i32
%33 = llvm.mlir.constant(1 : i64) : i64
%34 = llvm.alloca %33 x i32 : (i64) -> !llvm.ptr
llvm.store %32, %34 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%35 = llvm.load %31 : !llvm.ptr -> i64
%36 = arith.constant 2 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.remsi %35, %38 : i64
%39 = arith.constant 0 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.cmpi eq, %37, %41 : i64
cf.cond_br %40, ^bb7, ^bb8
^bb7:
%42 = llvm.load %31 : !llvm.ptr -> i64
%43 = arith.constant 2 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.divsi %42, %45 : i64
llvm.store %44, %31 : i64, !llvm.ptr
%46 = llvm.load %34 : !llvm.ptr -> i32
%47 = arith.constant 1 : i32
%48 = arith.addi %46, %47 : i32
llvm.store %48, %34 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%49 = llvm.load %34 : !llvm.ptr -> i32
%50 = llvm.load %26 : !llvm.ptr -> i64
%51 = llvm.getelementptr %4[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %49, %51 : i32, !llvm.ptr
%52 = llvm.load %26 : !llvm.ptr -> i64
%53 = arith.constant 2 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.addi %52, %55 : i64
llvm.store %54, %26 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%56 = arith.constant 5 : i32
%57 = arith.extsi %56 : i32 to i64
llvm.store %57, %26 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%58 = llvm.load %26 : !llvm.ptr -> i64
%59 = arith.cmpi sle, %58, %1 : i64
cf.cond_br %59, ^bb10, ^bb11
^bb10:
%60 = llvm.load %26 : !llvm.ptr -> i64
%61 = llvm.mlir.constant(1 : i64) : i64
%62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
llvm.store %60, %62 : i64, !llvm.ptr
%63 = arith.constant 0 : i32
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i32 : (i64) -> !llvm.ptr
llvm.store %63, %65 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%66 = llvm.load %62 : !llvm.ptr -> i64
%67 = arith.constant 5 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.remsi %66, %69 : i64
%70 = arith.constant 0 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.cmpi eq, %68, %72 : i64
cf.cond_br %71, ^bb13, ^bb14
^bb13:
%73 = llvm.load %62 : !llvm.ptr -> i64
%74 = arith.constant 5 : i32
%76 = arith.extsi %74 : i32 to i64
%75 = arith.divsi %73, %76 : i64
llvm.store %75, %62 : i64, !llvm.ptr
%77 = llvm.load %65 : !llvm.ptr -> i32
%78 = arith.constant 1 : i32
%79 = arith.addi %77, %78 : i32
llvm.store %79, %65 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%80 = llvm.load %65 : !llvm.ptr -> i32
%81 = llvm.load %26 : !llvm.ptr -> i64
%82 = llvm.getelementptr %10[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %80, %82 : i32, !llvm.ptr
%83 = llvm.load %26 : !llvm.ptr -> i64
%84 = arith.constant 5 : i32
%86 = arith.extsi %84 : i32 to i64
%85 = arith.addi %83, %86 : i64
llvm.store %85, %26 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%87 = arith.constant 1 : i32
%88 = arith.extsi %87 : i32 to i64
llvm.store %88, %26 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%89 = llvm.load %26 : !llvm.ptr -> i64
%90 = arith.cmpi sle, %89, %1 : i64
cf.cond_br %90, ^bb16, ^bb17
^bb16:
%92 = llvm.load %26 : !llvm.ptr -> i64
%93 = llvm.getelementptr %4[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%91 = llvm.load %93 : !llvm.ptr -> i32
%95 = llvm.load %26 : !llvm.ptr -> i64
%96 = arith.constant 1 : i32
%98 = arith.extsi %96 : i32 to i64
%97 = arith.subi %95, %98 : i64
%99 = llvm.getelementptr %4[%97] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%94 = llvm.load %99 : !llvm.ptr -> i32
%100 = arith.addi %91, %94 : i32
%101 = llvm.load %26 : !llvm.ptr -> i64
%102 = llvm.getelementptr %4[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %100, %102 : i32, !llvm.ptr
%104 = llvm.load %26 : !llvm.ptr -> i64
%105 = llvm.getelementptr %10[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%103 = llvm.load %105 : !llvm.ptr -> i32
%107 = llvm.load %26 : !llvm.ptr -> i64
%108 = arith.constant 1 : i32
%110 = arith.extsi %108 : i32 to i64
%109 = arith.subi %107, %110 : i64
%111 = llvm.getelementptr %10[%109] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%106 = llvm.load %111 : !llvm.ptr -> i32
%112 = arith.addi %103, %106 : i32
%113 = llvm.load %26 : !llvm.ptr -> i64
%114 = llvm.getelementptr %10[%113] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %112, %114 : i32, !llvm.ptr
%115 = llvm.load %26 : !llvm.ptr -> i64
%116 = arith.constant 1 : i32
%118 = arith.extsi %116 : i32 to i64
%117 = arith.addi %115, %118 : i64
llvm.store %117, %26 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%119 = arith.constant 0 : i32
%120 = arith.extsi %119 : i32 to i64
%121 = llvm.mlir.constant(1 : i64) : i64
%122 = llvm.alloca %121 x i64 : (i64) -> !llvm.ptr
llvm.store %120, %122 : i64, !llvm.ptr
%123 = arith.constant 0 : i32
%124 = arith.extsi %123 : i32 to i64
%125 = llvm.mlir.constant(1 : i64) : i64
%126 = llvm.alloca %125 x i64 : (i64) -> !llvm.ptr
llvm.store %124, %126 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%127 = llvm.load %126 : !llvm.ptr -> i64
%128 = arith.constant 3 : i32
%130 = arith.extsi %128 : i32 to i64
%129 = arith.divsi %1, %130 : i64
%131 = arith.cmpi sle, %127, %129 : i64
cf.cond_br %131, ^bb19, ^bb20
^bb19:
%132 = llvm.load %126 : !llvm.ptr -> i64
%133 = llvm.mlir.constant(1 : i64) : i64
%134 = llvm.alloca %133 x i64 : (i64) -> !llvm.ptr
llvm.store %132, %134 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%135 = llvm.load %134 : !llvm.ptr -> i64
%136 = llvm.load %126 : !llvm.ptr -> i64
%137 = arith.subi %1, %136 : i64
%138 = arith.constant 2 : i32
%140 = arith.extsi %138 : i32 to i64
%139 = arith.divsi %137, %140 : i64
%141 = arith.cmpi sle, %135, %139 : i64
cf.cond_br %141, ^bb22, ^bb23
^bb22:
%142 = llvm.load %126 : !llvm.ptr -> i64
%143 = arith.subi %1, %142 : i64
%144 = llvm.load %134 : !llvm.ptr -> i64
%145 = arith.subi %143, %144 : i64
%147 = llvm.getelementptr %10[%1] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%146 = llvm.load %147 : !llvm.ptr -> i32
%149 = llvm.load %126 : !llvm.ptr -> i64
%150 = llvm.getelementptr %10[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%148 = llvm.load %150 : !llvm.ptr -> i32
%151 = arith.subi %146, %148 : i32
%153 = llvm.load %134 : !llvm.ptr -> i64
%154 = llvm.getelementptr %10[%153] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%152 = llvm.load %154 : !llvm.ptr -> i32
%155 = arith.subi %151, %152 : i32
%157 = llvm.getelementptr %10[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%156 = llvm.load %157 : !llvm.ptr -> i32
%158 = arith.subi %155, %156 : i32
%159 = arith.cmpi sge, %158, %3 : i32
cf.cond_br %159, ^bb24, ^bb25
^bb24:
%161 = llvm.getelementptr %4[%1] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%160 = llvm.load %161 : !llvm.ptr -> i32
%163 = llvm.load %126 : !llvm.ptr -> i64
%164 = llvm.getelementptr %4[%163] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%162 = llvm.load %164 : !llvm.ptr -> i32
%165 = arith.subi %160, %162 : i32
%167 = llvm.load %134 : !llvm.ptr -> i64
%168 = llvm.getelementptr %4[%167] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%166 = llvm.load %168 : !llvm.ptr -> i32
%169 = arith.subi %165, %166 : i32
%171 = llvm.getelementptr %4[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%170 = llvm.load %171 : !llvm.ptr -> i32
%172 = arith.subi %169, %170 : i32
%173 = arith.cmpi sge, %172, %2 : i32
cf.cond_br %173, ^bb27, ^bb28
^bb27:
%174 = llvm.load %126 : !llvm.ptr -> i64
%175 = llvm.load %134 : !llvm.ptr -> i64
%176 = arith.cmpi eq, %174, %175 : i64
%177 = scf.if %176 -> (i1) {
%178 = llvm.load %134 : !llvm.ptr -> i64
%179 = arith.cmpi eq, %178, %145 : i64
scf.yield %179 : i1
} else {
%180 = arith.constant false
scf.yield %180 : i1
}
cf.cond_br %177, ^bb30, ^bb31
^bb30:
%181 = llvm.load %122 : !llvm.ptr -> i64
%182 = arith.constant 1 : i32
%184 = arith.extsi %182 : i32 to i64
%183 = arith.addi %181, %184 : i64
llvm.store %183, %122 : i64, !llvm.ptr
cf.br ^bb32
^bb31:
%185 = llvm.load %126 : !llvm.ptr -> i64
%186 = llvm.load %134 : !llvm.ptr -> i64
%187 = arith.cmpi eq, %185, %186 : i64
%188 = scf.if %187 -> (i1) {
%189 = arith.constant true
scf.yield %189 : i1
} else {
%190 = llvm.load %134 : !llvm.ptr -> i64
%191 = arith.cmpi eq, %190, %145 : i64
scf.yield %191 : i1
}
cf.cond_br %188, ^bb34, ^bb33
^bb34:
%192 = llvm.load %122 : !llvm.ptr -> i64
%193 = arith.constant 3 : i32
%195 = arith.extsi %193 : i32 to i64
%194 = arith.addi %192, %195 : i64
llvm.store %194, %122 : i64, !llvm.ptr
cf.br ^bb32
^bb33:
%196 = llvm.load %122 : !llvm.ptr -> i64
%197 = arith.constant 6 : i32
%199 = arith.extsi %197 : i32 to i64
%198 = arith.addi %196, %199 : i64
llvm.store %198, %122 : i64, !llvm.ptr
cf.br ^bb32
^bb32:
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%200 = llvm.load %134 : !llvm.ptr -> i64
%201 = arith.constant 1 : i32
%203 = arith.extsi %201 : i32 to i64
%202 = arith.addi %200, %203 : i64
llvm.store %202, %134 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%204 = llvm.load %126 : !llvm.ptr -> i64
%205 = arith.constant 1 : i32
%207 = arith.extsi %205 : i32 to i64
%206 = arith.addi %204, %207 : i64
llvm.store %206, %126 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%208 = llvm.mlir.addressof @str_0 : !llvm.ptr
%209 = llvm.load %122 : !llvm.ptr -> i64
%210 = llvm.call @printf(%208, %209) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%4) : (!llvm.ptr) -> ()
func.call @free(%10) : (!llvm.ptr) -> ()
%213 = arith.constant 0 : i32
func.return %213 : i32
}
}