← All problems
Problem 148
Entries not divisible by 7 in the first one billion rows of Pascal's triangle.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n^2)
Space complexity O(1)O(n^2)
Approach Flow solution Bottom-up DP
Verdict Optimal
Flow source
# Project Euler 148
# Entries not divisible by 7 in the first one billion rows of Pascal's triangle.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let N: i64 = 1000000000
let digs: ptr<i64> = calloc(20, 8)
let pow28: ptr<i64> = calloc(20, 8)
if digs == null || pow28 == null { return 1 }
let mut n: i64 = N
let mut len: i32 = 0
# collect digits least-significant first then reverse
let tmp: ptr<i64> = calloc(20, 8)
if tmp == null { return 1 }
while n > 0 {
tmp[len] = n % 7
n = n / 7
len = len + 1
}
let mut i: i32 = 0
while i < len {
digs[i] = tmp[len - 1 - i]
i = i + 1
}
pow28[0] = 1
i = 1
while i <= len {
pow28[i] = pow28[i - 1] * 28
i = i + 1
}
let mut total: i64 = 0
let mut prefix: i64 = 1
i = 0
while i < len {
let digit: i64 = digs[i]
let remaining: i32 = len - i - 1
let smaller: i64 = digit * (digit + 1) / 2
total = total + prefix * smaller * pow28[remaining]
prefix = prefix * (digit + 1)
i = i + 1
}
printf("%lld\n", total)
free(tmp)
free(pow28)
free(digs)
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 = 1000000000;
int64_t* digs = (int64_t*)(calloc(20, 8));
int64_t* pow28 = (int64_t*)(calloc(20, 8));
if ((digs == NULL || pow28 == NULL)) {
return 1;
}
int64_t n = N;
int32_t len = 0;
int64_t* tmp = (int64_t*)(calloc(20, 8));
if (tmp == NULL) {
return 1;
}
while (n > 0) {
tmp[len] = FLOW_CHECKED_MOD((n), (7));
n = FLOW_CHECKED_DIV((n), (7));
len = (len + 1);
}
int32_t i = 0;
while (i < len) {
digs[i] = tmp[((len - 1) - i)];
i = (i + 1);
}
pow28[0] = 1;
i = 1;
while (i <= len) {
pow28[i] = (pow28[(i - 1)] * 28);
i = (i + 1);
}
int64_t total = 0;
int64_t prefix = 1;
i = 0;
while (i < len) {
int64_t digit = digs[i];
int32_t remaining = ((len - i) - 1);
int64_t smaller = FLOW_CHECKED_DIV(((digit * (digit + 1))), (2));
total = (total + ((prefix * smaller) * pow28[remaining]));
prefix = (prefix * (digit + 1));
i = (i + 1);
}
printf("%lld\n", total);
free(tmp);
free(pow28);
free(digs);
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 1000000000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 20 : i32
%4 = arith.constant 8 : i32
%5 = arith.extsi %3 : i32 to i64
%6 = arith.extsi %4 : i32 to i64
%2 = func.call @calloc(%5, %6) : (i64, i64) -> !llvm.ptr
%8 = arith.constant 20 : i32
%9 = arith.constant 8 : i32
%10 = arith.extsi %8 : i32 to i64
%11 = arith.extsi %9 : i32 to i64
%7 = func.call @calloc(%10, %11) : (i64, i64) -> !llvm.ptr
%12 = llvm.mlir.zero : !llvm.ptr
%13 = llvm.icmp "eq" %2, %12 : !llvm.ptr
%14 = scf.if %13 -> (i1) {
%15 = arith.constant true
scf.yield %15 : i1
} else {
%16 = llvm.mlir.zero : !llvm.ptr
%17 = llvm.icmp "eq" %7, %16 : !llvm.ptr
scf.yield %17 : i1
}
cf.cond_br %14, ^bb0, ^bb1
^bb0:
%18 = arith.constant 1 : i32
func.return %18 : i32
^bb1:
cf.br ^bb2
^bb2:
%19 = llvm.mlir.constant(1 : i64) : i64
%20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %20 : i64, !llvm.ptr
%21 = arith.constant 0 : i32
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i32 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i32, !llvm.ptr
%25 = arith.constant 20 : i32
%26 = arith.constant 8 : i32
%27 = arith.extsi %25 : i32 to i64
%28 = arith.extsi %26 : i32 to i64
%24 = func.call @calloc(%27, %28) : (i64, i64) -> !llvm.ptr
%29 = llvm.mlir.zero : !llvm.ptr
%30 = llvm.icmp "eq" %24, %29 : !llvm.ptr
cf.cond_br %30, ^bb3, ^bb4
^bb3:
%31 = arith.constant 1 : i32
func.return %31 : i32
^bb4:
cf.br ^bb5
^bb5:
cf.br ^bb6
^bb6:
%32 = llvm.load %20 : !llvm.ptr -> i64
%33 = arith.constant 0 : i32
%35 = arith.extsi %33 : i32 to i64
%34 = arith.cmpi sgt, %32, %35 : i64
cf.cond_br %34, ^bb7, ^bb8
^bb7:
%36 = llvm.load %20 : !llvm.ptr -> i64
%37 = arith.constant 7 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.remsi %36, %39 : i64
%40 = llvm.load %23 : !llvm.ptr -> i32
%41 = arith.extsi %40 : i32 to i64
%42 = llvm.getelementptr %24[%41] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %38, %42 : i64, !llvm.ptr
%43 = llvm.load %20 : !llvm.ptr -> i64
%44 = arith.constant 7 : i32
%46 = arith.extsi %44 : i32 to i64
%45 = arith.divsi %43, %46 : i64
llvm.store %45, %20 : i64, !llvm.ptr
%47 = llvm.load %23 : !llvm.ptr -> i32
%48 = arith.constant 1 : i32
%49 = arith.addi %47, %48 : i32
llvm.store %49, %23 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%50 = arith.constant 0 : i32
%51 = llvm.mlir.constant(1 : i64) : i64
%52 = llvm.alloca %51 x i32 : (i64) -> !llvm.ptr
llvm.store %50, %52 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%53 = llvm.load %52 : !llvm.ptr -> i32
%54 = llvm.load %23 : !llvm.ptr -> i32
%55 = arith.cmpi slt, %53, %54 : i32
cf.cond_br %55, ^bb10, ^bb11
^bb10:
%57 = llvm.load %23 : !llvm.ptr -> i32
%58 = arith.constant 1 : i32
%59 = arith.subi %57, %58 : i32
%60 = llvm.load %52 : !llvm.ptr -> i32
%61 = arith.subi %59, %60 : i32
%62 = arith.extsi %61 : i32 to i64
%63 = llvm.getelementptr %24[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%56 = llvm.load %63 : !llvm.ptr -> i64
%64 = llvm.load %52 : !llvm.ptr -> i32
%65 = arith.extsi %64 : i32 to i64
%66 = llvm.getelementptr %2[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %56, %66 : i64, !llvm.ptr
%67 = llvm.load %52 : !llvm.ptr -> i32
%68 = arith.constant 1 : i32
%69 = arith.addi %67, %68 : i32
llvm.store %69, %52 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%70 = arith.constant 1 : i32
%71 = arith.constant 0 : i32
%72 = arith.extsi %70 : i32 to i64
%73 = arith.extsi %71 : i32 to i64
%74 = llvm.getelementptr %7[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %72, %74 : i64, !llvm.ptr
%75 = arith.constant 1 : i32
llvm.store %75, %52 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%76 = llvm.load %52 : !llvm.ptr -> i32
%77 = llvm.load %23 : !llvm.ptr -> i32
%78 = arith.cmpi sle, %76, %77 : i32
cf.cond_br %78, ^bb13, ^bb14
^bb13:
%80 = llvm.load %52 : !llvm.ptr -> i32
%81 = arith.constant 1 : i32
%82 = arith.subi %80, %81 : i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.getelementptr %7[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%79 = llvm.load %84 : !llvm.ptr -> i64
%85 = arith.constant 28 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.muli %79, %87 : i64
%88 = llvm.load %52 : !llvm.ptr -> i32
%89 = arith.extsi %88 : i32 to i64
%90 = llvm.getelementptr %7[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %86, %90 : i64, !llvm.ptr
%91 = llvm.load %52 : !llvm.ptr -> i32
%92 = arith.constant 1 : i32
%93 = arith.addi %91, %92 : i32
llvm.store %93, %52 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%94 = arith.constant 0 : i32
%95 = arith.extsi %94 : i32 to i64
%96 = llvm.mlir.constant(1 : i64) : i64
%97 = llvm.alloca %96 x i64 : (i64) -> !llvm.ptr
llvm.store %95, %97 : i64, !llvm.ptr
%98 = arith.constant 1 : i32
%99 = arith.extsi %98 : i32 to i64
%100 = llvm.mlir.constant(1 : i64) : i64
%101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
llvm.store %99, %101 : i64, !llvm.ptr
%102 = arith.constant 0 : i32
llvm.store %102, %52 : i32, !llvm.ptr
cf.br ^bb15
^bb15:
%103 = llvm.load %52 : !llvm.ptr -> i32
%104 = llvm.load %23 : !llvm.ptr -> i32
%105 = arith.cmpi slt, %103, %104 : i32
cf.cond_br %105, ^bb16, ^bb17
^bb16:
%107 = llvm.load %52 : !llvm.ptr -> i32
%108 = arith.extsi %107 : i32 to i64
%109 = llvm.getelementptr %2[%108] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%106 = llvm.load %109 : !llvm.ptr -> i64
%110 = llvm.load %23 : !llvm.ptr -> i32
%111 = llvm.load %52 : !llvm.ptr -> i32
%112 = arith.subi %110, %111 : i32
%113 = arith.constant 1 : i32
%114 = arith.subi %112, %113 : i32
%115 = arith.constant 1 : i32
%117 = arith.extsi %115 : i32 to i64
%116 = arith.addi %106, %117 : i64
%118 = arith.muli %106, %116 : i64
%119 = arith.constant 2 : i32
%121 = arith.extsi %119 : i32 to i64
%120 = arith.divsi %118, %121 : i64
%122 = llvm.load %97 : !llvm.ptr -> i64
%123 = llvm.load %101 : !llvm.ptr -> i64
%124 = arith.muli %123, %120 : i64
%126 = arith.extsi %114 : i32 to i64
%127 = llvm.getelementptr %7[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%125 = llvm.load %127 : !llvm.ptr -> i64
%128 = arith.muli %124, %125 : i64
%129 = arith.addi %122, %128 : i64
llvm.store %129, %97 : i64, !llvm.ptr
%130 = llvm.load %101 : !llvm.ptr -> i64
%131 = arith.constant 1 : i32
%133 = arith.extsi %131 : i32 to i64
%132 = arith.addi %106, %133 : i64
%134 = arith.muli %130, %132 : i64
llvm.store %134, %101 : i64, !llvm.ptr
%135 = llvm.load %52 : !llvm.ptr -> i32
%136 = arith.constant 1 : i32
%137 = arith.addi %135, %136 : i32
llvm.store %137, %52 : i32, !llvm.ptr
cf.br ^bb15
^bb17:
%138 = llvm.mlir.addressof @str_0 : !llvm.ptr
%139 = llvm.load %97 : !llvm.ptr -> i64
%140 = llvm.call @printf(%138, %139) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%24) : (!llvm.ptr) -> ()
func.call @free(%7) : (!llvm.ptr) -> ()
func.call @free(%2) : (!llvm.ptr) -> ()
%144 = arith.constant 0 : i32
func.return %144 : i32
}
}