← All problems
Problem 162
Hex numbers of length 1..16 containing 0, 1 and A at least once. Print decimal value of the hex answer string.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n * d * s)
Space complexity O(1)O(d * s)
Approach Flow solution Digit DP
Verdict Unknown
Flow source
# Project Euler 162
# Hex numbers of length 1..16 containing 0, 1 and A at least once.
# Print decimal value of the hex answer string.
function ipow(base: i64, exp0: i64) -> i64 {
let mut exp: i64 = exp0
let mut r: i64 = 1
let mut b: i64 = base
while exp > 0 {
if (exp & 1) == 1 { r = r * b }
b = b * b
exp = exp >> 1
}
return r
}
function main() -> i32 {
let mut ans: i64 = 0
let mut n: i64 = 1
while n <= 16 {
ans = ans + 15 * ipow(16, n - 1) - 43 * ipow(15, n - 1) + 41 * ipow(14, n - 1) - ipow(13, n)
n = n + 1
}
printf("%lld\n", ans)
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 ipow_i64_i64(int64_t base, int64_t exp0);
int32_t main(void);
int64_t ipow_i64_i64(int64_t base, int64_t exp0) {
int64_t exp = exp0;
int64_t r = 1;
int64_t b = base;
while (exp > 0) {
if ((exp & 1) == 1) {
r = (r * b);
}
b = (b * b);
exp = FLOW_CHECKED_SHR((exp), (1));
}
return r;
}
int32_t main(void) {
int64_t ans = 0;
int64_t n = 1;
while (n <= 16) {
ans = ((((ans + (15 * ipow_i64_i64(16, (n - 1)))) - (43 * ipow_i64_i64(15, (n - 1)))) + (41 * ipow_i64_i64(14, (n - 1)))) - ipow_i64_i64(13, n));
n = (n + 1);
}
printf("%lld\n", ans);
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 @ipow(%arg0: i64, %arg1: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %1 : i64, !llvm.ptr
%2 = arith.constant 1 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
llvm.store %3, %5 : i64, !llvm.ptr
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %7 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%8 = llvm.load %1 : !llvm.ptr -> i64
%9 = arith.constant 0 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.cmpi sgt, %8, %11 : i64
cf.cond_br %10, ^bb1, ^bb2
^bb1:
%12 = llvm.load %1 : !llvm.ptr -> i64
%13 = arith.constant 1 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.andi %12, %15 : i64
%16 = arith.constant 1 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.cmpi eq, %14, %18 : i64
cf.cond_br %17, ^bb3, ^bb4
^bb3:
%19 = llvm.load %5 : !llvm.ptr -> i64
%20 = llvm.load %7 : !llvm.ptr -> i64
%21 = arith.muli %19, %20 : i64
llvm.store %21, %5 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%22 = llvm.load %7 : !llvm.ptr -> i64
%23 = llvm.load %7 : !llvm.ptr -> i64
%24 = arith.muli %22, %23 : i64
llvm.store %24, %7 : i64, !llvm.ptr
%25 = llvm.load %1 : !llvm.ptr -> i64
%26 = arith.constant 1 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.shrsi %25, %28 : i64
llvm.store %27, %1 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%29 = llvm.load %5 : !llvm.ptr -> i64
func.return %29 : i64
}
func.func @main() -> i32 {
%30 = arith.constant 0 : i32
%31 = arith.extsi %30 : i32 to i64
%32 = llvm.mlir.constant(1 : i64) : i64
%33 = llvm.alloca %32 x i64 : (i64) -> !llvm.ptr
llvm.store %31, %33 : i64, !llvm.ptr
%34 = arith.constant 1 : i32
%35 = arith.extsi %34 : i32 to i64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%38 = llvm.load %37 : !llvm.ptr -> i64
%39 = arith.constant 16 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.cmpi sle, %38, %41 : i64
cf.cond_br %40, ^bb7, ^bb8
^bb7:
%42 = llvm.load %33 : !llvm.ptr -> i64
%43 = arith.constant 15 : i32
%45 = arith.constant 16 : i32
%46 = llvm.load %37 : !llvm.ptr -> i64
%47 = arith.constant 1 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.subi %46, %49 : i64
%50 = arith.extsi %45 : i32 to i64
%44 = func.call @ipow(%50, %48) : (i64, i64) -> i64
%52 = arith.extsi %43 : i32 to i64
%51 = arith.muli %52, %44 : i64
%53 = arith.addi %42, %51 : i64
%54 = arith.constant 43 : i32
%56 = arith.constant 15 : i32
%57 = llvm.load %37 : !llvm.ptr -> i64
%58 = arith.constant 1 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.subi %57, %60 : i64
%61 = arith.extsi %56 : i32 to i64
%55 = func.call @ipow(%61, %59) : (i64, i64) -> i64
%63 = arith.extsi %54 : i32 to i64
%62 = arith.muli %63, %55 : i64
%64 = arith.subi %53, %62 : i64
%65 = arith.constant 41 : i32
%67 = arith.constant 14 : i32
%68 = llvm.load %37 : !llvm.ptr -> i64
%69 = arith.constant 1 : i32
%71 = arith.extsi %69 : i32 to i64
%70 = arith.subi %68, %71 : i64
%72 = arith.extsi %67 : i32 to i64
%66 = func.call @ipow(%72, %70) : (i64, i64) -> i64
%74 = arith.extsi %65 : i32 to i64
%73 = arith.muli %74, %66 : i64
%75 = arith.addi %64, %73 : i64
%77 = arith.constant 13 : i32
%78 = llvm.load %37 : !llvm.ptr -> i64
%79 = arith.extsi %77 : i32 to i64
%76 = func.call @ipow(%79, %78) : (i64, i64) -> i64
%80 = arith.subi %75, %76 : i64
llvm.store %80, %33 : i64, !llvm.ptr
%81 = llvm.load %37 : !llvm.ptr -> i64
%82 = arith.constant 1 : i32
%84 = arith.extsi %82 : i32 to i64
%83 = arith.addi %81, %84 : i64
llvm.store %83, %37 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%85 = llvm.mlir.addressof @str_0 : !llvm.ptr
%86 = llvm.load %33 : !llvm.ptr -> i64
%87 = llvm.call @printf(%85, %86) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%88 = arith.constant 0 : i32
func.return %88 : i32
}
}