Problem 046

Smallest odd composite that cannot be written as p + 2·k².

Answer5777
Output5777
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 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 + Goldbach check
VerdictSuboptimal

Flow source

# Project Euler 046
# Smallest odd composite that cannot be written as p + 2·k².

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

function main() -> i32 {
    let limit: i64 = 10000
    let sieve: ptr<i8> = calloc(limit, 1)
    if sieve == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }

    let mut n: i64 = 9
    while n < limit {
        if sieve[n] != 0 {  # composite
            let mut ok: bool = false
            let mut k: i64 = 1
            while 2 * k * k < n {
                let rem: i64 = n - 2 * k * k
                if rem > 1 && sieve[rem] == 0 {
                    ok = true
                    break
                }
                k = k + 1
            }
            if !ok {
                printf("%lld\n", n)
                free(sieve)
                return 0
            }
        }
        n = n + 2
    }
    free(sieve)
    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; }

int32_t main(void);



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