Problem 052

Smallest positive integer x such that 2x..6x are permutations of x.

Answer142857
Output142857
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log n)
Space complexityO(1)O(1)
ApproachFlow solutionPermutation check on multiples
VerdictOptimal

Flow source

# Project Euler 052
# Smallest positive integer x such that 2x..6x are permutations of x.

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

function same_digits(a: i64, b: i64, ca: ptr<i32>, cb: ptr<i32>) -> bool {
    let mut i: i32 = 0
    while i < 10 {
        ca[i] = 0
        cb[i] = 0
        i = i + 1
    }
    let mut x: i64 = a
    while x > 0 {
        ca[(x % 10) as i32] = ca[(x % 10) as i32] + 1
        x = x / 10
    }
    x = b
    while x > 0 {
        cb[(x % 10) as i32] = cb[(x % 10) as i32] + 1
        x = x / 10
    }
    i = 0
    while i < 10 {
        if ca[i] != cb[i] { return false }
        i = i + 1
    }
    return true
}

function main() -> i32 {
    let ca: ptr<i32> = calloc(10, 4)
    let cb: ptr<i32> = calloc(10, 4)
    if ca == null || cb == null { return 1 }
    let mut n: i64 = 1
    while true {
        if same_digits(n, 2 * n, ca, cb) && same_digits(n, 3 * n, ca, cb) && same_digits(n, 4 * n, ca, cb) && same_digits(n, 5 * n, ca, cb) && same_digits(n, 6 * n, ca, cb) {
            printf("%lld\n", n)
            free(ca)
            free(cb)
            return 0
        }
        n = n + 1
    }
    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 same_digits_i64_i64_ptr_i32_ptr_i32(int64_t a, int64_t b, int32_t* ca, int32_t* cb);
int32_t main(void);



bool same_digits_i64_i64_ptr_i32_ptr_i32(int64_t a, int64_t b, int32_t* ca, int32_t* cb) {
    int32_t i = 0;
    while (i < 10) {
        ca[i] = 0;
        cb[i] = 0;
        i = (i + 1);
    }
    int64_t x = a;
    while (x > 0) {
        ca[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] = (ca[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] + 1);
        x = FLOW_CHECKED_DIV((x), (10));
    }
    x = b;
    while (x > 0) {
        cb[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] = (cb[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] + 1);
        x = FLOW_CHECKED_DIV((x), (10));
    }
    i = 0;
    while (i < 10) {
        if (ca[i] != cb[i]) {
            return 0;
        }
        i = (i + 1);
    }
    return 1;
}

int32_t main(void) {
    int32_t* ca = (int32_t*)(calloc(10, 4));
    int32_t* cb = (int32_t*)(calloc(10, 4));
    if ((ca == NULL || cb == NULL)) {
        return 1;
    }
    int64_t n = 1;
    while (1) {
        if (((((same_digits_i64_i64_ptr_i32_ptr_i32(n, (2 * n), ca, cb) && same_digits_i64_i64_ptr_i32_ptr_i32(n, (3 * n), ca, cb)) && same_digits_i64_i64_ptr_i32_ptr_i32(n, (4 * n), ca, cb)) && same_digits_i64_i64_ptr_i32_ptr_i32(n, (5 * n), ca, cb)) && same_digits_i64_i64_ptr_i32_ptr_i32(n, (6 * n), ca, cb))) {
            printf("%lld\n", n);
            free(ca);
            free(cb);
            return 0;
        }
        n = (n + 1);
    }
    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 @same_digits(%arg0: i64, %arg1: i64, %arg2: !llvm.ptr, %arg3: !llvm.ptr) -> i1 {
    %0 = arith.constant 0 : i32
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i32 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %3 = llvm.load %2 : !llvm.ptr -> i32
    %4 = arith.constant 10 : i32
    %5 = arith.cmpi slt, %3, %4 : i32
    cf.cond_br %5, ^bb1, ^bb2
    ^bb1:
      %6 = arith.constant 0 : i32
      %7 = llvm.load %2 : !llvm.ptr -> i32
      %8 = arith.extsi %7 : i32 to i64
      %9 = llvm.getelementptr %arg2[%8] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %6, %9 : i32, !llvm.ptr
      %10 = arith.constant 0 : i32
      %11 = llvm.load %2 : !llvm.ptr -> i32
      %12 = arith.extsi %11 : i32 to i64
      %13 = llvm.getelementptr %arg3[%12] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %10, %13 : i32, !llvm.ptr
      %14 = llvm.load %2 : !llvm.ptr -> i32
      %15 = arith.constant 1 : i32
      %16 = arith.addi %14, %15 : i32
      llvm.store %16, %2 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %18 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %19 = llvm.load %18 : !llvm.ptr -> i64
    %20 = arith.constant 0 : i32
    %22 = arith.extsi %20 : i32 to i64
    %21 = arith.cmpi sgt, %19, %22 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %24 = llvm.load %18 : !llvm.ptr -> i64
      %25 = arith.constant 10 : i32
      %27 = arith.extsi %25 : i32 to i64
      %26 = arith.remsi %24, %27 : i64
      %28 = arith.trunci %26 : i64 to i32
      %29 = arith.extsi %28 : i32 to i64
      %30 = llvm.getelementptr %arg2[%29] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %23 = llvm.load %30 : !llvm.ptr -> i32
      %31 = arith.constant 1 : i32
      %32 = arith.addi %23, %31 : i32
      %33 = llvm.load %18 : !llvm.ptr -> i64
      %34 = arith.constant 10 : i32
      %36 = arith.extsi %34 : i32 to i64
      %35 = arith.remsi %33, %36 : i64
      %37 = arith.trunci %35 : i64 to i32
      %38 = arith.extsi %37 : i32 to i64
      %39 = llvm.getelementptr %arg2[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %32, %39 : i32, !llvm.ptr
      %40 = llvm.load %18 : !llvm.ptr -> i64
      %41 = arith.constant 10 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.divsi %40, %43 : i64
      llvm.store %42, %18 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    llvm.store %arg1, %18 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %44 = llvm.load %18 : !llvm.ptr -> i64
    %45 = arith.constant 0 : i32
    %47 = arith.extsi %45 : i32 to i64
    %46 = arith.cmpi sgt, %44, %47 : i64
    cf.cond_br %46, ^bb7, ^bb8
    ^bb7:
      %49 = llvm.load %18 : !llvm.ptr -> i64
      %50 = arith.constant 10 : i32
      %52 = arith.extsi %50 : i32 to i64
      %51 = arith.remsi %49, %52 : i64
      %53 = arith.trunci %51 : i64 to i32
      %54 = arith.extsi %53 : i32 to i64
      %55 = llvm.getelementptr %arg3[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %48 = llvm.load %55 : !llvm.ptr -> i32
      %56 = arith.constant 1 : i32
      %57 = arith.addi %48, %56 : i32
      %58 = llvm.load %18 : !llvm.ptr -> i64
      %59 = arith.constant 10 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.remsi %58, %61 : i64
      %62 = arith.trunci %60 : i64 to i32
      %63 = arith.extsi %62 : i32 to i64
      %64 = llvm.getelementptr %arg3[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %57, %64 : i32, !llvm.ptr
      %65 = llvm.load %18 : !llvm.ptr -> i64
      %66 = arith.constant 10 : i32
      %68 = arith.extsi %66 : i32 to i64
      %67 = arith.divsi %65, %68 : i64
      llvm.store %67, %18 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %69 = arith.constant 0 : i32
    llvm.store %69, %2 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %70 = llvm.load %2 : !llvm.ptr -> i32
    %71 = arith.constant 10 : i32
    %72 = arith.cmpi slt, %70, %71 : i32
    cf.cond_br %72, ^bb10, ^bb11
    ^bb10:
      %74 = llvm.load %2 : !llvm.ptr -> i32
      %75 = arith.extsi %74 : i32 to i64
      %76 = llvm.getelementptr %arg2[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %73 = llvm.load %76 : !llvm.ptr -> i32
      %78 = llvm.load %2 : !llvm.ptr -> i32
      %79 = arith.extsi %78 : i32 to i64
      %80 = llvm.getelementptr %arg3[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %77 = llvm.load %80 : !llvm.ptr -> i32
      %81 = arith.cmpi ne, %73, %77 : i32
      cf.cond_br %81, ^bb12, ^bb13
      ^bb12:
        %82 = arith.constant 0 : i1
        func.return %82 : i1
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %83 = llvm.load %2 : !llvm.ptr -> i32
      %84 = arith.constant 1 : i32
      %85 = arith.addi %83, %84 : i32
      llvm.store %85, %2 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %86 = arith.constant 1 : i1
    func.return %86 : i1
  }
  func.func @main() -> i32 {
    %88 = arith.constant 10 : i32
    %89 = arith.constant 4 : i32
    %90 = arith.extsi %88 : i32 to i64
    %91 = arith.extsi %89 : i32 to i64
    %87 = func.call @calloc(%90, %91) : (i64, i64) -> !llvm.ptr
    %93 = arith.constant 10 : i32
    %94 = arith.constant 4 : i32
    %95 = arith.extsi %93 : i32 to i64
    %96 = arith.extsi %94 : i32 to i64
    %92 = func.call @calloc(%95, %96) : (i64, i64) -> !llvm.ptr
    %97 = llvm.mlir.zero : !llvm.ptr
    %98 = llvm.icmp "eq" %87, %97 : !llvm.ptr
    %99 = scf.if %98 -> (i1) {
      %100 = arith.constant true
      scf.yield %100 : i1
    } else {
      %101 = llvm.mlir.zero : !llvm.ptr
      %102 = llvm.icmp "eq" %92, %101 : !llvm.ptr
      scf.yield %102 : i1
    }
    cf.cond_br %99, ^bb15, ^bb16
    ^bb15:
      %103 = arith.constant 1 : i32
      func.return %103 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %104 = arith.constant 1 : i32
    %105 = arith.extsi %104 : i32 to i64
    %106 = llvm.mlir.constant(1 : i64) : i64
    %107 = llvm.alloca %106 x i64 : (i64) -> !llvm.ptr
    llvm.store %105, %107 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %108 = arith.constant 1 : i1
    cf.cond_br %108, ^bb19, ^bb20
    ^bb19:
      %110 = llvm.load %107 : !llvm.ptr -> i64
      %111 = arith.constant 2 : i32
      %112 = llvm.load %107 : !llvm.ptr -> i64
      %114 = arith.extsi %111 : i32 to i64
      %113 = arith.muli %114, %112 : i64
      %109 = func.call @same_digits(%110, %113, %87, %92) : (i64, i64, !llvm.ptr, !llvm.ptr) -> i1
      %115 = scf.if %109 -> (i1) {
        %117 = llvm.load %107 : !llvm.ptr -> i64
        %118 = arith.constant 3 : i32
        %119 = llvm.load %107 : !llvm.ptr -> i64
        %121 = arith.extsi %118 : i32 to i64
        %120 = arith.muli %121, %119 : i64
        %116 = func.call @same_digits(%117, %120, %87, %92) : (i64, i64, !llvm.ptr, !llvm.ptr) -> i1
        scf.yield %116 : i1
      } else {
        %122 = arith.constant false
        scf.yield %122 : i1
      }
      %123 = scf.if %115 -> (i1) {
        %125 = llvm.load %107 : !llvm.ptr -> i64
        %126 = arith.constant 4 : i32
        %127 = llvm.load %107 : !llvm.ptr -> i64
        %129 = arith.extsi %126 : i32 to i64
        %128 = arith.muli %129, %127 : i64
        %124 = func.call @same_digits(%125, %128, %87, %92) : (i64, i64, !llvm.ptr, !llvm.ptr) -> i1
        scf.yield %124 : i1
      } else {
        %130 = arith.constant false
        scf.yield %130 : i1
      }
      %131 = scf.if %123 -> (i1) {
        %133 = llvm.load %107 : !llvm.ptr -> i64
        %134 = arith.constant 5 : i32
        %135 = llvm.load %107 : !llvm.ptr -> i64
        %137 = arith.extsi %134 : i32 to i64
        %136 = arith.muli %137, %135 : i64
        %132 = func.call @same_digits(%133, %136, %87, %92) : (i64, i64, !llvm.ptr, !llvm.ptr) -> i1
        scf.yield %132 : i1
      } else {
        %138 = arith.constant false
        scf.yield %138 : i1
      }
      %139 = scf.if %131 -> (i1) {
        %141 = llvm.load %107 : !llvm.ptr -> i64
        %142 = arith.constant 6 : i32
        %143 = llvm.load %107 : !llvm.ptr -> i64
        %145 = arith.extsi %142 : i32 to i64
        %144 = arith.muli %145, %143 : i64
        %140 = func.call @same_digits(%141, %144, %87, %92) : (i64, i64, !llvm.ptr, !llvm.ptr) -> i1
        scf.yield %140 : i1
      } else {
        %146 = arith.constant false
        scf.yield %146 : i1
      }
      cf.cond_br %139, ^bb21, ^bb22
      ^bb21:
        %147 = llvm.mlir.addressof @str_0 : !llvm.ptr
        %148 = llvm.load %107 : !llvm.ptr -> i64
        %149 = llvm.call @printf(%147, %148) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
        func.call @free(%87) : (!llvm.ptr) -> ()
        func.call @free(%92) : (!llvm.ptr) -> ()
        %152 = arith.constant 0 : i32
        func.return %152 : i32
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %153 = llvm.load %107 : !llvm.ptr -> i64
      %154 = arith.constant 1 : i32
      %156 = arith.extsi %154 : i32 to i64
      %155 = arith.addi %153, %156 : i64
      llvm.store %155, %107 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %157 = arith.constant 0 : i32
    func.return %157 : i32
  }
}