← All problems
Problem 085
Nearest to two million rectangles in a grid; return area of that grid.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2)
Space complexity O(1)O(1)
Approach Flow solution Closed-form or combinatorial counting
Verdict Optimal
Flow source
# Project Euler 085
# Nearest to two million rectangles in a grid; return area of that grid.
function rects(m: i64, n: i64) -> i64 {
return m * (m + 1) / 2 * n * (n + 1) / 2
}
function abs64(x: i64) -> i64 {
if x < 0 { return 0 - x }
return x
}
function main() -> i32 {
let target: i64 = 2000000
let mut best_diff: i64 = target
let mut best_area: i64 = 0
let mut m: i64 = 1
while m < 2000 {
let mut n: i64 = 1
while n <= m {
let r: i64 = rects(m, n)
let d: i64 = abs64(r - target)
if d < best_diff {
best_diff = d
best_area = m * n
}
if r > target + best_diff {
break
}
n = n + 1
}
m = m + 1
}
printf("%lld\n", best_area)
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 rects_i64_i64(int64_t m, int64_t n);
int64_t abs64_i64(int64_t x);
int32_t main(void);
int64_t rects_i64_i64(int64_t m, int64_t n) {
return FLOW_CHECKED_DIV((((FLOW_CHECKED_DIV(((m * (m + 1))), (2)) * n) * (n + 1))), (2));
}
int64_t abs64_i64(int64_t x) {
if (x < 0) {
return (0 - x);
}
return x;
}
int32_t main(void) {
int64_t target = 2000000;
int64_t best_diff = target;
int64_t best_area = 0;
int64_t m = 1;
while (m < 2000) {
int64_t n = 1;
while (n <= m) {
int64_t r = rects_i64_i64(m, n);
int64_t d = abs64_i64((r - target));
if (d < best_diff) {
best_diff = d;
best_area = (m * n);
}
if (r > (target + best_diff)) {
break;
}
n = (n + 1);
}
m = (m + 1);
}
printf("%lld\n", best_area);
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 @rects(%arg0: i64, %arg1: i64) -> i64 {
%0 = arith.constant 1 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.addi %arg0, %2 : i64
%3 = arith.muli %arg0, %1 : i64
%4 = arith.constant 2 : i32
%6 = arith.extsi %4 : i32 to i64
%5 = arith.divsi %3, %6 : i64
%7 = arith.muli %5, %arg1 : i64
%8 = arith.constant 1 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.addi %arg1, %10 : i64
%11 = arith.muli %7, %9 : i64
%12 = arith.constant 2 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.divsi %11, %14 : i64
func.return %13 : i64
}
func.func @abs64(%arg0: i64) -> i64 {
%15 = arith.constant 0 : i32
%17 = arith.extsi %15 : i32 to i64
%16 = arith.cmpi slt, %arg0, %17 : i64
cf.cond_br %16, ^bb0, ^bb1
^bb0:
%18 = arith.constant 0 : i32
%20 = arith.extsi %18 : i32 to i64
%19 = arith.subi %20, %arg0 : i64
func.return %19 : i64
^bb1:
cf.br ^bb2
^bb2:
func.return %arg0 : i64
}
func.func @main() -> i32 {
%21 = arith.constant 2000000 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
llvm.store %22, %24 : i64, !llvm.ptr
%25 = arith.constant 0 : i32
%26 = arith.extsi %25 : i32 to i64
%27 = llvm.mlir.constant(1 : i64) : i64
%28 = llvm.alloca %27 x i64 : (i64) -> !llvm.ptr
llvm.store %26, %28 : i64, !llvm.ptr
%29 = arith.constant 1 : i32
%30 = arith.extsi %29 : i32 to i64
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i64 : (i64) -> !llvm.ptr
llvm.store %30, %32 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%33 = llvm.load %32 : !llvm.ptr -> i64
%34 = arith.constant 2000 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.cmpi slt, %33, %36 : i64
cf.cond_br %35, ^bb4, ^bb5
^bb4:
%37 = arith.constant 1 : i32
%38 = arith.extsi %37 : i32 to i64
%39 = llvm.mlir.constant(1 : i64) : i64
%40 = llvm.alloca %39 x i64 : (i64) -> !llvm.ptr
llvm.store %38, %40 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%41 = llvm.load %40 : !llvm.ptr -> i64
%42 = llvm.load %32 : !llvm.ptr -> i64
%43 = arith.cmpi sle, %41, %42 : i64
cf.cond_br %43, ^bb7, ^bb8
^bb7:
%45 = llvm.load %32 : !llvm.ptr -> i64
%46 = llvm.load %40 : !llvm.ptr -> i64
%44 = func.call @rects(%45, %46) : (i64, i64) -> i64
%48 = arith.subi %44, %22 : i64
%47 = func.call @abs64(%48) : (i64) -> i64
%49 = llvm.load %24 : !llvm.ptr -> i64
%50 = arith.cmpi slt, %47, %49 : i64
cf.cond_br %50, ^bb9, ^bb10
^bb9:
llvm.store %47, %24 : i64, !llvm.ptr
%51 = llvm.load %32 : !llvm.ptr -> i64
%52 = llvm.load %40 : !llvm.ptr -> i64
%53 = arith.muli %51, %52 : i64
llvm.store %53, %28 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%54 = llvm.load %24 : !llvm.ptr -> i64
%55 = arith.addi %22, %54 : i64
%56 = arith.cmpi sgt, %44, %55 : i64
cf.cond_br %56, ^bb12, ^bb13
^bb12:
cf.br ^bb8
^bb13:
cf.br ^bb14
^bb14:
%57 = llvm.load %40 : !llvm.ptr -> i64
%58 = arith.constant 1 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.addi %57, %60 : i64
llvm.store %59, %40 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%61 = llvm.load %32 : !llvm.ptr -> i64
%62 = arith.constant 1 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.addi %61, %64 : i64
llvm.store %63, %32 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%65 = llvm.mlir.addressof @str_0 : !llvm.ptr
%66 = llvm.load %28 : !llvm.ptr -> i64
%67 = llvm.call @printf(%65, %66) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%68 = arith.constant 0 : i32
func.return %68 : i32
}
}