Problem 024

Millionth lexicographic permutation of the digits 0..9.

Answer2783915460
Output2783915460
StatusPASS
Native helperno
Runtime0 ms
Peak memory1120 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 solutionFactorial number system
VerdictSuboptimal

Flow source

# Project Euler 024
# Millionth lexicographic permutation of the digits 0..9.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function factorial(n: i64) -> i64 {
    let mut r: i64 = 1
    for i in 2..(n + 1) {
        r = r * i
    }
    return r
}

function main() -> i32 {
    let digits: ptr<i32> = calloc(10, 4)
    for i in 0..10 {
        digits[i] = i
    }

    let mut remaining: i64 = 999999  # zero-based index of the millionth
    let mut result: i64 = 0
    let mut n: i32 = 10
    while n > 0 {
        let f: i64 = factorial((n - 1) as i64)
        let idx: i32 = (remaining / f) as i32
        remaining = remaining % f
        result = result * 10 + (digits[idx] as i64)

        # remove used digit
        for j in idx..(n - 1) {
            digits[j] = digits[j + 1]
        }
        n = n - 1
    }

    printf("%lld\n", result)
    free(digits)
    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 factorial_i64(int64_t n);
int32_t main(void);



int64_t factorial_i64(int64_t n) {
    int64_t r = 1;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 2; (2 <= (n + 1)) ? i < (n + 1) : i > (n + 1); i += (2 <= (n + 1)) ? 1 : -1) {
        r = (r * i);
    }
    return r;
}

