← All problems
Problem 110
Least n such that 1/x + 1/y = 1/n has over 4_000_000 solutions.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n log n)O(n log n)
Space complexity O(1)O(1)
Approach Flow solution Divisor-based Diophantine solver
Verdict Optimal
Flow source
# Project Euler 110
# Least n such that 1/x + 1/y = 1/n has over 4_000_000 solutions.
function search(primes: array<i64, 15>, idx: i32, n: i64, divs: i64, maxe: i32, limit: i64, best0: i64) -> i64 {
let mut best: i64 = best0
if divs > limit {
if n < best { best = n }
return best
}
if idx >= 15 { return best }
let p: i64 = primes[idx]
let mut m: i64 = n
let mut e: i32 = 1
while e <= maxe {
if m > best / p { break }
m = m * p
best = search(primes, idx + 1, m, divs * (2 * (e as i64) + 1), e, limit, best)
e = e + 1
}
return best
}
function main() -> i32 {
let primes: array<i64, 15>
primes[0] = 2
primes[1] = 3
primes[2] = 5
primes[3] = 7
primes[4] = 11
primes[5] = 13
primes[6] = 17
primes[7] = 19
primes[8] = 23
primes[9] = 29
primes[10] = 31
primes[11] = 37
primes[12] = 41
primes[13] = 43
primes[14] = 47
let best: i64 = search(primes, 0, 1, 1, 60, 8000000, 9223372036854775807)
printf("%lld\n", best)
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 search_array_15_i64_i32_i64_i64_i32_i64_i64(int64_t* primes, int32_t idx, int64_t n, int64_t divs, int32_t maxe, int64_t limit, int64_t best0);
int32_t main(void);
int64_t search_array_15_i64_i32_i64_i64_i32_i64_i64(int64_t* primes, int32_t idx, int64_t n, int64_t divs, int32_t maxe, int64_t limit, int64_t best0) {
int64_t best = best0;
if (divs > limit) {
if (n < best) {
best = n;
}
return best;
}
if (idx >= 15) {
return best;
}
int64_t p = (((unsigned)(idx) < 15) ? primes[idx] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(idx), 15), flow_fault_handler("array index out of bounds"), primes[0]));
int64_t m = n;
int32_t e = 1;
while (e <= maxe) {
if (m > FLOW_CHECKED_DIV((best), (p))) {
break;
}
m = (m * p);
best = search_array_15_i64_i32_i64_i64_i32_i64_i64(primes, (idx + 1), m, (divs * ((2 * ((int64_t)(e))) + 1)), e, limit, best);
e = (e + 1);
}
return best;
}
int32_t main(void) {
int64_t primes[15];
primes[0] = 2;
primes[1] = 3;
primes[2] = 5;
primes[3] = 7;
primes[4] = 11;
primes[5] = 13;
primes[6] = 17;
primes[7] = 19;
primes[8] = 23;
primes[9] = 29;
primes[10] = 31;
primes[11] = 37;
primes[12] = 41;
primes[13] = 43;
primes[14] = 47;
int64_t best = search_array_15_i64_i32_i64_i64_i32_i64_i64(primes, 0, 1, 1, 60, 8000000, 9223372036854775807);
printf("%lld\n", best);
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 @search(%arg0: !llvm.ptr, %arg1: i32, %arg2: i64, %arg3: i64, %arg4: i32, %arg5: i64, %arg6: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg6, %1 : i64, !llvm.ptr
%2 = arith.cmpi sgt, %arg3, %arg5 : i64
cf.cond_br %2, ^bb0, ^bb1
^bb0:
%3 = llvm.load %1 : !llvm.ptr -> i64
%4 = arith.cmpi slt, %arg2, %3 : i64
cf.cond_br %4, ^bb3, ^bb4
^bb3:
llvm.store %arg2, %1 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%5 = llvm.load %1 : !llvm.ptr -> i64
func.return %5 : i64
^bb1:
cf.br ^bb2
^bb2:
%6 = arith.constant 15 : i32
%7 = arith.cmpi sge, %arg1, %6 : i32
cf.cond_br %7, ^bb6, ^bb7
^bb6:
%8 = llvm.load %1 : !llvm.ptr -> i64
func.return %8 : i64
^bb7:
cf.br ^bb8
^bb8:
%10 = arith.extsi %arg1 : i32 to i64
%11 = llvm.getelementptr %arg0[0, %10] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<15 x i64>
%9 = llvm.load %11 : !llvm.ptr -> i64
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
llvm.store %arg2, %13 : i64, !llvm.ptr
%14 = arith.constant 1 : i32
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i32 : (i64) -> !llvm.ptr
llvm.store %14, %16 : i32, !llvm.ptr
cf.br ^bb9
^bb9:
%17 = llvm.load %16 : !llvm.ptr -> i32
%18 = arith.cmpi sle, %17, %arg4 : i32
cf.cond_br %18, ^bb10, ^bb11
^bb10:
%19 = llvm.load %13 : !llvm.ptr -> i64
%20 = llvm.load %1 : !llvm.ptr -> i64
%21 = arith.divsi %20, %9 : i64
%22 = arith.cmpi sgt, %19, %21 : i64
cf.cond_br %22, ^bb12, ^bb13
^bb12:
cf.br ^bb11
^bb13:
cf.br ^bb14
^bb14:
%23 = llvm.load %13 : !llvm.ptr -> i64
%24 = arith.muli %23, %9 : i64
llvm.store %24, %13 : i64, !llvm.ptr
%26 = arith.constant 1 : i32
%27 = arith.addi %arg1, %26 : i32
%28 = llvm.load %13 : !llvm.ptr -> i64
%29 = arith.constant 2 : i32
%30 = llvm.load %16 : !llvm.ptr -> i32
%31 = arith.extsi %30 : i32 to i64
%33 = arith.extsi %29 : i32 to i64
%32 = arith.muli %33, %31 : i64
%34 = arith.constant 1 : i32
%36 = arith.extsi %34 : i32 to i64
%35 = arith.addi %32, %36 : i64
%37 = arith.muli %arg3, %35 : i64
%38 = llvm.load %16 : !llvm.ptr -> i32
%39 = llvm.load %1 : !llvm.ptr -> i64
%25 = func.call @search(%arg0, %27, %28, %37, %38, %arg5, %39) : (!llvm.ptr, i32, i64, i64, i32, i64, i64) -> i64
llvm.store %25, %1 : i64, !llvm.ptr
%40 = llvm.load %16 : !llvm.ptr -> i32
%41 = arith.constant 1 : i32
%42 = arith.addi %40, %41 : i32
llvm.store %42, %16 : i32, !llvm.ptr
cf.br ^bb9
^bb11:
%43 = llvm.load %1 : !llvm.ptr -> i64
func.return %43 : i64
}
func.func @main() -> i32 {
%44 = memref.alloca() {type = memref<15xi64>} : memref<15xi64>
%45 = arith.constant 2 : i32
%46 = arith.constant 0 : i32
%47 = arith.index_cast %46 : i32 to index
%48 = arith.extsi %45 : i32 to i64
memref.store %48, %44[%47] : memref<15xi64>
%49 = arith.constant 3 : i32
%50 = arith.constant 1 : i32
%51 = arith.index_cast %50 : i32 to index
%52 = arith.extsi %49 : i32 to i64
memref.store %52, %44[%51] : memref<15xi64>
%53 = arith.constant 5 : i32
%54 = arith.constant 2 : i32
%55 = arith.index_cast %54 : i32 to index
%56 = arith.extsi %53 : i32 to i64
memref.store %56, %44[%55] : memref<15xi64>
%57 = arith.constant 7 : i32
%58 = arith.constant 3 : i32
%59 = arith.index_cast %58 : i32 to index
%60 = arith.extsi %57 : i32 to i64
memref.store %60, %44[%59] : memref<15xi64>
%61 = arith.constant 11 : i32
%62 = arith.constant 4 : i32
%63 = arith.index_cast %62 : i32 to index
%64 = arith.extsi %61 : i32 to i64
memref.store %64, %44[%63] : memref<15xi64>
%65 = arith.constant 13 : i32
%66 = arith.constant 5 : i32
%67 = arith.index_cast %66 : i32 to index
%68 = arith.extsi %65 : i32 to i64
memref.store %68, %44[%67] : memref<15xi64>
%69 = arith.constant 17 : i32
%70 = arith.constant 6 : i32
%71 = arith.index_cast %70 : i32 to index
%72 = arith.extsi %69 : i32 to i64
memref.store %72, %44[%71] : memref<15xi64>
%73 = arith.constant 19 : i32
%74 = arith.constant 7 : i32
%75 = arith.index_cast %74 : i32 to index
%76 = arith.extsi %73 : i32 to i64
memref.store %76, %44[%75] : memref<15xi64>
%77 = arith.constant 23 : i32
%78 = arith.constant 8 : i32
%79 = arith.index_cast %78 : i32 to index
%80 = arith.extsi %77 : i32 to i64
memref.store %80, %44[%79] : memref<15xi64>
%81 = arith.constant 29 : i32
%82 = arith.constant 9 : i32
%83 = arith.index_cast %82 : i32 to index
%84 = arith.extsi %81 : i32 to i64
memref.store %84, %44[%83] : memref<15xi64>
%85 = arith.constant 31 : i32
%86 = arith.constant 10 : i32
%87 = arith.index_cast %86 : i32 to index
%88 = arith.extsi %85 : i32 to i64
memref.store %88, %44[%87] : memref<15xi64>
%89 = arith.constant 37 : i32
%90 = arith.constant 11 : i32
%91 = arith.index_cast %90 : i32 to index
%92 = arith.extsi %89 : i32 to i64
memref.store %92, %44[%91] : memref<15xi64>
%93 = arith.constant 41 : i32
%94 = arith.constant 12 : i32
%95 = arith.index_cast %94 : i32 to index
%96 = arith.extsi %93 : i32 to i64
memref.store %96, %44[%95] : memref<15xi64>
%97 = arith.constant 43 : i32
%98 = arith.constant 13 : i32
%99 = arith.index_cast %98 : i32 to index
%100 = arith.extsi %97 : i32 to i64
memref.store %100, %44[%99] : memref<15xi64>
%101 = arith.constant 47 : i32
%102 = arith.constant 14 : i32
%103 = arith.index_cast %102 : i32 to index
%104 = arith.extsi %101 : i32 to i64
memref.store %104, %44[%103] : memref<15xi64>
%106 = arith.constant 0 : i32
%107 = arith.constant 1 : i32
%108 = arith.constant 1 : i32
%109 = arith.constant 60 : i32
%110 = arith.constant 8000000 : i32
%111 = arith.constant 9223372032559808511 : i32
%113 = memref.extract_aligned_pointer_as_index %44 : memref<15xi64> -> index
%114 = arith.index_cast %113 : index to i64
%115 = llvm.inttoptr %114 : i64 to !llvm.ptr
%116 = arith.extsi %107 : i32 to i64
%117 = arith.extsi %108 : i32 to i64
%118 = arith.extsi %110 : i32 to i64
%119 = arith.extsi %111 : i32 to i64
%105 = func.call @search(%115, %106, %116, %117, %109, %118, %119) : (!llvm.ptr, i32, i64, i64, i32, i64, i64) -> i64
%120 = llvm.mlir.addressof @str_0 : !llvm.ptr
%121 = llvm.call @printf(%120, %105) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%122 = arith.constant 0 : i32
func.return %122 : i32
}
}