Problem 144
Laser reflections in an elliptical mirror until exit.
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 | Iterative reflection simulation |
| Verdict | Optimal |
Flow source
# Project Euler 144
# Laser reflections in an elliptical mirror until exit.
extern {
function sqrt(x: f64) -> f64
}
function main() -> i32 {
let mut steps: i64 = 0
let mut from_x: f64 = 0.0
let mut from_y: f64 = 10.1
let mut to_x: f64 = 1.4
let mut to_y: f64 = 0.0 - 9.6
while true {
if to_x >= (0.0 - 0.01) && to_x <= 0.01 && to_y > 9.9 {
break
}
let mut nx: f64 = 0.0 - 4.0 * to_x
let mut ny: f64 = 0.0 - to_y
let length: f64 = sqrt(nx * nx + ny * ny)
nx = nx / length
ny = ny / length
let dx: f64 = to_x - from_x
let dy: f64 = to_y - from_y
let dot: f64 = dx * nx + dy * ny
let rx: f64 = dx - 2.0 * dot * nx
let ry: f64 = dy - 2.0 * dot * ny
let slope: f64 = ry / rx
from_x = to_x
from_y = to_y
to_x = (4.0 * from_x - slope * slope * from_x + 2.0 * slope * from_y) / (0.0 - 4.0 - slope * slope)
to_y = slope * (to_x - from_x) + from_y
steps = steps + 1
}
printf("%lld\n", steps)
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 steps = 0;
double from_x = 0.0;
double from_y = 10.1;
double to_x = 1.4;
double to_y = (0.0 - 9.6);
while (1) {
if (((to_x >= (0.0 - 0.01) && to_x <= 0.01) && to_y > 9.9)) {
break;
}
double nx = (0.0 - (4.0 * to_x));
double ny = (0.0 - to_y);
double length = sqrt(((nx * nx) + (ny * ny)));
nx = (nx / length);
ny = (ny / length);
double dx = (to_x - from_x);
double dy = (to_y - from_y);
double dot = ((dx * nx) + (dy * ny));
double rx = (dx - ((2.0 * dot) * nx));
double ry = (dy - ((2.0 * dot) * ny));
double slope = (ry / rx);
from_x = to_x;
from_y = to_y;
to_x = ((((4.0 * from_x) - ((slope * slope) * from_x)) + ((2.0 * slope) * from_y)) / ((0.0 - 4.0) - (slope * slope)));
to_y = ((slope * (to_x - from_x)) + from_y);
steps = (steps + 1);
}
printf("%lld\n", steps);
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 @sqrt(f64) -> f64
func.func @main() -> i32 {
%0 = arith.constant 0 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
%4 = arith.constant 0.0 : f32
%5 = arith.extf %4 : f32 to f64
%6 = llvm.mlir.constant(1 : i64) : i64
%7 = llvm.alloca %6 x f64 : (i64) -> !llvm.ptr
llvm.store %5, %7 : f64, !llvm.ptr
%8 = arith.constant 10.1 : f32
%9 = arith.extf %8 : f32 to f64
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x f64 : (i64) -> !llvm.ptr
llvm.store %9, %11 : f64, !llvm.ptr
%12 = arith.constant 1.4 : f32
%13 = arith.extf %12 : f32 to f64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x f64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : f64, !llvm.ptr
%16 = arith.constant 0.0 : f32
%17 = arith.constant 9.6 : f32
%18 = arith.subf %16, %17 : f32
%19 = arith.extf %18 : f32 to f64
%20 = llvm.mlir.constant(1 : i64) : i64
%21 = llvm.alloca %20 x f64 : (i64) -> !llvm.ptr
llvm.store %19, %21 : f64, !llvm.ptr
cf.br ^bb0
^bb0:
%22 = arith.constant 1 : i1
cf.cond_br %22, ^bb1, ^bb2
^bb1:
%23 = llvm.load %15 : !llvm.ptr -> f64
%24 = arith.constant 0.0 : f32
%25 = arith.constant 0.01 : f32
%26 = arith.subf %24, %25 : f32
%28 = arith.extf %26 : f32 to f64
%27 = arith.cmpf oge, %23, %28 : f64
%29 = scf.if %27 -> (i1) {
%30 = llvm.load %15 : !llvm.ptr -> f64
%31 = arith.constant 0.01 : f32
%33 = arith.extf %31 : f32 to f64
%32 = arith.cmpf ole, %30, %33 : f64
scf.yield %32 : i1
} else {
%34 = arith.constant false
scf.yield %34 : i1
}
%35 = scf.if %29 -> (i1) {
%36 = llvm.load %21 : !llvm.ptr -> f64
%37 = arith.constant 9.9 : f32
%39 = arith.extf %37 : f32 to f64
%38 = arith.cmpf ogt, %36, %39 : f64
scf.yield %38 : i1
} else {
%40 = arith.constant false
scf.yield %40 : i1
}
cf.cond_br %35, ^bb3, ^bb4
^bb3:
cf.br ^bb2
^bb4:
cf.br ^bb5
^bb5:
%41 = arith.constant 0.0 : f32
%42 = arith.constant 4.0 : f32
%43 = llvm.load %15 : !llvm.ptr -> f64
%45 = arith.extf %42 : f32 to f64
%44 = arith.mulf %45, %43 : f64
%47 = arith.extf %41 : f32 to f64
%46 = arith.subf %47, %44 : f64
%48 = llvm.mlir.constant(1 : i64) : i64
%49 = llvm.alloca %48 x f64 : (i64) -> !llvm.ptr
llvm.store %46, %49 : f64, !llvm.ptr
%50 = arith.constant 0.0 : f32
%51 = llvm.load %21 : !llvm.ptr -> f64
%53 = arith.extf %50 : f32 to f64
%52 = arith.subf %53, %51 : f64
%54 = llvm.mlir.constant(1 : i64) : i64
%55 = llvm.alloca %54 x f64 : (i64) -> !llvm.ptr
llvm.store %52, %55 : f64, !llvm.ptr
%56 = llvm.load %49 : !llvm.ptr -> f64
%57 = llvm.load %49 : !llvm.ptr -> f64
%58 = arith.mulf %56, %57 : f64
%59 = llvm.load %55 : !llvm.ptr -> f64
%60 = llvm.load %55 : !llvm.ptr -> f64
%61 = arith.mulf %59, %60 : f64
%62 = arith.addf %58, %61 : f64
%63 = math.sqrt %62 : f64
%64 = llvm.load %49 : !llvm.ptr -> f64
%65 = arith.divf %64, %63 : f64
llvm.store %65, %49 : f64, !llvm.ptr
%66 = llvm.load %55 : !llvm.ptr -> f64
%67 = arith.divf %66, %63 : f64
llvm.store %67, %55 : f64, !llvm.ptr
%68 = llvm.load %15 : !llvm.ptr -> f64
%69 = llvm.load %7 : !llvm.ptr -> f64
%70 = arith.subf %68, %69 : f64
%71 = llvm.load %21 : !llvm.ptr -> f64
%72 = llvm.load %11 : !llvm.ptr -> f64
%73 = arith.subf %71, %72 : f64
%74 = llvm.load %49 : !llvm.ptr -> f64
%75 = arith.mulf %70, %74 : f64
%76 = llvm.load %55 : !llvm.ptr -> f64
%77 = arith.mulf %73, %76 : f64
%78 = arith.addf %75, %77 : f64
%79 = arith.constant 2.0 : f32
%81 = arith.extf %79 : f32 to f64
%80 = arith.mulf %81, %78 : f64
%82 = llvm.load %49 : !llvm.ptr -> f64
%83 = arith.mulf %80, %82 : f64
%84 = arith.subf %70, %83 : f64
%85 = arith.constant 2.0 : f32
%87 = arith.extf %85 : f32 to f64
%86 = arith.mulf %87, %78 : f64
%88 = llvm.load %55 : !llvm.ptr -> f64
%89 = arith.mulf %86, %88 : f64
%90 = arith.subf %73, %89 : f64
%91 = arith.divf %90, %84 : f64
%92 = llvm.load %15 : !llvm.ptr -> f64
llvm.store %92, %7 : f64, !llvm.ptr
%93 = llvm.load %21 : !llvm.ptr -> f64
llvm.store %93, %11 : f64, !llvm.ptr
%94 = arith.constant 4.0 : f32
%95 = llvm.load %7 : !llvm.ptr -> f64
%97 = arith.extf %94 : f32 to f64
%96 = arith.mulf %97, %95 : f64
%98 = arith.mulf %91, %91 : f64
%99 = llvm.load %7 : !llvm.ptr -> f64
%100 = arith.mulf %98, %99 : f64
%101 = arith.subf %96, %100 : f64
%102 = arith.constant 2.0 : f32
%104 = arith.extf %102 : f32 to f64
%103 = arith.mulf %104, %91 : f64
%105 = llvm.load %11 : !llvm.ptr -> f64
%106 = arith.mulf %103, %105 : f64
%107 = arith.addf %101, %106 : f64
%108 = arith.constant 0.0 : f32
%109 = arith.constant 4.0 : f32
%110 = arith.subf %108, %109 : f32
%111 = arith.mulf %91, %91 : f64
%113 = arith.extf %110 : f32 to f64
%112 = arith.subf %113, %111 : f64
%114 = arith.divf %107, %112 : f64
llvm.store %114, %15 : f64, !llvm.ptr
%115 = llvm.load %15 : !llvm.ptr -> f64
%116 = llvm.load %7 : !llvm.ptr -> f64
%117 = arith.subf %115, %116 : f64
%118 = arith.mulf %91, %117 : f64
%119 = llvm.load %11 : !llvm.ptr -> f64
%120 = arith.addf %118, %119 : f64
llvm.store %120, %21 : f64, !llvm.ptr
%121 = llvm.load %3 : !llvm.ptr -> i64
%122 = arith.constant 1 : i32
%124 = arith.extsi %122 : i32 to i64
%123 = arith.addi %121, %124 : i64
llvm.store %123, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%125 = llvm.mlir.addressof @str_0 : !llvm.ptr
%126 = llvm.load %3 : !llvm.ptr -> i64
%127 = llvm.call @printf(%125, %126) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%128 = arith.constant 0 : i32
func.return %128 : i32
}
}