int32_t main(void) {
    int32_t* digits = (int32_t*)(calloc(10, 4));
    int32_t __flow_step_2 = 1;
    for (int32_t i = 0; (0 <= 10) ? i < 10 : i > 10; i += (0 <= 10) ? 1 : -1) {
        digits[i] = i;
    }
    int64_t remaining = 999999;
    int64_t result = 0;
    int32_t n = 10;
    while (n > 0) {
        int64_t f = factorial_i64(((int64_t)((n - 1))));
        int32_t idx = ((int32_t)(FLOW_CHECKED_DIV((remaining), (f))));
        remaining = FLOW_CHECKED_MOD((remaining), (f));
        result = ((result * 10) + ((int64_t)(digits[idx])));
        int32_t __flow_step_3 = 1;
        for (int32_t j = idx; (idx <= (n - 1)) ? j < (n - 1) : j > (n - 1); j += (idx <= (n - 1)) ? 1 : -1) {
            digits[j] = digits[(j + 1)];
        }
        n = (n - 1);
    }
    printf("%lld\n", result);
    free(digits);
    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 @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @factorial(%arg0: i64) -> i64 {
    %0 = arith.constant 1 : 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 2 : i32
    %5 = arith.constant 1 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.addi %arg0, %7 : i64
    %8 = arith.index_cast %4 : i32 to index
    %9 = arith.index_cast %6 : i32 to index
    %11 = arith.constant 1 : index
    %12 = arith.constant -1 : index
    %13 = arith.cmpi sle, %8, %9 : index
    %10 = arith.select %13, %11, %12 : index
    cf.br ^bb0(%8 : index)
    ^bb0(%14: index):
    %15 = arith.cmpi slt, %14, %9 : index
    %16 = arith.cmpi sgt, %14, %9 : index
    %17 = arith.select %13, %15, %16 : i1
    cf.cond_br %17, ^bb1(%14 : index), ^bb2(%14 : index)
    ^bb1(%18: index):
      %19 = llvm.load %3 : !llvm.ptr -> i64
      %21 = arith.trunci %19 : i64 to i32
      %22 = arith.index_cast %18 : index to i32
      %20 = arith.muli %21, %22 : i32
      %23 = arith.extsi %20 : i32 to i64
      llvm.store %23, %3 : i64, !llvm.ptr
      %24 = arith.addi %18, %10 : index
      cf.br ^bb0(%24 : index)
    ^bb2(%25: index):
    %26 = llvm.load %3 : !llvm.ptr -> i64
    func.return %26 : i64
  }
  func.func @main() -> i32 {
    %28 = arith.constant 10 : i32
    %29 = arith.constant 4 : i32
    %30 = arith.extsi %28 : i32 to i64
    %31 = arith.extsi %29 : i32 to i64
    %27 = func.call @calloc(%30, %31) : (i64, i64) -> !llvm.ptr
    %32 = arith.constant 0 : i32
    %33 = arith.constant 10 : i32
    %34 = arith.index_cast %32 : i32 to index
    %35 = arith.index_cast %33 : i32 to index
    %37 = arith.constant 1 : index
    %38 = arith.constant -1 : index
    %39 = arith.cmpi sle, %34, %35 : index
    %36 = arith.select %39, %37, %38 : index
    cf.br ^bb3(%34 : index)
    ^bb3(%40: index):
    %41 = arith.cmpi slt, %40, %35 : index
    %42 = arith.cmpi sgt, %40, %35 : index
    %43 = arith.select %39, %41, %42 : i1
    cf.cond_br %43, ^bb4(%40 : index), ^bb5(%40 : index)
    ^bb4(%44: index):
      %45 = arith.index_cast %44 : index to i32
      %46 = arith.index_cast %44 : index to i64
      %47 = llvm.getelementptr %27[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %45, %47 : i32, !llvm.ptr
      %48 = arith.addi %44, %36 : index
      cf.br ^bb3(%48 : index)
    ^bb5(%49: index):
    %50 = arith.constant 999999 : i32
    %51 = arith.extsi %50 : i32 to i64
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
    llvm.store %51, %53 : i64, !llvm.ptr
    %54 = arith.constant 0 : i32
    %55 = arith.extsi %54 : i32 to i64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i64, !llvm.ptr
    %58 = arith.constant 10 : i32
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i32 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %61 = llvm.load %60 : !llvm.ptr -> i32
    %62 = arith.constant 0 : i32
    %63 = arith.cmpi sgt, %61, %62 : i32
    cf.cond_br %63, ^bb7, ^bb8
    ^bb7:
      %65 = llvm.load %60 : !llvm.ptr -> i32
      %66 = arith.constant 1 : i32
      %67 = arith.subi %65, %66 : i32
      %68 = arith.extsi %67 : i32 to i64
      %64 = func.call @factorial(%68) : (i64) -> i64
      %69 = llvm.load %53 : !llvm.ptr -> i64
      %70 = arith.divsi %69, %64 : i64
      %71 = arith.trunci %70 : i64 to i32
      %72 = llvm.load %53 : !llvm.ptr -> i64
      %73 = arith.remsi %72, %64 : i64
      llvm.store %73, %53 : i64, !llvm.ptr
      %74 = llvm.load %57 : !llvm.ptr -> i64
      %75 = arith.constant 10 : i32
      %77 = arith.extsi %75 : i32 to i64
      %76 = arith.muli %74, %77 : i64
      %79 = arith.extsi %71 : i32 to i64
      %80 = llvm.getelementptr %27[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %78 = llvm.load %80 : !llvm.ptr -> i32
      %81 = arith.extsi %78 : i32 to i64
      %82 = arith.addi %76, %81 : i64
      llvm.store %82, %57 : i64, !llvm.ptr
      %83 = llvm.load %60 : !llvm.ptr -> i32
      %84 = arith.constant 1 : i32
      %85 = arith.subi %83, %84 : i32
      %86 = arith.index_cast %71 : i32 to index
      %87 = arith.index_cast %85 : i32 to index
      %89 = arith.constant 1 : index
      %90 = arith.constant -1 : index
      %91 = arith.cmpi sle, %86, %87 : index
      %88 = arith.select %91, %89, %90 : index
      cf.br ^bb9(%86 : index)
      ^bb9(%92: index):
      %93 = arith.cmpi slt, %92, %87 : index
      %94 = arith.cmpi sgt, %92, %87 : index
      %95 = arith.select %91, %93, %94 : i1
      cf.cond_br %95, ^bb10(%92 : index), ^bb11(%92 : index)
      ^bb10(%96: index):
        %98 = arith.constant 1 : i32
        %100 = arith.index_cast %96 : index to i32
        %99 = arith.addi %100, %98 : i32
        %101 = arith.extsi %99 : i32 to i64
        %102 = llvm.getelementptr %27[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %97 = llvm.load %102 : !llvm.ptr -> i32
        %103 = arith.index_cast %96 : index to i64
        %104 = llvm.getelementptr %27[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %97, %104 : i32, !llvm.ptr
        %105 = arith.addi %96, %88 : index
        cf.br ^bb9(%105 : index)
      ^bb11(%106: index):
      %107 = llvm.load %60 : !llvm.ptr -> i32
      %108 = arith.constant 1 : i32
      %109 = arith.subi %107, %108 : i32
      llvm.store %109, %60 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %110 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %111 = llvm.load %57 : !llvm.ptr -> i64
    %112 = llvm.call @printf(%110, %111) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%27) : (!llvm.ptr) -> ()
    %114 = arith.constant 0 : i32
    func.return %114 : i32
  }
}