Problem 932

2025-numbers: n = (a+b)^2 with decimal concatenation ab. Sum all such n with at most 16 digits.

Answer72673459417881349
Output72673459417881349
StatusPASS
Native helperno
Runtime620 ms
Peak memory1104 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(1)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 932
# 2025-numbers: n = (a+b)^2 with decimal concatenation ab.
# Sum all such n with at most 16 digits.

extern {
    function sqrt(x: f64) -> f64
}

function isqrt(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let mut x: i64 = sqrt(n as f64) as i64
    while (x + 1) * (x + 1) <= n { x = x + 1 }
    while x > 0 && x * x > n { x = x - 1 }
    return x
}

function digit_len(b: i64) -> i64 {
    let mut n: i64 = 1
    let mut p: i64 = 10
    while b >= p {
        n = n + 1
        p = p * 10
    }
    return n
}

function compute(N: i64) -> i64 {
    let mut total: i64 = -1
    let mut lim: i64 = 1
    for i in 0..(N / 2) {
        lim = lim * 10
    }
    for b in 1..lim {
        let n: i64 = digit_len(b)
        let mut ten_n: i64 = 1
        for j in 0..n {
            ten_n = ten_n * 10
        }
        let v0: i64 = 4 * b * (1 - ten_n) + ten_n * ten_n
        if v0 > 0 {
            let v: i64 = isqrt(v0)
            if v * v == v0 {
                let a1: i64 = (ten_n - 2 * b + v) / 2
                total = total + a1 * ten_n + b
                let a2: i64 = (ten_n - 2 * b - v) / 2
                total = total + a2 * ten_n + b
            }
        }
    }
    return total
}

function main() -> i32 {
    printf("%lld\n", compute(16))
    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 isqrt_i64(int64_t n);
int64_t digit_len_i64(int64_t b);
int64_t compute_i64(int64_t N);
int32_t main(void);


int64_t isqrt_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int64_t x = ((int64_t)(sqrt(((double)(n)))));
    while (((x + 1) * (x + 1)) <= n) {
        x = (x + 1);
    }
    while ((x > 0 && (x * x) > n)) {
        x = (x - 1);
    }
    return x;
}

int64_t digit_len_i64(int64_t b) {
    int64_t n = 1;
    int64_t p = 10;
    while (b >= p) {
        n = (n + 1);
        p = (p * 10);
    }
    return n;
}

int64_t compute_i64(int64_t N) {
    int64_t total = (-1);
    int64_t lim = 1;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= FLOW_CHECKED_DIV((N), (2))) ? i < FLOW_CHECKED_DIV((N), (2)) : i > FLOW_CHECKED_DIV((N), (2)); i += (0 <= FLOW_CHECKED_DIV((N), (2))) ? 1 : -1) {
        lim = (lim * 10);
    }
    int32_t __flow_step_2 = 1;
    for (int32_t b = 1; (1 <= lim) ? b < lim : b > lim; b += (1 <= lim) ? 1 : -1) {
        int64_t n = digit_len_i64(b);
        int64_t ten_n = 1;
        int32_t __flow_step_3 = 1;
        for (int32_t j = 0; (0 <= n) ? j < n : j > n; j += (0 <= n) ? 1 : -1) {
            ten_n = (ten_n * 10);
        }
        int64_t v0 = (((4 * b) * (1 - ten_n)) + (ten_n * ten_n));
        if (v0 > 0) {
            int64_t v = isqrt_i64(v0);
            if ((v * v) == v0) {
                int64_t a1 = FLOW_CHECKED_DIV((((ten_n - (2 * b)) + v)), (2));
                total = ((total + (a1 * ten_n)) + b);
                int64_t a2 = FLOW_CHECKED_DIV((((ten_n - (2 * b)) - v)), (2));
                total = ((total + (a2 * ten_n)) + b);
            }
        }
    }
    return total;
}

