Problem 686
Powers of two with leading digits 123: p(123, 678910).
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | ? |
| Space complexity | O(1) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 686
# Powers of two with leading digits 123: p(123, 678910).
extern {
function log(x: f64) -> f64
function floor(x: f64) -> f64
}
function main() -> i32 {
let L: i64 = 123
let n: i64 = 678910
let digits: f64 = 3.0
let lower: f64 = log(L as f64) / log(10.0) - (digits - 1.0)
let upper: f64 = log((L + 1) as f64) / log(10.0) - (digits - 1.0)
let cons: f64 = log(2.0) / log(10.0)
let mut count: i64 = 0
let mut j: i64 = 0
while count != n {
j = j + 1
let temp: f64 = (j as f64) * cons
let frac: f64 = temp - floor(temp)
if frac > lower && frac < upper {
count = count + 1
}
}
printf("%lld\n", j)
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 L = 123;
int64_t n = 678910;
double digits = 3.0;
double lower = ((log(((double)(L))) / log(10.0)) - (digits - 1.0));
double upper = ((log(((double)((L + 1)))) / log(10.0)) - (digits - 1.0));
double cons = (log(2.0) / log(10.0));
int64_t count = 0;
int64_t j = 0;
while (count != n) {
j = (j + 1);
double temp = (((double)(j)) * cons);
double frac = (temp - floor(temp));
if ((frac > lower && frac < upper)) {
count = (count + 1);
}
}
printf("%lld\n", j);
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 @log(f64) -> f64
func.func private @floor(f64) -> f64
func.func @main() -> i32 {
%0 = arith.constant 123 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 678910 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = arith.constant 3.0 : f32
%5 = arith.extf %4 : f32 to f64
%6 = arith.sitofp %1 : i64 to f64
%7 = math.log %6 : f64
%8 = arith.constant 10.0 : f32
%9 = math.log %8 : f32
%10 = arith.divf %7, %9 : f64
%11 = arith.constant 1.0 : f32
%13 = arith.extf %11 : f32 to f64
%12 = arith.subf %5, %13 : f64
%14 = arith.subf %10, %12 : f64
%15 = arith.constant 1 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.addi %1, %17 : i64
%18 = arith.sitofp %16 : i64 to f64
%19 = math.log %18 : f64
%20 = arith.constant 10.0 : f32
%21 = math.log %20 : f32
%22 = arith.divf %19, %21 : f64
%23 = arith.constant 1.0 : f32
%25 = arith.extf %23 : f32 to f64
%24 = arith.subf %5, %25 : f64
%26 = arith.subf %22, %24 : f64
%27 = arith.constant 2.0 : f32
%28 = math.log %27 : f32
%29 = arith.constant 10.0 : f32
%30 = math.log %29 : f32
%31 = arith.divf %28, %30 : f64
%32 = arith.constant 0 : i32
%33 = arith.extsi %32 : i32 to i64
%34 = llvm.mlir.constant(1 : i64) : i64
%35 = llvm.alloca %34 x i64 : (i64) -> !llvm.ptr
llvm.store %33, %35 : i64, !llvm.ptr
%36 = arith.constant 0 : i32
%37 = arith.extsi %36 : i32 to i64
%38 = llvm.mlir.constant(1 : i64) : i64
%39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
llvm.store %37, %39 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%40 = llvm.load %35 : !llvm.ptr -> i64
%41 = arith.cmpi ne, %40, %3 : i64
cf.cond_br %41, ^bb1, ^bb2
^bb1:
%42 = llvm.load %39 : !llvm.ptr -> i64
%43 = arith.constant 1 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.addi %42, %45 : i64
llvm.store %44, %39 : i64, !llvm.ptr
%46 = llvm.load %39 : !llvm.ptr -> i64
%47 = arith.sitofp %46 : i64 to f64
%48 = arith.mulf %47, %31 : f64
%49 = func.call @floor(%48) : (f64) -> f64
%50 = arith.subf %48, %49 : f64
%51 = arith.cmpf ogt, %50, %14 : f64
%52 = scf.if %51 -> (i1) {
%53 = arith.cmpf olt, %50, %26 : f64
scf.yield %53 : i1
} else {
%54 = arith.constant false
scf.yield %54 : i1
}
cf.cond_br %52, ^bb3, ^bb4
^bb3:
%55 = llvm.load %35 : !llvm.ptr -> i64
%56 = arith.constant 1 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.addi %55, %58 : i64
llvm.store %57, %35 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
cf.br ^bb0
^bb2:
%59 = llvm.mlir.addressof @str_0 : !llvm.ptr
%60 = llvm.load %39 : !llvm.ptr -> i64
%61 = llvm.call @printf(%59, %60) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%62 = arith.constant 0 : i32
func.return %62 : i32
}
}