← All problems
Problem 112
Least n such that the proportion of bouncy numbers is exactly 99%.
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 112
# Least n such that the proportion of bouncy numbers is exactly 99%.
function is_bouncy(n0: i64) -> bool {
let mut n: i64 = n0
let mut prev: i64 = n % 10
n = n / 10
let mut inc: bool = false
let mut dec: bool = false
while n > 0 {
let d: i64 = n % 10
if d < prev { inc = true }
if d > prev { dec = true }
if inc && dec { return true }
prev = d
n = n / 10
}
return false
}
function main() -> i32 {
let mut bouncy: i64 = 0
let mut n: i64 = 1
while true {
if is_bouncy(n) {
bouncy = bouncy + 1
}
if n > 100 && bouncy * 100 == n * 99 {
printf("%lld\n", n)
return 0
}
n = n + 1
if n > 10000000 {
printf("0\n")
return 1
}
}
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; }
bool is_bouncy_i64(int64_t n0);
int32_t main(void);
bool is_bouncy_i64(int64_t n0) {
int64_t n = n0;
int64_t prev = FLOW_CHECKED_MOD((n), (10));
n = FLOW_CHECKED_DIV((n), (10));
bool inc = 0;
bool dec = 0;
while (n > 0) {
int64_t d = FLOW_CHECKED_MOD((n), (10));
if (d < prev) {
inc = 1;
}
if (d > prev) {
dec = 1;
}
if ((inc && dec)) {
return 1;
}
prev = d;
n = FLOW_CHECKED_DIV((n), (10));
}
return 0;
}
int32_t main(void) {
int64_t bouncy = 0;
int64_t n = 1;
while (1) {
if (is_bouncy_i64(n)) {
bouncy = (bouncy + 1);
}
if ((n > 100 && (bouncy * 100) == (n * 99))) {
printf("%lld\n", n);
return 0;
}
n = (n + 1);
if (n > 10000000) {
printf("0\n");
return 1;
}
}
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>
llvm.mlir.global internal constant @str_1("0\n\00") {addr_space = 0 : i32} : !llvm.array<3 x i8>
func.func @is_bouncy(%arg0: i64) -> i1 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = llvm.load %1 : !llvm.ptr -> i64
%3 = arith.constant 10 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.remsi %2, %5 : i64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
llvm.store %4, %7 : i64, !llvm.ptr
%8 = llvm.load %1 : !llvm.ptr -> i64
%9 = arith.constant 10 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.divsi %8, %11 : i64
llvm.store %10, %1 : i64, !llvm.ptr
%12 = arith.constant 0 : i1
%13 = llvm.mlir.constant(1 : i64) : i64
%14 = llvm.alloca %13 x i1 : (i64) -> !llvm.ptr
llvm.store %12, %14 : i1, !llvm.ptr
%15 = arith.constant 0 : i1
%16 = llvm.mlir.constant(1 : i64) : i64
%17 = llvm.alloca %16 x i1 : (i64) -> !llvm.ptr
llvm.store %15, %17 : i1, !llvm.ptr
cf.br ^bb0
^bb0:
%18 = llvm.load %1 : !llvm.ptr -> i64
%19 = arith.constant 0 : i32
%21 = arith.extsi %19 : i32 to i64
%20 = arith.cmpi sgt, %18, %21 : i64
cf.cond_br %20, ^bb1, ^bb2
^bb1:
%22 = llvm.load %1 : !llvm.ptr -> i64
%23 = arith.constant 10 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.remsi %22, %25 : i64
%26 = llvm.load %7 : !llvm.ptr -> i64
%27 = arith.cmpi slt, %24, %26 : i64
cf.cond_br %27, ^bb3, ^bb4
^bb3:
%28 = arith.constant 1 : i1
llvm.store %28, %14 : i1, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%29 = llvm.load %7 : !llvm.ptr -> i64
%30 = arith.cmpi sgt, %24, %29 : i64
cf.cond_br %30, ^bb6, ^bb7
^bb6:
%31 = arith.constant 1 : i1
llvm.store %31, %17 : i1, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%32 = llvm.load %14 : !llvm.ptr -> i1
%33 = scf.if %32 -> (i1) {
%34 = llvm.load %17 : !llvm.ptr -> i1
scf.yield %34 : i1
} else {
%35 = arith.constant false
scf.yield %35 : i1
}
cf.cond_br %33, ^bb9, ^bb10
^bb9:
%36 = arith.constant 1 : i1
func.return %36 : i1
^bb10:
cf.br ^bb11
^bb11:
llvm.store %24, %7 : i64, !llvm.ptr
%37 = llvm.load %1 : !llvm.ptr -> i64
%38 = arith.constant 10 : i32
%40 = arith.extsi %38 : i32 to i64
%39 = arith.divsi %37, %40 : i64
llvm.store %39, %1 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%41 = arith.constant 0 : i1
func.return %41 : i1
}
func.func @main() -> i32 {
%42 = arith.constant 0 : i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i64, !llvm.ptr
%46 = arith.constant 1 : i32
%47 = arith.extsi %46 : i32 to i64
%48 = llvm.mlir.constant(1 : i64) : i64
%49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
llvm.store %47, %49 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%50 = arith.constant 1 : i1
cf.cond_br %50, ^bb13, ^bb14
^bb13:
%52 = llvm.load %49 : !llvm.ptr -> i64
%51 = func.call @is_bouncy(%52) : (i64) -> i1
cf.cond_br %51, ^bb15, ^bb16
^bb15:
%53 = llvm.load %45 : !llvm.ptr -> i64
%54 = arith.constant 1 : i32
%56 = arith.extsi %54 : i32 to i64
%55 = arith.addi %53, %56 : i64
llvm.store %55, %45 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%57 = llvm.load %49 : !llvm.ptr -> i64
%58 = arith.constant 100 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.cmpi sgt, %57, %60 : i64
%61 = scf.if %59 -> (i1) {
%62 = llvm.load %45 : !llvm.ptr -> i64
%63 = arith.constant 100 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.muli %62, %65 : i64
%66 = llvm.load %49 : !llvm.ptr -> i64
%67 = arith.constant 99 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.muli %66, %69 : i64
%70 = arith.cmpi eq, %64, %68 : i64
scf.yield %70 : i1
} else {
%71 = arith.constant false
scf.yield %71 : i1
}
cf.cond_br %61, ^bb18, ^bb19
^bb18:
%72 = llvm.mlir.addressof @str_0 : !llvm.ptr
%73 = llvm.load %49 : !llvm.ptr -> i64
%74 = llvm.call @printf(%72, %73) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%75 = arith.constant 0 : i32
func.return %75 : i32
^bb19:
cf.br ^bb20
^bb20:
%76 = llvm.load %49 : !llvm.ptr -> i64
%77 = arith.constant 1 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.addi %76, %79 : i64
llvm.store %78, %49 : i64, !llvm.ptr
%80 = llvm.load %49 : !llvm.ptr -> i64
%81 = arith.constant 10000000 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.cmpi sgt, %80, %83 : i64
cf.cond_br %82, ^bb21, ^bb22
^bb21:
%84 = llvm.mlir.addressof @str_1 : !llvm.ptr
%85 = llvm.call @printf(%84) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
%86 = arith.constant 1 : i32
func.return %86 : i32
^bb22:
cf.br ^bb23
^bb23:
cf.br ^bb12
^bb14:
%87 = arith.constant 0 : i32
func.return %87 : i32
}
}