Problem 004

Largest palindrome product of two 3-digit numbers.

Answer906609
Output906609
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(1)O(1)
ApproachFlow solutionBrute-force product, check palindrome
VerdictOptimal

Flow source

# Project Euler 004
# Largest palindrome product of two 3-digit numbers.

function is_palindrome(n: i64) -> bool {
    let mut x: i64 = n
    let mut rev: i64 = 0
    while x > 0 {
        rev = rev * 10 + x % 10
        x = x / 10
    }
    return rev == n
}

function solve() -> i64 {
    let mut best: i64 = 0
    let mut a: i64 = 999
    while a >= 100 {
        let mut b: i64 = 999
        while b >= a {
            let prod: i64 = a * b
            if prod <= best {
                break
            }
            if is_palindrome(prod) {
                best = prod
            }
            b = b - 1
        }
        a = a - 1
    }
    return best
}

function main() -> i32 {
    printf("%lld\n", solve())
    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_palindrome_i64(int64_t n);
int64_t solve(void);
int32_t main(void);

bool is_palindrome_i64(int64_t n) {
    int64_t x = n;
    int64_t rev = 0;
    while (x > 0) {
        rev = ((rev * 10) + FLOW_CHECKED_MOD((x), (10)));
        x = FLOW_CHECKED_DIV((x), (10));
    }
    return rev == n;
}

int64_t solve(void) {
    int64_t best = 0;
    int64_t a = 999;
    while (a >= 100) {
        int64_t b = 999;
        while (b >= a) {
            int64_t prod = (a * b);
            if (prod <= best) {
                break;
            }
            if (is_palindrome_i64(prod)) {
                best = prod;
            }
            b = (b - 1);
        }
        a = (a - 1);
    }
    return best;
}

int32_t main(void) {
    printf("%lld\n", solve());
    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 @is_palindrome(%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 = arith.constant 0 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %5 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %6 = llvm.load %1 : !llvm.ptr -> i64
    %7 = arith.constant 0 : i32
    %9 = arith.extsi %7 : i32 to i64
    %8 = arith.cmpi sgt, %6, %9 : i64
    cf.cond_br %8, ^bb1, ^bb2
    ^bb1:
      %10 = llvm.load %5 : !llvm.ptr -> i64
      %11 = arith.constant 10 : i32
      %13 = arith.extsi %11 : i32 to i64
      %12 = arith.muli %10, %13 : i64
      %14 = llvm.load %1 : !llvm.ptr -> i64
      %15 = arith.constant 10 : i32
      %17 = arith.extsi %15 : i32 to i64
      %16 = arith.remsi %14, %17 : i64
      %18 = arith.addi %12, %16 : i64
      llvm.store %18, %5 : i64, !llvm.ptr
      %19 = llvm.load %1 : !llvm.ptr -> i64
      %20 = arith.constant 10 : i32
      %22 = arith.extsi %20 : i32 to i64
      %21 = arith.divsi %19, %22 : i64
      llvm.store %21, %1 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %23 = llvm.load %5 : !llvm.ptr -> i64
    %24 = arith.cmpi eq, %23, %arg0 : i64
    func.return %24 : i1
  }
  func.func @solve() -> i64 {
    %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 999 : 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 100 : i32
    %36 = arith.extsi %34 : i32 to i64
    %35 = arith.cmpi sge, %33, %36 : i64
    cf.cond_br %35, ^bb4, ^bb5
    ^bb4:
      %37 = arith.constant 999 : 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 sge, %41, %42 : i64
      cf.cond_br %43, ^bb7, ^bb8
      ^bb7:
        %44 = llvm.load %32 : !llvm.ptr -> i64
        %45 = llvm.load %40 : !llvm.ptr -> i64
        %46 = arith.muli %44, %45 : i64
        %47 = llvm.load %28 : !llvm.ptr -> i64
        %48 = arith.cmpi sle, %46, %47 : i64
        cf.cond_br %48, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %49 = func.call @is_palindrome(%46) : (i64) -> i1
        cf.cond_br %49, ^bb12, ^bb13
        ^bb12:
          llvm.store %46, %28 : i64, !llvm.ptr
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        %50 = llvm.load %40 : !llvm.ptr -> i64
        %51 = arith.constant 1 : i32
        %53 = arith.extsi %51 : i32 to i64
        %52 = arith.subi %50, %53 : i64
        llvm.store %52, %40 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %54 = llvm.load %32 : !llvm.ptr -> i64
      %55 = arith.constant 1 : i32
      %57 = arith.extsi %55 : i32 to i64
      %56 = arith.subi %54, %57 : i64
      llvm.store %56, %32 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %58 = llvm.load %28 : !llvm.ptr -> i64
    func.return %58 : i64
  }
  func.func @main() -> i32 {
    %59 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %60 = func.call @solve() : () -> 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
  }
}