Problem 136

Count n < 50e6 with exactly one solution of the PE135 equation.

Answer2544559
Output2544559
StatusPASS
Native helperno
Runtime1140 ms
Peak memory98752 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 136
# Count n < 50e6 with exactly one solution of the PE135 equation.

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

function main() -> i32 {
    let limit: i64 = 50000000
    let once: ptr<i8> = calloc(limit, 1)
    let many: ptr<i8> = calloc(limit, 1)
    if once == null || many == 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 {
                if once[current] == 1 {
                    many[current] = 1
                } else {
                    once[current] = 1
                }
            }
            b = b + 1
        }
        a = a + 1
    }

    let mut count: i64 = 0
    let mut n: i64 = 1
    while n < limit {
        if once[n] == 1 && many[n] == 0 { count = count + 1 }
        n = n + 1
    }
    printf("%lld\n", count)
    free(many)
    free(once)
    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 = 50000000;
    int8_t* once = (int8_t*)(calloc(limit, 1));
    int8_t* many = (int8_t*)(calloc(limit, 1));
    if ((once == NULL || many == 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) {
                if (once[current] == 1) {
                    many[current] = 1;
                } else {
                    once[current] = 1;
                }
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    int64_t count = 0;
    int64_t n = 1;
    while (n < limit) {
        if ((once[n] == 1 && many[n] == 0)) {
            count = (count + 1);
        }
        n = (n + 1);
    }
    printf("%lld\n", count);
    free(many);
    free(once);
    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 50000000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 1 : i32
    %4 = arith.extsi %3 : i32 to i64
    %2 = func.call @calloc(%1, %4) : (i64, i64) -> !llvm.ptr
    %6 = arith.constant 1 : i32
    %7 = arith.extsi %6 : i32 to i64
    %5 = func.call @calloc(%1, %7) : (i64, i64) -> !llvm.ptr
    %8 = llvm.mlir.zero : !llvm.ptr
    %9 = llvm.icmp "eq" %2, %8 : !llvm.ptr
    %10 = scf.if %9 -> (i1) {
      %11 = arith.constant true
      scf.yield %11 : i1
    } else {
      %12 = llvm.mlir.zero : !llvm.ptr
      %13 = llvm.icmp "eq" %5, %12 : !llvm.ptr
      scf.yield %13 : i1
    }
    cf.cond_br %10, ^bb0, ^bb1
    ^bb0:
      %14 = arith.constant 1 : i32
      func.return %14 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %15 = arith.constant 1 : 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.cmpi slt, %19, %1 : i64
    cf.cond_br %20, ^bb4, ^bb5
    ^bb4:
      %21 = llvm.load %18 : !llvm.ptr -> i64
      %22 = arith.constant 3 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.addi %21, %24 : i64
      %25 = arith.constant 4 : i32
      %27 = arith.extsi %25 : i32 to i64
      %26 = arith.divsi %23, %27 : i64
      %28 = llvm.mlir.constant(1 : i64) : i64
      %29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
      llvm.store %26, %29 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %30 = llvm.load %29 : !llvm.ptr -> i64
      %31 = llvm.load %18 : !llvm.ptr -> i64
      %32 = arith.cmpi slt, %30, %31 : i64
      cf.cond_br %32, ^bb7, ^bb8
      ^bb7:
        %33 = llvm.load %18 : !llvm.ptr -> i64
        %34 = arith.constant 4 : i32
        %35 = llvm.load %29 : !llvm.ptr -> i64
        %37 = arith.extsi %34 : i32 to i64
        %36 = arith.muli %37, %35 : i64
        %38 = llvm.load %18 : !llvm.ptr -> i64
        %39 = arith.subi %36, %38 : i64
        %40 = arith.muli %33, %39 : i64
        %41 = arith.cmpi sge, %40, %1 : i64
        cf.cond_br %41, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %42 = arith.constant 0 : i32
        %44 = arith.extsi %42 : i32 to i64
        %43 = arith.cmpi sgt, %40, %44 : i64
        cf.cond_br %43, ^bb12, ^bb13
        ^bb12:
          %46 = llvm.getelementptr %2[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %45 = llvm.load %46 : !llvm.ptr -> i8
          %47 = arith.constant 1 : i32
          %49 = arith.extsi %45 : i8 to i32
          %48 = arith.cmpi eq, %49, %47 : i32
          cf.cond_br %48, ^bb15, ^bb16
          ^bb15:
            %50 = arith.constant 1 : i32
            %51 = arith.trunci %50 : i32 to i8
            %52 = llvm.getelementptr %5[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i8
            llvm.store %51, %52 : i8, !llvm.ptr
            cf.br ^bb17
          ^bb16:
            %53 = arith.constant 1 : i32
            %54 = arith.trunci %53 : i32 to i8
            %55 = llvm.getelementptr %2[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i8
            llvm.store %54, %55 : i8, !llvm.ptr
            cf.br ^bb17
          ^bb17:
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        %56 = llvm.load %29 : !llvm.ptr -> i64
        %57 = arith.constant 1 : i32
        %59 = arith.extsi %57 : i32 to i64
        %58 = arith.addi %56, %59 : i64
        llvm.store %58, %29 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %60 = llvm.load %18 : !llvm.ptr -> i64
      %61 = arith.constant 1 : i32
      %63 = arith.extsi %61 : i32 to i64
      %62 = arith.addi %60, %63 : i64
      llvm.store %62, %18 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %64 = arith.constant 0 : 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 1 : i32
    %69 = arith.extsi %68 : i32 to i64
    %70 = llvm.mlir.constant(1 : i64) : i64
    %71 = llvm.alloca %70 x i64 : (i64) -> !llvm.ptr
    llvm.store %69, %71 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %72 = llvm.load %71 : !llvm.ptr -> i64
    %73 = arith.cmpi slt, %72, %1 : i64
    cf.cond_br %73, ^bb19, ^bb20
    ^bb19:
      %75 = llvm.load %71 : !llvm.ptr -> i64
      %76 = llvm.getelementptr %2[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %74 = llvm.load %76 : !llvm.ptr -> i8
      %77 = arith.constant 1 : i32
      %79 = arith.extsi %74 : i8 to i32
      %78 = arith.cmpi eq, %79, %77 : i32
      %80 = scf.if %78 -> (i1) {
        %82 = llvm.load %71 : !llvm.ptr -> i64
        %83 = llvm.getelementptr %5[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %81 = llvm.load %83 : !llvm.ptr -> i8
        %84 = arith.constant 0 : i32
        %86 = arith.extsi %81 : i8 to i32
        %85 = arith.cmpi eq, %86, %84 : i32
        scf.yield %85 : i1
      } else {
        %87 = arith.constant false
        scf.yield %87 : i1
      }
      cf.cond_br %80, ^bb21, ^bb22
      ^bb21:
        %88 = llvm.load %67 : !llvm.ptr -> i64
        %89 = arith.constant 1 : i32
        %91 = arith.extsi %89 : i32 to i64
        %90 = arith.addi %88, %91 : i64
        llvm.store %90, %67 : i64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %92 = llvm.load %71 : !llvm.ptr -> i64
      %93 = arith.constant 1 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.addi %92, %95 : i64
      llvm.store %94, %71 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %96 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %97 = llvm.load %67 : !llvm.ptr -> i64
    %98 = llvm.call @printf(%96, %97) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%5) : (!llvm.ptr) -> ()
    func.call @free(%2) : (!llvm.ptr) -> ()
    %101 = arith.constant 0 : i32
    func.return %101 : i32
  }
}