← All problems
Problem 099
Line number of maximal base^exp (compare via exp*log(base)).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n)
Space complexity O(1)O(1)
Approach Flow solution Compare logarithms of large numbers
Verdict Optimal
Flow source
# Project Euler 099
# Line number of maximal base^exp (compare via exp*log(base)).
extern {
function fopen(path: string, mode: string) -> ptr<void>
function fgetc(f: ptr<void>) -> i32
function fclose(f: ptr<void>) -> i32
function log(x: f64) -> f64
}
function read_int(f: ptr<void>) -> i64 {
let mut c: i32 = fgetc(f)
while c >= 0 && (c < 48 || c > 57) {
c = fgetc(f)
}
if c < 0 { return 0 - 1 }
let mut v: i64 = 0
while c >= 48 && c <= 57 {
v = v * 10 + ((c - 48) as i64)
c = fgetc(f)
}
return v
}
function main() -> i32 {
let f: ptr<void> = fopen("data/p099.txt", "r")
if f == null {
printf("failed to read data/p099.txt\n")
return 1
}
let mut best: f64 = 0.0 - 1.0
let mut best_line: i64 = 0
let mut line: i64 = 1
while line <= 1000 {
let base: i64 = read_int(f)
let exp: i64 = read_int(f)
if base < 0 { break }
let score: f64 = (exp as f64) * log(base as f64)
if score > best {
best = score
best_line = line
}
line = line + 1
}
fclose(f)
printf("%lld\n", best_line)
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 read_int_ptr_void(void* f);
int32_t main(void);
int64_t read_int_ptr_void(void* f) {
int32_t c = fgetc(f);
while ((c >= 0 && (c < 48 || c > 57))) {
c = fgetc(f);
}
if (c < 0) {
return (0 - 1);
}
int64_t v = 0;
while ((c >= 48 && c <= 57)) {
v = ((v * 10) + ((int64_t)((c - 48))));
c = fgetc(f);
}
return v;
}
int32_t main(void) {
void* f = (void*)(fopen("data/p099.txt", "r"));
if (f == NULL) {
printf("failed to read data/p099.txt\n");
return 1;
}
double best = (0.0 - 1.0);
int64_t best_line = 0;
int64_t line = 1;
while (line <= 1000) {
int64_t base = read_int_ptr_void(f);
int64_t exp = read_int_ptr_void(f);
if (base < 0) {
break;
}
double score = (((double)(exp)) * log(((double)(base))));
if (score > best) {
best = score;
best_line = line;
}
line = (line + 1);
}
fclose(f);
printf("%lld\n", best_line);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("data/p099.txt\00") {addr_space = 0 : i32} : !llvm.array<14 x i8>
llvm.mlir.global internal constant @str_1("r\00") {addr_space = 0 : i32} : !llvm.array<2 x i8>
llvm.mlir.global internal constant @str_2("failed to read data/p099.txt\n\00") {addr_space = 0 : i32} : !llvm.array<30 x i8>
llvm.mlir.global internal constant @str_3("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @fopen(!llvm.ptr, !llvm.ptr) -> !llvm.ptr
func.func private @fgetc(!llvm.ptr) -> i32
func.func private @fclose(!llvm.ptr) -> i32
func.func private @log(f64) -> f64
func.func @read_int(%arg0: !llvm.ptr) -> i64 {
%0 = func.call @fgetc(%arg0) : (!llvm.ptr) -> i32
%1 = llvm.mlir.constant(1 : i64) : i64
%2 = llvm.alloca %1 x i32 : (i64) -> !llvm.ptr
llvm.store %0, %2 : i32, !llvm.ptr
cf.br ^bb0
^bb0:
%3 = llvm.load %2 : !llvm.ptr -> i32
%4 = arith.constant 0 : i32
%5 = arith.cmpi sge, %3, %4 : i32
%6 = scf.if %5 -> (i1) {
%7 = llvm.load %2 : !llvm.ptr -> i32
%8 = arith.constant 48 : i32
%9 = arith.cmpi slt, %7, %8 : i32
%10 = scf.if %9 -> (i1) {
%11 = arith.constant true
scf.yield %11 : i1
} else {
%12 = llvm.load %2 : !llvm.ptr -> i32
%13 = arith.constant 57 : i32
%14 = arith.cmpi sgt, %12, %13 : i32
scf.yield %14 : i1
}
scf.yield %10 : i1
} else {
%15 = arith.constant false
scf.yield %15 : i1
}
cf.cond_br %6, ^bb1, ^bb2
^bb1:
%16 = func.call @fgetc(%arg0) : (!llvm.ptr) -> i32
llvm.store %16, %2 : i32, !llvm.ptr
cf.br ^bb0
^bb2:
%17 = llvm.load %2 : !llvm.ptr -> i32
%18 = arith.constant 0 : i32
%19 = arith.cmpi slt, %17, %18 : i32
cf.cond_br %19, ^bb3, ^bb4
^bb3:
%20 = arith.constant 0 : i32
%21 = arith.constant 1 : i32
%22 = arith.subi %20, %21 : i32
%23 = arith.extsi %22 : i32 to i64
func.return %23 : i64
^bb4:
cf.br ^bb5
^bb5:
%24 = arith.constant 0 : i32
%25 = arith.extsi %24 : i32 to i64
%26 = llvm.mlir.constant(1 : i64) : i64
%27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
llvm.store %25, %27 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%28 = llvm.load %2 : !llvm.ptr -> i32
%29 = arith.constant 48 : i32
%30 = arith.cmpi sge, %28, %29 : i32
%31 = scf.if %30 -> (i1) {
%32 = llvm.load %2 : !llvm.ptr -> i32
%33 = arith.constant 57 : i32
%34 = arith.cmpi sle, %32, %33 : i32
scf.yield %34 : i1
} else {
%35 = arith.constant false
scf.yield %35 : i1
}
cf.cond_br %31, ^bb7, ^bb8
^bb7:
%36 = llvm.load %27 : !llvm.ptr -> i64
%37 = arith.constant 10 : i32
%39 = arith.extsi %37 : i32 to i64
%38 = arith.muli %36, %39 : i64
%40 = llvm.load %2 : !llvm.ptr -> i32
%41 = arith.constant 48 : i32
%42 = arith.subi %40, %41 : i32
%43 = arith.extsi %42 : i32 to i64
%44 = arith.addi %38, %43 : i64
llvm.store %44, %27 : i64, !llvm.ptr
%45 = func.call @fgetc(%arg0) : (!llvm.ptr) -> i32
llvm.store %45, %2 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%46 = llvm.load %27 : !llvm.ptr -> i64
func.return %46 : i64
}
func.func @main() -> i32 {
%48 = llvm.mlir.addressof @str_0 : !llvm.ptr
%49 = llvm.mlir.addressof @str_1 : !llvm.ptr
%47 = func.call @fopen(%48, %49) : (!llvm.ptr, !llvm.ptr) -> !llvm.ptr
%50 = llvm.mlir.zero : !llvm.ptr
%51 = llvm.icmp "eq" %47, %50 : !llvm.ptr
cf.cond_br %51, ^bb9, ^bb10
^bb9:
%52 = llvm.mlir.addressof @str_2 : !llvm.ptr
%53 = llvm.call @printf(%52) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
%54 = arith.constant 1 : i32
func.return %54 : i32
^bb10:
cf.br ^bb11
^bb11:
%55 = arith.constant 0.0 : f32
%56 = arith.constant 1.0 : f32
%57 = arith.subf %55, %56 : f32
%58 = arith.extf %57 : f32 to f64
%59 = llvm.mlir.constant(1 : i64) : i64
%60 = llvm.alloca %59 x f64 : (i64) -> !llvm.ptr
llvm.store %58, %60 : f64, !llvm.ptr
%61 = arith.constant 0 : i32
%62 = arith.extsi %61 : i32 to i64
%63 = llvm.mlir.constant(1 : i64) : i64
%64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
llvm.store %62, %64 : i64, !llvm.ptr
%65 = arith.constant 1 : i32
%66 = arith.extsi %65 : i32 to i64
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i64 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%69 = llvm.load %68 : !llvm.ptr -> i64
%70 = arith.constant 1000 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.cmpi sle, %69, %72 : i64
cf.cond_br %71, ^bb13, ^bb14
^bb13:
%73 = func.call @read_int(%47) : (!llvm.ptr) -> i64
%74 = func.call @read_int(%47) : (!llvm.ptr) -> i64
%75 = arith.constant 0 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.cmpi slt, %73, %77 : i64
cf.cond_br %76, ^bb15, ^bb16
^bb15:
cf.br ^bb14
^bb16:
cf.br ^bb17
^bb17:
%78 = arith.sitofp %74 : i64 to f64
%79 = arith.sitofp %73 : i64 to f64
%80 = math.log %79 : f64
%81 = arith.mulf %78, %80 : f64
%82 = llvm.load %60 : !llvm.ptr -> f64
%83 = arith.cmpf ogt, %81, %82 : f64
cf.cond_br %83, ^bb18, ^bb19
^bb18:
llvm.store %81, %60 : f64, !llvm.ptr
%84 = llvm.load %68 : !llvm.ptr -> i64
llvm.store %84, %64 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%85 = llvm.load %68 : !llvm.ptr -> i64
%86 = arith.constant 1 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.addi %85, %88 : i64
llvm.store %87, %68 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%89 = func.call @fclose(%47) : (!llvm.ptr) -> i32
%90 = llvm.mlir.addressof @str_3 : !llvm.ptr
%91 = llvm.load %64 : !llvm.ptr -> i64
%92 = llvm.call @printf(%90, %91) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%93 = arith.constant 0 : i32
func.return %93 : i32
}
}