Problem 206

Concealed Square: find n where n^2 = 1_2_3_4_5_6_7_8_9_0.

Answer1389019170
Output1389019170
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)
Space complexityO(1)O(1)
ApproachFlow solutionConcealed square search
VerdictOptimal

Flow source

# Project Euler 206
# Concealed Square: find n where n^2 = 1_2_3_4_5_6_7_8_9_0.

function form_ok(x: i64) -> bool {
    let mut square: i64 = x * x
    if square % 10 != 0 { return false }
    square = square / 100
    let mut want: i64 = 9
    while want >= 1 {
        if square % 10 != want { return false }
        square = square / 100
        want = want - 1
    }
    return true
}

function main() -> i32 {
    let mut x: i64 = 1389026620
    while x >= 1010101010 {
        if form_ok(x) {
            printf("%lld\n", x)
            return 0
        }
        x = x - 10
    }
    return 1
}

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 form_ok_i64(int64_t x);
int32_t main(void);

bool form_ok_i64(int64_t x) {
    int64_t square = (x * x);
    if (FLOW_CHECKED_MOD((square), (10)) != 0) {
        return 0;
    }
    square = FLOW_CHECKED_DIV((square), (100));
    int64_t want = 9;
    while (want >= 1) {
        if (FLOW_CHECKED_MOD((square), (10)) != want) {
            return 0;
        }
        square = FLOW_CHECKED_DIV((square), (100));
        want = (want - 1);
    }
    return 1;
}

int32_t main(void) {
    int64_t x = 1389026620;
    while (x >= 1010101010) {
        if (form_ok_i64(x)) {
            printf("%lld\n", x);
            return 0;
        }
        x = (x - 10);
    }
    return 1;
}

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 @form_ok(%arg0: i64) -> i1 {
    %0 = arith.muli %arg0, %arg0 : i64
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i64, !llvm.ptr
    %3 = llvm.load %2 : !llvm.ptr -> i64
    %4 = arith.constant 10 : i32
    %6 = arith.extsi %4 : i32 to i64
    %5 = arith.remsi %3, %6 : i64
    %7 = arith.constant 0 : i32
    %9 = arith.extsi %7 : i32 to i64
    %8 = arith.cmpi ne, %5, %9 : i64
    cf.cond_br %8, ^bb0, ^bb1
    ^bb0:
      %10 = arith.constant 0 : i1
      func.return %10 : i1
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %11 = llvm.load %2 : !llvm.ptr -> i64
    %12 = arith.constant 100 : i32
    %14 = arith.extsi %12 : i32 to i64
    %13 = arith.divsi %11, %14 : i64
    llvm.store %13, %2 : i64, !llvm.ptr
    %15 = arith.constant 9 : i32
    %16 = arith.extsi %15 : i32 to i64
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %16, %18 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %19 = llvm.load %18 : !llvm.ptr -> i64
    %20 = arith.constant 1 : i32
    %22 = arith.extsi %20 : i32 to i64
    %21 = arith.cmpi sge, %19, %22 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %23 = llvm.load %2 : !llvm.ptr -> i64
      %24 = arith.constant 10 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.remsi %23, %26 : i64
      %27 = llvm.load %18 : !llvm.ptr -> i64
      %28 = arith.cmpi ne, %25, %27 : i64
      cf.cond_br %28, ^bb6, ^bb7
      ^bb6:
        %29 = arith.constant 0 : i1
        func.return %29 : i1
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %30 = llvm.load %2 : !llvm.ptr -> i64
      %31 = arith.constant 100 : i32
      %33 = arith.extsi %31 : i32 to i64
      %32 = arith.divsi %30, %33 : i64
      llvm.store %32, %2 : i64, !llvm.ptr
      %34 = llvm.load %18 : !llvm.ptr -> i64
      %35 = arith.constant 1 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.subi %34, %37 : i64
      llvm.store %36, %18 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %38 = arith.constant 1 : i1
    func.return %38 : i1
  }
  func.func @main() -> i32 {
    %39 = arith.constant 1389026620 : i32
    %40 = arith.extsi %39 : i32 to i64
    %41 = llvm.mlir.constant(1 : i64) : i64
    %42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
    llvm.store %40, %42 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %43 = llvm.load %42 : !llvm.ptr -> i64
    %44 = arith.constant 1010101010 : i32
    %46 = arith.extsi %44 : i32 to i64
    %45 = arith.cmpi sge, %43, %46 : i64
    cf.cond_br %45, ^bb10, ^bb11
    ^bb10:
      %48 = llvm.load %42 : !llvm.ptr -> i64
      %47 = func.call @form_ok(%48) : (i64) -> i1
      cf.cond_br %47, ^bb12, ^bb13
      ^bb12:
        %49 = llvm.mlir.addressof @str_0 : !llvm.ptr
        %50 = llvm.load %42 : !llvm.ptr -> i64
        %51 = llvm.call @printf(%49, %50) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
        %52 = arith.constant 0 : i32
        func.return %52 : i32
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %53 = llvm.load %42 : !llvm.ptr -> i64
      %54 = arith.constant 10 : i32
      %56 = arith.extsi %54 : i32 to i64
      %55 = arith.subi %53, %56 : i64
      llvm.store %55, %42 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %57 = arith.constant 1 : i32
    func.return %57 : i32
  }
}