Problem 038

Largest 1–9 pandigital 9-digit number formed as concatenated product of an integer with (1,2,...,n) where n > 1.

Answer932718654
Output932718654
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)
Space complexityO(1)O(1)
ApproachFlow solutionPandigital concatenation search
VerdictSuboptimal

Flow source

# Project Euler 038
# Largest 1–9 pandigital 9-digit number formed as concatenated product
# of an integer with (1,2,...,n) where n > 1.

function is_pandigital9(n: i64) -> bool {
    if n < 100000000 || n > 999999999 { return false }
    let mut seen: i32 = 0
    let mut x: i64 = n
    while x > 0 {
        let d: i32 = (x % 10) as i32
        if d == 0 { return false }
        let bit: i32 = 1 << d
        if (seen & bit) != 0 { return false }
        seen = seen | bit
        x = x / 10
    }
    return seen == 1022  # bits 1..9 set
}

function concat_prod(k: i64, n: i64) -> i64 {
    let mut result: i64 = 0
    let mut i: i64 = 1
    while i <= n {
        let mut part: i64 = k * i
        # shift result left by digits of part
        let mut p: i64 = part
        let mut mul: i64 = 1
        while p > 0 {
            mul = mul * 10
            p = p / 10
        }
        result = result * mul + part
        if result > 999999999 { return -1 }
        i = i + 1
    }
    return result
}

function main() -> i32 {
    let mut best: i64 = 0
    # n=2: k up to 4 digits; largest candidates near 9xxx
    let mut k: i64 = 1
    while k < 10000 {
        let mut n: i64 = 2
        while n <= 9 {
            let v: i64 = concat_prod(k, n)
            if v < 0 { break }
            if is_pandigital9(v) && v > best {
                best = v
            }
            n = n + 1
        }
        k = k + 1
    }
    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; }

bool is_pandigital9_i64(int64_t n);
int64_t concat_prod_i64_i64(int64_t k, int64_t n);
int32_t main(void);

bool is_pandigital9_i64(int64_t n) {
    if ((n < 100000000 || n > 999999999)) {
        return 0;
    }
    int32_t seen = 0;
    int64_t x = n;
    while (x > 0) {
        int32_t d = ((int32_t)(FLOW_CHECKED_MOD((x), (10))));
        if (d == 0) {
            return 0;
        }
        int32_t bit = FLOW_CHECKED_SHL((1), (d));
        if ((seen & bit) != 0) {
            return 0;
        }
        seen = (seen | bit);
        x = FLOW_CHECKED_DIV((x), (10));
    }
    return seen == 1022;
}

int64_t concat_prod_i64_i64(int64_t k, int64_t n) {
    int64_t result = 0;
    int64_t i = 1;
    while (i <= n) {
        int64_t part = (k * i);
        int64_t p = part;
        int64_t mul = 1;
        while (p > 0) {
            mul = (mul * 10);
            p = FLOW_CHECKED_DIV((p), (10));
        }
        result = ((result * mul) + part);
        if (result > 999999999) {
            return (-1);
        }
        i = (i + 1);
    }
    return result;
}

