Problem 804

Counting Binary Quadratic Representations — T(10^16).

Answer4921370551019052
Output4921370551019052
StatusPASS
Native helperno
Runtime1600 ms
Peak memory1104 KB
Time complexityO(1) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 804
# Counting Binary Quadratic Representations — T(10^16).

function isqrt(n: i64) -> i64 {
    if n < 2 { return n }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function T(N: i64) -> i64 {
    if N <= 0 { return 0 }
    let A: i64 = 4 * N
    let B: i64 = 163
    let ymax: i64 = isqrt(A / B)
    let t0: i64 = isqrt(A)
    let mut total: i64 = t0 + 1 - (t0 & 1)
    let mut By2: i64 = 0
    let mut Bdelta: i64 = B
    let twoB: i64 = 2 * B
    let mut y: i64 = 1
    while y <= ymax {
        By2 = By2 + Bdelta
        Bdelta = Bdelta + twoB
        let t: i64 = isqrt(A - By2)
        let mut c: i64 = 0
        if (y & 1) != 0 {
            c = t + (t & 1)
        } else {
            c = t + 1 - (t & 1)
        }
        total = total + 2 * c
        y = y + 1
    }
    return total - 1
}

function main() -> i32 {
    printf("%lld\n", T(10000000000000000))
    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 T_i64(int64_t N);
int32_t main(void);

int64_t isqrt_i64(int64_t n) {
    if (n < 2) {
        return n;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

int64_t T_i64(int64_t N) {
    if (N <= 0) {
        return 0;
    }
    int64_t A = (4 * N);
    int64_t B = 163;
    int64_t ymax = isqrt_i64(FLOW_CHECKED_DIV((A), (B)));
    int64_t t0 = isqrt_i64(A);
    int64_t total = ((t0 + 1) - (t0 & 1));
    int64_t By2 = 0;
    int64_t Bdelta = B;
    int64_t twoB = (2 * B);
    int64_t y = 1;
    while (y <= ymax) {
        By2 = (By2 + Bdelta);
        Bdelta = (Bdelta + twoB);
        int64_t t = isqrt_i64((A - By2));
        int64_t c = 0;
        if ((y & 1) != 0) {
            c = (t + (t & 1));
        } else {
            c = ((t + 1) - (t & 1));
        }
        total = (total + (2 * c));
        y = (y + 1);
    }
    return (total - 1);
}

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