Problem 135

Count n < 10^6 with exactly ten solutions of x^2-y^2-z^2 = n (AP).

Answer4989
Output4989
StatusPASS
Native helperno
Runtime0 ms
Peak memory5024 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve or enumeration
VerdictSuboptimal

Flow source

# Project Euler 135
# Count n < 10^6 with exactly ten solutions of x^2-y^2-z^2 = n (AP).

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

function main() -> i32 {
    let limit: i64 = 1000000
    let sol: ptr<i32> = calloc(limit + 1, 4)
    if sol == null { return 1 }

    let mut a: i64 = 1
    while a <= limit {
        let mut b: i64 = (a + 3) / 4
        while b < a {
            let current: i64 = a * (4 * b - a)
            if current > limit { break }
            if current > 0 {
                sol[current] = sol[current] + 1
            }
            b = b + 1
        }
        a = a + 1
    }

    let mut count: i64 = 0
    let mut n: i64 = 1
    while n < limit {
        if sol[n] == 10 { count = count + 1 }
        n = n + 1
    }
    printf("%lld\n", count)
    free(sol)
    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; }

int32_t main(void);



int32_t main(void) {
    int64_t limit = 1000000;
    int32_t* sol = (int32_t*)(calloc((limit + 1), 4));
    if (sol == NULL) {
        return 1;
    }
    int64_t a = 1;
    while (a <= limit) {
        int64_t b = FLOW_CHECKED_DIV(((a + 3)), (4));
        while (b < a) {
            int64_t current = (a * ((4 * b) - a));
            if (current > limit) {
                break;
            }
            if (current > 0) {
                sol[current] = (sol[current] + 1);
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    int64_t count = 0;
    int64_t n = 1;
    while (n < limit) {
        if (sol[n] == 10) {
            count = (count + 1);
        }
        n = (n + 1);
    }
    printf("%lld\n", count);
    free(sol);
    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 @main() -> i32 {
    %0 = arith.constant 1000000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 1 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.addi %1, %5 : i64
    %6 = arith.constant 4 : i32
    %7 = arith.extsi %6 : i32 to i64
    %2 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
    %8 = llvm.mlir.zero : !llvm.ptr
    %9 = llvm.icmp "eq" %2, %8 : !llvm.ptr
    cf.cond_br %9, ^bb0, ^bb1
    ^bb0:
      %10 = arith.constant 1 : i32
      func.return %10 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %11 = arith.constant 1 : i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.cmpi sle, %15, %1 : i64
    cf.cond_br %16, ^bb4, ^bb5
    ^bb4:
      %17 = llvm.load %14 : !llvm.ptr -> i64
      %18 = arith.constant 3 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.addi %17, %20 : i64
      %21 = arith.constant 4 : i32
      %23 = arith.extsi %21 : i32 to i64
      %22 = arith.divsi %19, %23 : i64
      %24 = llvm.mlir.constant(1 : i64) : i64
      %25 = llvm.alloca %24 x i64 : (i64) -> !llvm.ptr
      llvm.store %22, %25 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %26 = llvm.load %25 : !llvm.ptr -> i64
      %27 = llvm.load %14 : !llvm.ptr -> i64
      %28 = arith.cmpi slt, %26, %27 : i64
      cf.cond_br %28, ^bb7, ^bb8
      ^bb7:
        %29 = llvm.load %14 : !llvm.ptr -> i64
        %30 = arith.constant 4 : i32
        %31 = llvm.load %25 : !llvm.ptr -> i64
        %33 = arith.extsi %30 : i32 to i64
        %32 = arith.muli %33, %31 : i64
        %34 = llvm.load %14 : !llvm.ptr -> i64
        %35 = arith.subi %32, %34 : i64
        %36 = arith.muli %29, %35 : i64
        %37 = arith.cmpi sgt, %36, %1 : i64
        cf.cond_br %37, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %38 = arith.constant 0 : i32
        %40 = arith.extsi %38 : i32 to i64
        %39 = arith.cmpi sgt, %36, %40 : i64
        cf.cond_br %39, ^bb12, ^bb13
        ^bb12:
          %42 = llvm.getelementptr %2[%36] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %41 = llvm.load %42 : !llvm.ptr -> i32
          %43 = arith.constant 1 : i32
          %44 = arith.addi %41, %43 : i32
          %45 = llvm.getelementptr %2[%36] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %44, %45 : i32, !llvm.ptr
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        %46 = llvm.load %25 : !llvm.ptr -> i64
        %47 = arith.constant 1 : i32
        %49 = arith.extsi %47 : i32 to i64
        %48 = arith.addi %46, %49 : i64
        llvm.store %48, %25 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %50 = llvm.load %14 : !llvm.ptr -> i64
      %51 = arith.constant 1 : i32
      %53 = arith.extsi %51 : i32 to i64
      %52 = arith.addi %50, %53 : i64
      llvm.store %52, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %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 1 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %62 = llvm.load %61 : !llvm.ptr -> i64
    %63 = arith.cmpi slt, %62, %1 : i64
    cf.cond_br %63, ^bb16, ^bb17
    ^bb16:
      %65 = llvm.load %61 : !llvm.ptr -> i64
      %66 = llvm.getelementptr %2[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %64 = llvm.load %66 : !llvm.ptr -> i32
      %67 = arith.constant 10 : i32
      %68 = arith.cmpi eq, %64, %67 : i32
      cf.cond_br %68, ^bb18, ^bb19
      ^bb18:
        %69 = llvm.load %57 : !llvm.ptr -> i64
        %70 = arith.constant 1 : i32
        %72 = arith.extsi %70 : i32 to i64
        %71 = arith.addi %69, %72 : i64
        llvm.store %71, %57 : i64, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %73 = llvm.load %61 : !llvm.ptr -> i64
      %74 = arith.constant 1 : i32
      %76 = arith.extsi %74 : i32 to i64
      %75 = arith.addi %73, %76 : i64
      llvm.store %75, %61 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %77 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %78 = llvm.load %57 : !llvm.ptr -> i64
    %79 = llvm.call @printf(%77, %78) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %81 = arith.constant 0 : i32
    func.return %81 : i32
  }
}