int32_t main(void) {
    int64_t best = 0;
    int64_t k = 1;
    while (k < 10000) {
        int64_t n = 2;
        while (n <= 9) {
            int64_t v = concat_prod_i64_i64(k, n);
            if (v < 0) {
                break;
            }
            if ((is_pandigital9_i64(v) && v > best)) {
                best = v;
            }
            n = (n + 1);
        }
        k = (k + 1);
    }
    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 @is_pandigital9(%arg0: i64) -> i1 {
    %0 = arith.constant 100000000 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi slt, %arg0, %2 : i64
    %3 = scf.if %1 -> (i1) {
      %4 = arith.constant true
      scf.yield %4 : i1
    } else {
      %5 = arith.constant 999999999 : i32
      %7 = arith.extsi %5 : i32 to i64
      %6 = arith.cmpi sgt, %arg0, %7 : i64
      scf.yield %6 : i1
    }
    cf.cond_br %3, ^bb0, ^bb1
    ^bb0:
      %8 = arith.constant 0 : i1
      func.return %8 : i1
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %9 = arith.constant 0 : i32
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i32 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i32, !llvm.ptr
    %12 = llvm.mlir.constant(1 : i64) : i64
    %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %13 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %14 = llvm.load %13 : !llvm.ptr -> i64
    %15 = arith.constant 0 : i32
    %17 = arith.extsi %15 : i32 to i64
    %16 = arith.cmpi sgt, %14, %17 : i64
    cf.cond_br %16, ^bb4, ^bb5
    ^bb4:
      %18 = llvm.load %13 : !llvm.ptr -> i64
      %19 = arith.constant 10 : i32
      %21 = arith.extsi %19 : i32 to i64
      %20 = arith.remsi %18, %21 : i64
      %22 = arith.trunci %20 : i64 to i32
      %23 = arith.constant 0 : i32
      %24 = arith.cmpi eq, %22, %23 : i32
      cf.cond_br %24, ^bb6, ^bb7
      ^bb6:
        %25 = arith.constant 0 : i1
        func.return %25 : i1
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %26 = arith.constant 1 : i32
      %27 = arith.shli %26, %22 : i32
      %28 = llvm.load %11 : !llvm.ptr -> i32
      %29 = arith.andi %28, %27 : i32
      %30 = arith.constant 0 : i32
      %31 = arith.cmpi ne, %29, %30 : i32
      cf.cond_br %31, ^bb9, ^bb10
      ^bb9:
        %32 = arith.constant 0 : i1
        func.return %32 : i1
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %33 = llvm.load %11 : !llvm.ptr -> i32
      %34 = arith.ori %33, %27 : i32
      llvm.store %34, %11 : i32, !llvm.ptr
      %35 = llvm.load %13 : !llvm.ptr -> i64
      %36 = arith.constant 10 : i32
      %38 = arith.extsi %36 : i32 to i64
      %37 = arith.divsi %35, %38 : i64
      llvm.store %37, %13 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %39 = llvm.load %11 : !llvm.ptr -> i32
    %40 = arith.constant 1022 : i32
    %41 = arith.cmpi eq, %39, %40 : i32
    func.return %41 : i1
  }
  func.func @concat_prod(%arg0: i64, %arg1: i64) -> i64 {
    %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 = llvm.load %49 : !llvm.ptr -> i64
    %51 = arith.cmpi sle, %50, %arg1 : i64
    cf.cond_br %51, ^bb13, ^bb14
    ^bb13:
      %52 = llvm.load %49 : !llvm.ptr -> i64
      %53 = arith.muli %arg0, %52 : i64
      %54 = llvm.mlir.constant(1 : i64) : i64
      %55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
      llvm.store %53, %55 : i64, !llvm.ptr
      %56 = llvm.load %55 : !llvm.ptr -> i64
      %57 = llvm.mlir.constant(1 : i64) : i64
      %58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
      llvm.store %56, %58 : i64, !llvm.ptr
      %59 = arith.constant 1 : i32
      %60 = arith.extsi %59 : i32 to i64
      %61 = llvm.mlir.constant(1 : i64) : i64
      %62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
      llvm.store %60, %62 : i64, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %63 = llvm.load %58 : !llvm.ptr -> i64
      %64 = arith.constant 0 : i32
      %66 = arith.extsi %64 : i32 to i64
      %65 = arith.cmpi sgt, %63, %66 : i64
      cf.cond_br %65, ^bb16, ^bb17
      ^bb16:
        %67 = llvm.load %62 : !llvm.ptr -> i64
        %68 = arith.constant 10 : i32
        %70 = arith.extsi %68 : i32 to i64
        %69 = arith.muli %67, %70 : i64
        llvm.store %69, %62 : i64, !llvm.ptr
        %71 = llvm.load %58 : !llvm.ptr -> i64
        %72 = arith.constant 10 : i32
        %74 = arith.extsi %72 : i32 to i64
        %73 = arith.divsi %71, %74 : i64
        llvm.store %73, %58 : i64, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %75 = llvm.load %45 : !llvm.ptr -> i64
      %76 = llvm.load %62 : !llvm.ptr -> i64
      %77 = arith.muli %75, %76 : i64
      %78 = llvm.load %55 : !llvm.ptr -> i64
      %79 = arith.addi %77, %78 : i64
      llvm.store %79, %45 : i64, !llvm.ptr
      %80 = llvm.load %45 : !llvm.ptr -> i64
      %81 = arith.constant 999999999 : i32
      %83 = arith.extsi %81 : i32 to i64
      %82 = arith.cmpi sgt, %80, %83 : i64
      cf.cond_br %82, ^bb18, ^bb19
      ^bb18:
        %84 = arith.constant 1 : i32
        %86 = arith.constant 0 : i32
        %85 = arith.subi %86, %84 : i32
        %87 = arith.extsi %85 : i32 to i64
        func.return %87 : i64
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %88 = llvm.load %49 : !llvm.ptr -> i64
      %89 = arith.constant 1 : i32
      %91 = arith.extsi %89 : i32 to i64
      %90 = arith.addi %88, %91 : i64
      llvm.store %90, %49 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %92 = llvm.load %45 : !llvm.ptr -> i64
    func.return %92 : i64
  }
  func.func @main() -> i32 {
    %93 = arith.constant 0 : i32
    %94 = arith.extsi %93 : i32 to i64
    %95 = llvm.mlir.constant(1 : i64) : i64
    %96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
    llvm.store %94, %96 : i64, !llvm.ptr
    %97 = arith.constant 1 : i32
    %98 = arith.extsi %97 : i32 to i64
    %99 = llvm.mlir.constant(1 : i64) : i64
    %100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
    llvm.store %98, %100 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %101 = llvm.load %100 : !llvm.ptr -> i64
    %102 = arith.constant 10000 : i32
    %104 = arith.extsi %102 : i32 to i64
    %103 = arith.cmpi slt, %101, %104 : i64
    cf.cond_br %103, ^bb22, ^bb23
    ^bb22:
      %105 = arith.constant 2 : i32
      %106 = arith.extsi %105 : i32 to i64
      %107 = llvm.mlir.constant(1 : i64) : i64
      %108 = llvm.alloca %107 x i64 : (i64) -> !llvm.ptr
      llvm.store %106, %108 : i64, !llvm.ptr
      cf.br ^bb24
      ^bb24:
      %109 = llvm.load %108 : !llvm.ptr -> i64
      %110 = arith.constant 9 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.cmpi sle, %109, %112 : i64
      cf.cond_br %111, ^bb25, ^bb26
      ^bb25:
        %114 = llvm.load %100 : !llvm.ptr -> i64
        %115 = llvm.load %108 : !llvm.ptr -> i64
        %113 = func.call @concat_prod(%114, %115) : (i64, i64) -> i64
        %116 = arith.constant 0 : i32
        %118 = arith.extsi %116 : i32 to i64
        %117 = arith.cmpi slt, %113, %118 : i64
        cf.cond_br %117, ^bb27, ^bb28
        ^bb27:
          cf.br ^bb26
        ^bb28:
          cf.br ^bb29
        ^bb29:
        %119 = func.call @is_pandigital9(%113) : (i64) -> i1
        %120 = scf.if %119 -> (i1) {
          %121 = llvm.load %96 : !llvm.ptr -> i64
          %122 = arith.cmpi sgt, %113, %121 : i64
          scf.yield %122 : i1
        } else {
          %123 = arith.constant false
          scf.yield %123 : i1
        }
        cf.cond_br %120, ^bb30, ^bb31
        ^bb30:
          llvm.store %113, %96 : i64, !llvm.ptr
          cf.br ^bb32
        ^bb31:
          cf.br ^bb32
        ^bb32:
        %124 = llvm.load %108 : !llvm.ptr -> i64
        %125 = arith.constant 1 : i32
        %127 = arith.extsi %125 : i32 to i64
        %126 = arith.addi %124, %127 : i64
        llvm.store %126, %108 : i64, !llvm.ptr
        cf.br ^bb24
      ^bb26:
      %128 = llvm.load %100 : !llvm.ptr -> i64
      %129 = arith.constant 1 : i32
      %131 = arith.extsi %129 : i32 to i64
      %130 = arith.addi %128, %131 : i64
      llvm.store %130, %100 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %132 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %133 = llvm.load %96 : !llvm.ptr -> i64
    %134 = llvm.call @printf(%132, %133) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %135 = arith.constant 0 : i32
    func.return %135 : i32
  }
}