← All problems
Problem 957
Point Genesis — closed form for maximal blue points g(16).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(1)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 957
# Point Genesis — closed form for maximal blue points g(16).
function g(n: i64) -> i64 {
let sign: i64 = 1
if (n & 1) != 0 { sign = -1 }
let one: i128 = 1
let pow2n: i128 = one << (n as i32)
let pow2_2n: i128 = one << ((2 * n) as i32)
let pow2_3n: i128 = one << ((3 * n) as i32)
let pow2_4n: i128 = one << ((4 * n) as i32)
let m2n: i128 = pow2n * (sign as i128)
let mut numer: i128 = 0
numer = numer + (11 as i128) * pow2_4n
numer = numer + (132 as i128) * pow2_3n
numer = numer + (564 as i128) * pow2_2n
numer = numer + (1008 as i128) * pow2n
numer = numer - (384 as i128) * (sign as i128)
numer = numer - (128 as i128) * m2n
numer = numer + (768 as i128)
return (numer / (864 as i128)) as i64
}
function main() -> i32 {
printf("%lld\n", g(16))
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 g_i64(int64_t n);
int32_t main(void);
int64_t g_i64(int64_t n) {
int64_t sign = 1;
if ((n & 1) != 0) {
sign = (-1);
}
__int128 one = 1;
__int128 pow2n = FLOW_CHECKED_SHL((one), (((int32_t)(n))));
__int128 pow2_2n = FLOW_CHECKED_SHL((one), (((int32_t)((2 * n)))));
__int128 pow2_3n = FLOW_CHECKED_SHL((one), (((int32_t)((3 * n)))));
__int128 pow2_4n = FLOW_CHECKED_SHL((one), (((int32_t)((4 * n)))));
__int128 m2n = (pow2n * ((__int128)(sign)));
__int128 numer = 0;
numer = (numer + (((__int128)(11)) * pow2_4n));
numer = (numer + (((__int128)(132)) * pow2_3n));
numer = (numer + (((__int128)(564)) * pow2_2n));
numer = (numer + (((__int128)(1008)) * pow2n));
numer = (numer - (((__int128)(384)) * ((__int128)(sign))));
numer = (numer - (((__int128)(128)) * m2n));
numer = (numer + ((__int128)(768)));
return ((int64_t)(FLOW_CHECKED_DIV((numer), (((__int128)(864))))));
}
int32_t main(void) {
printf("%lld\n", g_i64(16));
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 @g(%arg0: i64) -> i64 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 1 : i32
%4 = arith.extsi %2 : i32 to i64
%3 = arith.andi %arg0, %4 : i64
%5 = arith.constant 0 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.cmpi ne, %3, %7 : i64
%8 = scf.if %6 -> (i64) {
%9 = arith.constant 1 : i32
%11 = arith.constant 0 : i32
%10 = arith.subi %11, %9 : i32
%12 = arith.extsi %10 : i32 to i64
scf.yield %12 : i64
} else {
scf.yield %1 : i64
}
%13 = arith.constant 1 : i32
%14 = arith.extsi %13 : i32 to i128
%15 = arith.trunci %arg0 : i64 to i32
%17 = arith.trunci %14 : i128 to i64
%18 = arith.extsi %15 : i32 to i64
%16 = arith.shli %17, %18 : i64
%19 = arith.extsi %16 : i64 to i128
%20 = arith.constant 2 : i32
%22 = arith.extsi %20 : i32 to i64
%21 = arith.muli %22, %arg0 : i64
%23 = arith.trunci %21 : i64 to i32
%25 = arith.trunci %14 : i128 to i64
%26 = arith.extsi %23 : i32 to i64
%24 = arith.shli %25, %26 : i64
%27 = arith.extsi %24 : i64 to i128
%28 = arith.constant 3 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.muli %30, %arg0 : i64
%31 = arith.trunci %29 : i64 to i32
%33 = arith.trunci %14 : i128 to i64
%34 = arith.extsi %31 : i32 to i64
%32 = arith.shli %33, %34 : i64
%35 = arith.extsi %32 : i64 to i128
%36 = arith.constant 4 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.muli %38, %arg0 : i64
%39 = arith.trunci %37 : i64 to i32
%41 = arith.trunci %14 : i128 to i64
%42 = arith.extsi %39 : i32 to i64
%40 = arith.shli %41, %42 : i64
%43 = arith.extsi %40 : i64 to i128
%44 = arith.extsi %8 : i64 to i128
%46 = arith.trunci %19 : i128 to i64
%47 = arith.trunci %44 : i128 to i64
%45 = arith.muli %46, %47 : i64
%48 = arith.extsi %45 : i64 to i128
%49 = arith.constant 0 : i32
%50 = arith.extsi %49 : i32 to i128
%51 = llvm.mlir.constant(1 : i64) : i64
%52 = llvm.alloca %51 x i128 : (i64) -> !llvm.ptr
llvm.store %50, %52 : i128, !llvm.ptr
%53 = llvm.load %52 : !llvm.ptr -> i128
%54 = arith.constant 11 : i32
%55 = arith.extsi %54 : i32 to i128
%57 = arith.trunci %55 : i128 to i64
%58 = arith.trunci %43 : i128 to i64
%56 = arith.muli %57, %58 : i64
%60 = arith.trunci %53 : i128 to i64
%59 = arith.addi %60, %56 : i64
%61 = arith.extsi %59 : i64 to i128
llvm.store %61, %52 : i128, !llvm.ptr
%62 = llvm.load %52 : !llvm.ptr -> i128
%63 = arith.constant 132 : i32
%64 = arith.extsi %63 : i32 to i128
%66 = arith.trunci %64 : i128 to i64
%67 = arith.trunci %35 : i128 to i64
%65 = arith.muli %66, %67 : i64
%69 = arith.trunci %62 : i128 to i64
%68 = arith.addi %69, %65 : i64
%70 = arith.extsi %68 : i64 to i128
llvm.store %70, %52 : i128, !llvm.ptr
%71 = llvm.load %52 : !llvm.ptr -> i128
%72 = arith.constant 564 : i32
%73 = arith.extsi %72 : i32 to i128
%75 = arith.trunci %73 : i128 to i64
%76 = arith.trunci %27 : i128 to i64
%74 = arith.muli %75, %76 : i64
%78 = arith.trunci %71 : i128 to i64
%77 = arith.addi %78, %74 : i64
%79 = arith.extsi %77 : i64 to i128
llvm.store %79, %52 : i128, !llvm.ptr
%80 = llvm.load %52 : !llvm.ptr -> i128
%81 = arith.constant 1008 : i32
%82 = arith.extsi %81 : i32 to i128
%84 = arith.trunci %82 : i128 to i64
%85 = arith.trunci %19 : i128 to i64
%83 = arith.muli %84, %85 : i64
%87 = arith.trunci %80 : i128 to i64
%86 = arith.addi %87, %83 : i64
%88 = arith.extsi %86 : i64 to i128
llvm.store %88, %52 : i128, !llvm.ptr
%89 = llvm.load %52 : !llvm.ptr -> i128
%90 = arith.constant 384 : i32
%91 = arith.extsi %90 : i32 to i128
%92 = arith.extsi %8 : i64 to i128
%94 = arith.trunci %91 : i128 to i64
%95 = arith.trunci %92 : i128 to i64
%93 = arith.muli %94, %95 : i64
%97 = arith.trunci %89 : i128 to i64
%96 = arith.subi %97, %93 : i64
%98 = arith.extsi %96 : i64 to i128
llvm.store %98, %52 : i128, !llvm.ptr
%99 = llvm.load %52 : !llvm.ptr -> i128
%100 = arith.constant 128 : i32
%101 = arith.extsi %100 : i32 to i128
%103 = arith.trunci %101 : i128 to i64
%104 = arith.trunci %48 : i128 to i64
%102 = arith.muli %103, %104 : i64
%106 = arith.trunci %99 : i128 to i64
%105 = arith.subi %106, %102 : i64
%107 = arith.extsi %105 : i64 to i128
llvm.store %107, %52 : i128, !llvm.ptr
%108 = llvm.load %52 : !llvm.ptr -> i128
%109 = arith.constant 768 : i32
%110 = arith.extsi %109 : i32 to i128
%112 = arith.trunci %108 : i128 to i64
%113 = arith.trunci %110 : i128 to i64
%111 = arith.addi %112, %113 : i64
%114 = arith.extsi %111 : i64 to i128
llvm.store %114, %52 : i128, !llvm.ptr
%115 = llvm.load %52 : !llvm.ptr -> i128
%116 = arith.constant 864 : i32
%117 = arith.extsi %116 : i32 to i128
%119 = arith.trunci %115 : i128 to i64
%120 = arith.trunci %117 : i128 to i64
%118 = arith.divsi %119, %120 : i64
func.return %118 : i64
}
func.func @main() -> i32 {
%121 = llvm.mlir.addressof @str_0 : !llvm.ptr
%123 = arith.constant 16 : i32
%124 = arith.extsi %123 : i32 to i64
%122 = func.call @g(%124) : (i64) -> i64
%125 = llvm.call @printf(%121, %122) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%126 = arith.constant 0 : i32
func.return %126 : i32
}
}