Problem 278

Sum f(pq,pr,qr) for primes p<q<r<5000.

Answer1228215747273908452
Output1228215747273908452
StatusPASS
Native helperno
Runtime30 ms
Peak memory1152 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n^2)
Space complexityO(n)O(n)
ApproachFlow solutionTriple enumeration
VerdictSuboptimal

Flow source

# Project Euler 278
# Sum f(pq,pr,qr) for primes p<q<r<5000.

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

function main() -> i32 {
    let limit: i64 = 5000
    let primes: ptr<i64> = calloc(limit, 8)
    if primes == null { return 1 }
    let mut pc: i64 = 0
    primes[0] = 2
    pc = 1
    let mut i: i64 = 3
    while i < limit {
        let mut is_p: bool = true
        let mut j: i64 = 0
        while j < pc {
            let x: i64 = primes[j]
            if x * x > i { break }
            if i % x == 0 {
                is_p = false
                break
            }
            j = j + 1
        }
        if is_p {
            primes[pc] = i
            pc = pc + 1
        }
        i = i + 2
    }
    let mut sum: i64 = 0
    i = 0
    while i < pc {
        let mut j: i64 = i + 1
        while j < pc {
            let mut k: i64 = j + 1
            while k < pc {
                let p: i64 = primes[i]
                let q: i64 = primes[j]
                let r: i64 = primes[k]
                sum = sum + 2 * p * q * r - p * q - p * r - q * r
                k = k + 1
            }
            j = j + 1
        }
        i = i + 1
    }
    printf("%lld\n", sum)
    free(primes)
    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 = 5000;
    int64_t* primes = (int64_t*)(calloc(limit, 8));
    if (primes == NULL) {
        return 1;
    }
    int64_t pc = 0;
    primes[0] = 2;
    pc = 1;
    int64_t i = 3;
    while (i < limit) {
        bool is_p = 1;
        int64_t j = 0;
        while (j < pc) {
            int64_t x = primes[j];
            if ((x * x) > i) {
                break;
            }
            if (FLOW_CHECKED_MOD((i), (x)) == 0) {
                is_p = 0;
                break;
            }
            j = (j + 1);
        }
        if (is_p) {
            primes[pc] = i;
            pc = (pc + 1);
        }
        i = (i + 2);
    }
    int64_t sum = 0;
    i = 0;
    while (i < pc) {
        int64_t j = (i + 1);
        while (j < pc) {
            int64_t k = (j + 1);
            while (k < pc) {
                int64_t p = primes[i];
                int64_t q = primes[j];
                int64_t r = primes[k];
                sum = ((((sum + (((2 * p) * q) * r)) - (p * q)) - (p * r)) - (q * r));
                k = (k + 1);
            }
            j = (j + 1);
        }
        i = (i + 1);
    }
    printf("%lld\n", sum);
    free(primes);
    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 5000 : i32
    %1 = arith.extsi %0 : i32 to i64
    %3 = arith.constant 8 : 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 0 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    %12 = arith.constant 2 : i32
    %13 = arith.constant 0 : i32
    %14 = arith.extsi %12 : i32 to i64
    %15 = arith.extsi %13 : i32 to i64
    %16 = llvm.getelementptr %2[%15] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %14, %16 : i64, !llvm.ptr
    %17 = arith.constant 1 : i32
    %18 = arith.extsi %17 : i32 to i64
    llvm.store %18, %11 : i64, !llvm.ptr
    %19 = arith.constant 3 : i32
    %20 = arith.extsi %19 : i32 to i64
    %21 = llvm.mlir.constant(1 : i64) : i64
    %22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
    llvm.store %20, %22 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %23 = llvm.load %22 : !llvm.ptr -> i64
    %24 = arith.cmpi slt, %23, %1 : i64
    cf.cond_br %24, ^bb4, ^bb5
    ^bb4:
      %25 = arith.constant 1 : i1
      %26 = llvm.mlir.constant(1 : i64) : i64
      %27 = llvm.alloca %26 x i1 : (i64) -> !llvm.ptr
      llvm.store %25, %27 : i1, !llvm.ptr
      %28 = arith.constant 0 : i32
      %29 = arith.extsi %28 : i32 to i64
      %30 = llvm.mlir.constant(1 : i64) : i64
      %31 = llvm.alloca %30 x i64 : (i64) -> !llvm.ptr
      llvm.store %29, %31 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %32 = llvm.load %31 : !llvm.ptr -> i64
      %33 = llvm.load %11 : !llvm.ptr -> i64
      %34 = arith.cmpi slt, %32, %33 : i64
      cf.cond_br %34, ^bb7, ^bb8
      ^bb7:
        %36 = llvm.load %31 : !llvm.ptr -> i64
        %37 = llvm.getelementptr %2[%36] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %35 = llvm.load %37 : !llvm.ptr -> i64
        %38 = arith.muli %35, %35 : i64
        %39 = llvm.load %22 : !llvm.ptr -> i64
        %40 = arith.cmpi sgt, %38, %39 : i64
        cf.cond_br %40, ^bb9, ^bb10
        ^bb9:
          cf.br ^bb8
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %41 = llvm.load %22 : !llvm.ptr -> i64
        %42 = arith.remsi %41, %35 : i64
        %43 = arith.constant 0 : i32
        %45 = arith.extsi %43 : i32 to i64
        %44 = arith.cmpi eq, %42, %45 : i64
        cf.cond_br %44, ^bb12, ^bb13
        ^bb12:
          %46 = arith.constant 0 : i1
          llvm.store %46, %27 : i1, !llvm.ptr
          cf.br ^bb8
        ^bb13:
          cf.br ^bb14
        ^bb14:
        %47 = llvm.load %31 : !llvm.ptr -> i64
        %48 = arith.constant 1 : i32
        %50 = arith.extsi %48 : i32 to i64
        %49 = arith.addi %47, %50 : i64
        llvm.store %49, %31 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %51 = llvm.load %27 : !llvm.ptr -> i1
      cf.cond_br %51, ^bb15, ^bb16
      ^bb15:
        %52 = llvm.load %22 : !llvm.ptr -> i64
        %53 = llvm.load %11 : !llvm.ptr -> i64
        %54 = llvm.getelementptr %2[%53] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %52, %54 : i64, !llvm.ptr
        %55 = llvm.load %11 : !llvm.ptr -> i64
        %56 = arith.constant 1 : i32
        %58 = arith.extsi %56 : i32 to i64
        %57 = arith.addi %55, %58 : i64
        llvm.store %57, %11 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %59 = llvm.load %22 : !llvm.ptr -> i64
      %60 = arith.constant 2 : i32
      %62 = arith.extsi %60 : i32 to i64
      %61 = arith.addi %59, %62 : i64
      llvm.store %61, %22 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %63 = arith.constant 0 : i32
    %64 = arith.extsi %63 : i32 to i64
    %65 = llvm.mlir.constant(1 : i64) : i64
    %66 = llvm.alloca %65 x i64 : (i64) -> !llvm.ptr
    llvm.store %64, %66 : i64, !llvm.ptr
    %67 = arith.constant 0 : i32
    %68 = arith.extsi %67 : i32 to i64
    llvm.store %68, %22 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %69 = llvm.load %22 : !llvm.ptr -> i64
    %70 = llvm.load %11 : !llvm.ptr -> i64
    %71 = arith.cmpi slt, %69, %70 : i64
    cf.cond_br %71, ^bb19, ^bb20
    ^bb19:
      %72 = llvm.load %22 : !llvm.ptr -> i64
      %73 = arith.constant 1 : i32
      %75 = arith.extsi %73 : i32 to i64
      %74 = arith.addi %72, %75 : i64
      %76 = llvm.mlir.constant(1 : i64) : i64
      %77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
      llvm.store %74, %77 : i64, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %78 = llvm.load %77 : !llvm.ptr -> i64
      %79 = llvm.load %11 : !llvm.ptr -> i64
      %80 = arith.cmpi slt, %78, %79 : i64
      cf.cond_br %80, ^bb22, ^bb23
      ^bb22:
        %81 = llvm.load %77 : !llvm.ptr -> i64
        %82 = arith.constant 1 : i32
        %84 = arith.extsi %82 : i32 to i64
        %83 = arith.addi %81, %84 : i64
        %85 = llvm.mlir.constant(1 : i64) : i64
        %86 = llvm.alloca %85 x i64 : (i64) -> !llvm.ptr
        llvm.store %83, %86 : i64, !llvm.ptr
        cf.br ^bb24
        ^bb24:
        %87 = llvm.load %86 : !llvm.ptr -> i64
        %88 = llvm.load %11 : !llvm.ptr -> i64
        %89 = arith.cmpi slt, %87, %88 : i64
        cf.cond_br %89, ^bb25, ^bb26
        ^bb25:
          %91 = llvm.load %22 : !llvm.ptr -> i64
          %92 = llvm.getelementptr %2[%91] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %90 = llvm.load %92 : !llvm.ptr -> i64
          %94 = llvm.load %77 : !llvm.ptr -> i64
          %95 = llvm.getelementptr %2[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %93 = llvm.load %95 : !llvm.ptr -> i64
          %97 = llvm.load %86 : !llvm.ptr -> i64
          %98 = llvm.getelementptr %2[%97] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %96 = llvm.load %98 : !llvm.ptr -> i64
          %99 = llvm.load %66 : !llvm.ptr -> i64
          %100 = arith.constant 2 : i32
          %102 = arith.extsi %100 : i32 to i64
          %101 = arith.muli %102, %90 : i64
          %103 = arith.muli %101, %93 : i64
          %104 = arith.muli %103, %96 : i64
          %105 = arith.addi %99, %104 : i64
          %106 = arith.muli %90, %93 : i64
          %107 = arith.subi %105, %106 : i64
          %108 = arith.muli %90, %96 : i64
          %109 = arith.subi %107, %108 : i64
          %110 = arith.muli %93, %96 : i64
          %111 = arith.subi %109, %110 : i64
          llvm.store %111, %66 : i64, !llvm.ptr
          %112 = llvm.load %86 : !llvm.ptr -> i64
          %113 = arith.constant 1 : i32
          %115 = arith.extsi %113 : i32 to i64
          %114 = arith.addi %112, %115 : i64
          llvm.store %114, %86 : i64, !llvm.ptr
          cf.br ^bb24
        ^bb26:
        %116 = llvm.load %77 : !llvm.ptr -> i64
        %117 = arith.constant 1 : i32
        %119 = arith.extsi %117 : i32 to i64
        %118 = arith.addi %116, %119 : i64
        llvm.store %118, %77 : i64, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      %120 = llvm.load %22 : !llvm.ptr -> i64
      %121 = arith.constant 1 : i32
      %123 = arith.extsi %121 : i32 to i64
      %122 = arith.addi %120, %123 : i64
      llvm.store %122, %22 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %124 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %125 = llvm.load %66 : !llvm.ptr -> i64
    %126 = llvm.call @printf(%124, %125) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %128 = arith.constant 0 : i32
    func.return %128 : i32
  }
}