int32_t main(void) {
    printf("%lld\n", compute_i64(16));
    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 @sqrt(f64) -> f64
  func.func @isqrt(%arg0: i64) -> i64 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 0 : i32
      %4 = arith.extsi %3 : i32 to i64
      func.return %4 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = arith.sitofp %arg0 : i64 to f64
    %6 = math.sqrt %5 : f64
    %7 = arith.fptosi %6 : f64 to i64
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
    llvm.store %7, %9 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %10 = llvm.load %9 : !llvm.ptr -> i64
    %11 = arith.constant 1 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.addi %10, %13 : i64
    %14 = llvm.load %9 : !llvm.ptr -> i64
    %15 = arith.constant 1 : i32
    %17 = arith.extsi %15 : i32 to i64
    %16 = arith.addi %14, %17 : i64
    %18 = arith.muli %12, %16 : i64
    %19 = arith.cmpi sle, %18, %arg0 : i64
    cf.cond_br %19, ^bb4, ^bb5
    ^bb4:
      %20 = llvm.load %9 : !llvm.ptr -> i64
      %21 = arith.constant 1 : i32
      %23 = arith.extsi %21 : i32 to i64
      %22 = arith.addi %20, %23 : i64
      llvm.store %22, %9 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    cf.br ^bb6
    ^bb6:
    %24 = llvm.load %9 : !llvm.ptr -> i64
    %25 = arith.constant 0 : i32
    %27 = arith.extsi %25 : i32 to i64
    %26 = arith.cmpi sgt, %24, %27 : i64
    %28 = scf.if %26 -> (i1) {
      %29 = llvm.load %9 : !llvm.ptr -> i64
      %30 = llvm.load %9 : !llvm.ptr -> i64
      %31 = arith.muli %29, %30 : i64
      %32 = arith.cmpi sgt, %31, %arg0 : i64
      scf.yield %32 : i1
    } else {
      %33 = arith.constant false
      scf.yield %33 : i1
    }
    cf.cond_br %28, ^bb7, ^bb8
    ^bb7:
      %34 = llvm.load %9 : !llvm.ptr -> i64
      %35 = arith.constant 1 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.subi %34, %37 : i64
      llvm.store %36, %9 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %38 = llvm.load %9 : !llvm.ptr -> i64
    func.return %38 : i64
  }
  func.func @digit_len(%arg0: i64) -> i64 {
    %39 = arith.constant 1 : 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
    %43 = arith.constant 10 : i32
    %44 = arith.extsi %43 : i32 to i64
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x i64 : (i64) -> !llvm.ptr
    llvm.store %44, %46 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %47 = llvm.load %46 : !llvm.ptr -> i64
    %48 = arith.cmpi sge, %arg0, %47 : i64
    cf.cond_br %48, ^bb10, ^bb11
    ^bb10:
      %49 = llvm.load %42 : !llvm.ptr -> i64
      %50 = arith.constant 1 : i32
      %52 = arith.extsi %50 : i32 to i64
      %51 = arith.addi %49, %52 : i64
      llvm.store %51, %42 : i64, !llvm.ptr
      %53 = llvm.load %46 : !llvm.ptr -> i64
      %54 = arith.constant 10 : i32
      %56 = arith.extsi %54 : i32 to i64
      %55 = arith.muli %53, %56 : i64
      llvm.store %55, %46 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %57 = llvm.load %42 : !llvm.ptr -> i64
    func.return %57 : i64
  }
  func.func @compute(%arg0: i64) -> i64 {
    %58 = arith.constant 1 : i32
    %60 = arith.constant 0 : i32
    %59 = arith.subi %60, %58 : i32
    %61 = arith.extsi %59 : i32 to i64
    %62 = llvm.mlir.constant(1 : i64) : i64
    %63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
    llvm.store %61, %63 : i64, !llvm.ptr
    %64 = arith.constant 1 : i32
    %65 = arith.extsi %64 : i32 to i64
    %66 = llvm.mlir.constant(1 : i64) : i64
    %67 = llvm.alloca %66 x i64 : (i64) -> !llvm.ptr
    llvm.store %65, %67 : i64, !llvm.ptr
    %68 = arith.constant 0 : i32
    %69 = arith.constant 2 : i32
    %71 = arith.extsi %69 : i32 to i64
    %70 = arith.divsi %arg0, %71 : i64
    %72 = arith.index_cast %68 : i32 to index
    %73 = arith.index_cast %70 : i32 to index
    %75 = arith.constant 1 : index
    %76 = arith.constant -1 : index
    %77 = arith.cmpi sle, %72, %73 : index
    %74 = arith.select %77, %75, %76 : index
    cf.br ^bb12(%72 : index)
    ^bb12(%78: index):
    %79 = arith.cmpi slt, %78, %73 : index
    %80 = arith.cmpi sgt, %78, %73 : index
    %81 = arith.select %77, %79, %80 : i1
    cf.cond_br %81, ^bb13(%78 : index), ^bb14(%78 : index)
    ^bb13(%82: index):
      %83 = llvm.load %67 : !llvm.ptr -> i64
      %84 = arith.constant 10 : i32
      %86 = arith.extsi %84 : i32 to i64
      %85 = arith.muli %83, %86 : i64
      llvm.store %85, %67 : i64, !llvm.ptr
      %87 = arith.addi %82, %74 : index
      cf.br ^bb12(%87 : index)
    ^bb14(%88: index):
    %89 = arith.constant 1 : i32
    %90 = llvm.load %67 : !llvm.ptr -> i64
    %91 = arith.index_cast %89 : i32 to index
    %92 = arith.index_cast %90 : i32 to index
    %94 = arith.constant 1 : index
    %95 = arith.constant -1 : index
    %96 = arith.cmpi sle, %91, %92 : index
    %93 = arith.select %96, %94, %95 : index
    cf.br ^bb15(%91 : index)
    ^bb15(%97: index):
    %98 = arith.cmpi slt, %97, %92 : index
    %99 = arith.cmpi sgt, %97, %92 : index
    %100 = arith.select %96, %98, %99 : i1
    cf.cond_br %100, ^bb16(%97 : index), ^bb17(%97 : index)
    ^bb16(%101: index):
      %103 = arith.index_cast %101 : index to i64
      %102 = func.call @digit_len(%103) : (i64) -> i64
      %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
      %108 = arith.constant 0 : i32
      %109 = arith.index_cast %108 : i32 to index
      %110 = arith.index_cast %102 : i32 to index
      %112 = arith.constant 1 : index
      %113 = arith.constant -1 : index
      %114 = arith.cmpi sle, %109, %110 : index
      %111 = arith.select %114, %112, %113 : index
      cf.br ^bb18(%109 : index)
      ^bb18(%115: index):
      %116 = arith.cmpi slt, %115, %110 : index
      %117 = arith.cmpi sgt, %115, %110 : index
      %118 = arith.select %114, %116, %117 : i1
      cf.cond_br %118, ^bb19(%115 : index), ^bb20(%115 : index)
      ^bb19(%119: index):
        %120 = llvm.load %107 : !llvm.ptr -> i64
        %121 = arith.constant 10 : i32
        %123 = arith.extsi %121 : i32 to i64
        %122 = arith.muli %120, %123 : i64
        llvm.store %122, %107 : i64, !llvm.ptr
        %124 = arith.addi %119, %111 : index
        cf.br ^bb18(%124 : index)
      ^bb20(%125: index):
      %126 = arith.constant 4 : i32
      %128 = arith.index_cast %101 : index to i32
      %127 = arith.muli %126, %128 : i32
      %129 = arith.constant 1 : i32
      %130 = llvm.load %107 : !llvm.ptr -> i64
      %132 = arith.extsi %129 : i32 to i64
      %131 = arith.subi %132, %130 : i64
      %134 = arith.extsi %127 : i32 to i64
      %133 = arith.muli %134, %131 : i64
      %135 = llvm.load %107 : !llvm.ptr -> i64
      %136 = llvm.load %107 : !llvm.ptr -> i64
      %137 = arith.muli %135, %136 : i64
      %138 = arith.addi %133, %137 : i64
      %139 = arith.constant 0 : i32
      %141 = arith.extsi %139 : i32 to i64
      %140 = arith.cmpi sgt, %138, %141 : i64
      cf.cond_br %140, ^bb21, ^bb22
      ^bb21:
        %142 = func.call @isqrt(%138) : (i64) -> i64
        %143 = arith.muli %142, %142 : i64
        %144 = arith.cmpi eq, %143, %138 : i64
        cf.cond_br %144, ^bb24, ^bb25
        ^bb24:
          %145 = llvm.load %107 : !llvm.ptr -> i64
          %146 = arith.constant 2 : i32
          %148 = arith.index_cast %101 : index to i32
          %147 = arith.muli %146, %148 : i32
          %150 = arith.extsi %147 : i32 to i64
          %149 = arith.subi %145, %150 : i64
          %151 = arith.addi %149, %142 : i64
          %152 = arith.constant 2 : i32
          %154 = arith.extsi %152 : i32 to i64
          %153 = arith.divsi %151, %154 : i64
          %155 = llvm.load %63 : !llvm.ptr -> i64
          %156 = llvm.load %107 : !llvm.ptr -> i64
          %157 = arith.muli %153, %156 : i64
          %158 = arith.addi %155, %157 : i64
          %160 = arith.trunci %158 : i64 to i32
          %161 = arith.index_cast %101 : index to i32
          %159 = arith.addi %160, %161 : i32
          %162 = arith.extsi %159 : i32 to i64
          llvm.store %162, %63 : i64, !llvm.ptr
          %163 = llvm.load %107 : !llvm.ptr -> i64
          %164 = arith.constant 2 : i32
          %166 = arith.index_cast %101 : index to i32
          %165 = arith.muli %164, %166 : i32
          %168 = arith.extsi %165 : i32 to i64
          %167 = arith.subi %163, %168 : i64
          %169 = arith.subi %167, %142 : i64
          %170 = arith.constant 2 : i32
          %172 = arith.extsi %170 : i32 to i64
          %171 = arith.divsi %169, %172 : i64
          %173 = llvm.load %63 : !llvm.ptr -> i64
          %174 = llvm.load %107 : !llvm.ptr -> i64
          %175 = arith.muli %171, %174 : i64
          %176 = arith.addi %173, %175 : i64
          %178 = arith.trunci %176 : i64 to i32
          %179 = arith.index_cast %101 : index to i32
          %177 = arith.addi %178, %179 : i32
          %180 = arith.extsi %177 : i32 to i64
          llvm.store %180, %63 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %181 = arith.addi %101, %93 : index
      cf.br ^bb15(%181 : index)
    ^bb17(%182: index):
    %183 = llvm.load %63 : !llvm.ptr -> i64
    func.return %183 : i64
  }
  func.func @main() -> i32 {
    %184 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %186 = arith.constant 16 : i32
    %187 = arith.extsi %186 : i32 to i64
    %185 = func.call @compute(%187) : (i64) -> i64
    %188 = llvm.call @printf(%184, %185) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %189 = arith.constant 0 : i32
    func.return %189 : i32
  }
}