Problem 826

Birds on a Wire - average painted fraction over primes 3 <= p <= 10^6. Conjecture: F(p) = (7p + 15) / (18(p + 1)). Answer is average of F(p) over all primes p with 3 <= p <= 10^6, rounded to 10 decimal places.

Answer0.3889014797
Output0.3889014797
StatusPASS
Native helperno
Runtime0 ms
Peak memory2096 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 826
# Birds on a Wire - average painted fraction over primes 3 <= p <= 10^6.
# Conjecture: F(p) = (7p + 15) / (18(p + 1)). Answer is average of F(p)
# over all primes p with 3 <= p <= 10^6, rounded to 10 decimal places.

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

function main() -> i32 {
    let limit: i64 = 1000000

    # Sieve of Eratosthenes
    let is_prime: ptr<i8> = calloc(limit + 1, 1)
    if is_prime == null { return 1 }

    # calloc zero-fills, so is_prime[i] == 0 means prime (like p010)
    is_prime[0] = 1
    is_prime[1] = 1
    let mut i: i64 = 2
    while i * i <= limit {
        if is_prime[i] == 0 {
            let mut j: i64 = i * i
            while j <= limit {
                is_prime[j] = 1
                j = j + i
            }
        }
        i = i + 1
    }

    # Count primes and sum F(p) for p >= 3
    let mut prime_count: i64 = 0
    let mut total: f64 = 0.0
    let mut p: i64 = 2
    while p <= limit {
        if is_prime[p] == 0 {
            prime_count = prime_count + 1
            if p >= 3 {
                let pf: f64 = (p as f64)
                let num: f64 = 7.0 * pf + 15.0
                let den: f64 = 18.0 * (pf + 1.0)
                total = total + num / den
            }
        }
        p = p + 1
    }

    free(is_prime)

    # Average over primes p >= 3 (exclude p = 2)
    let denom: f64 = ((prime_count - 1) as f64)
    let avg: f64 = total / denom

    # Round to 10 decimal places
    let scale: f64 = 10000000000.0
    let rounded: f64 = round(avg * scale) / scale

    printf("%.10f\n", rounded)
    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;
    int8_t* is_prime = (int8_t*)(calloc((limit + 1), 1));
    if (is_prime == NULL) {
        return 1;
    }
    is_prime[0] = 1;
    is_prime[1] = 1;
    int64_t i = 2;
    while ((i * i) <= limit) {
        if (is_prime[i] == 0) {
            int64_t j = (i * i);
            while (j <= limit) {
                is_prime[j] = 1;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t prime_count = 0;
    double total = 0.0;
    int64_t p = 2;
    while (p <= limit) {
        if (is_prime[p] == 0) {
            prime_count = (prime_count + 1);
            if (p >= 3) {
                double pf = ((double)(p));
                double num = ((7.0 * pf) + 15.0);
                double den = (18.0 * (pf + 1.0));
                total = (total + (num / den));
            }
        }
        p = (p + 1);
    }
    free(is_prime);
    double denom = ((double)((prime_count - 1)));
    double avg = (total / denom);
    double scale = 10000000000.0;
    double rounded = (round((avg * scale)) / scale);
    printf("%.10f\n", rounded);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%.10f\n\00") {addr_space = 0 : i32} : !llvm.array<7 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func private @round(f64) -> f64
  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 1 : 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.constant 0 : i32
    %13 = arith.trunci %11 : i32 to i8
    %14 = arith.extsi %12 : i32 to i64
    %15 = llvm.getelementptr %2[%14] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %13, %15 : i8, !llvm.ptr
    %16 = arith.constant 1 : i32
    %17 = arith.constant 1 : i32
    %18 = arith.trunci %16 : i32 to i8
    %19 = arith.extsi %17 : i32 to i64
    %20 = llvm.getelementptr %2[%19] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %18, %20 : i8, !llvm.ptr
    %21 = arith.constant 2 : i32
    %22 = arith.extsi %21 : i32 to i64
    %23 = llvm.mlir.constant(1 : i64) : i64
    %24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
    llvm.store %22, %24 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %25 = llvm.load %24 : !llvm.ptr -> i64
    %26 = llvm.load %24 : !llvm.ptr -> i64
    %27 = arith.muli %25, %26 : i64
    %28 = arith.cmpi sle, %27, %1 : i64
    cf.cond_br %28, ^bb4, ^bb5
    ^bb4:
      %30 = llvm.load %24 : !llvm.ptr -> i64
      %31 = llvm.getelementptr %2[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %29 = llvm.load %31 : !llvm.ptr -> i8
      %32 = arith.constant 0 : i32
      %34 = arith.extsi %29 : i8 to i32
      %33 = arith.cmpi eq, %34, %32 : i32
      cf.cond_br %33, ^bb6, ^bb7
      ^bb6:
        %35 = llvm.load %24 : !llvm.ptr -> i64
        %36 = llvm.load %24 : !llvm.ptr -> i64
        %37 = arith.muli %35, %36 : i64
        %38 = llvm.mlir.constant(1 : i64) : i64
        %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
        llvm.store %37, %39 : i64, !llvm.ptr
        cf.br ^bb9
        ^bb9:
        %40 = llvm.load %39 : !llvm.ptr -> i64
        %41 = arith.cmpi sle, %40, %1 : i64
        cf.cond_br %41, ^bb10, ^bb11
        ^bb10:
          %42 = arith.constant 1 : i32
          %43 = llvm.load %39 : !llvm.ptr -> i64
          %44 = arith.trunci %42 : i32 to i8
          %45 = llvm.getelementptr %2[%43] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %44, %45 : i8, !llvm.ptr
          %46 = llvm.load %39 : !llvm.ptr -> i64
          %47 = llvm.load %24 : !llvm.ptr -> i64
          %48 = arith.addi %46, %47 : i64
          llvm.store %48, %39 : i64, !llvm.ptr
          cf.br ^bb9
        ^bb11:
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %49 = llvm.load %24 : !llvm.ptr -> i64
      %50 = arith.constant 1 : i32
      %52 = arith.extsi %50 : i32 to i64
      %51 = arith.addi %49, %52 : i64
      llvm.store %51, %24 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %53 = arith.constant 0 : i32
    %54 = arith.extsi %53 : i32 to i64
    %55 = llvm.mlir.constant(1 : i64) : i64
    %56 = llvm.alloca %55 x i64 : (i64) -> !llvm.ptr
    llvm.store %54, %56 : i64, !llvm.ptr
    %57 = arith.constant 0.0 : f32
    %58 = arith.extf %57 : f32 to f64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x f64 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : f64, !llvm.ptr
    %61 = arith.constant 2 : i32
    %62 = arith.extsi %61 : i32 to i64
    %63 = llvm.mlir.constant(1 : i64) : i64
    %64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
    llvm.store %62, %64 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %65 = llvm.load %64 : !llvm.ptr -> i64
    %66 = arith.cmpi sle, %65, %1 : i64
    cf.cond_br %66, ^bb13, ^bb14
    ^bb13:
      %68 = llvm.load %64 : !llvm.ptr -> i64
      %69 = llvm.getelementptr %2[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %67 = llvm.load %69 : !llvm.ptr -> i8
      %70 = arith.constant 0 : i32
      %72 = arith.extsi %67 : i8 to i32
      %71 = arith.cmpi eq, %72, %70 : i32
      cf.cond_br %71, ^bb15, ^bb16
      ^bb15:
        %73 = llvm.load %56 : !llvm.ptr -> i64
        %74 = arith.constant 1 : i32
        %76 = arith.extsi %74 : i32 to i64
        %75 = arith.addi %73, %76 : i64
        llvm.store %75, %56 : i64, !llvm.ptr
        %77 = llvm.load %64 : !llvm.ptr -> i64
        %78 = arith.constant 3 : i32
        %80 = arith.extsi %78 : i32 to i64
        %79 = arith.cmpi sge, %77, %80 : i64
        cf.cond_br %79, ^bb18, ^bb19
        ^bb18:
          %81 = llvm.load %64 : !llvm.ptr -> i64
          %82 = arith.sitofp %81 : i64 to f64
          %83 = arith.constant 7.0 : f32
          %85 = arith.extf %83 : f32 to f64
          %84 = arith.mulf %85, %82 : f64
          %86 = arith.constant 15.0 : f32
          %88 = arith.extf %86 : f32 to f64
          %87 = arith.addf %84, %88 : f64
          %89 = arith.constant 18.0 : f32
          %90 = arith.constant 1.0 : f32
          %92 = arith.extf %90 : f32 to f64
          %91 = arith.addf %82, %92 : f64
          %94 = arith.extf %89 : f32 to f64
          %93 = arith.mulf %94, %91 : f64
          %95 = llvm.load %60 : !llvm.ptr -> f64
          %96 = arith.divf %87, %93 : f64
          %97 = arith.addf %95, %96 : f64
          llvm.store %97, %60 : f64, !llvm.ptr
          cf.br ^bb20
        ^bb19:
          cf.br ^bb20
        ^bb20:
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %98 = llvm.load %64 : !llvm.ptr -> i64
      %99 = arith.constant 1 : i32
      %101 = arith.extsi %99 : i32 to i64
      %100 = arith.addi %98, %101 : i64
      llvm.store %100, %64 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    func.call @free(%2) : (!llvm.ptr) -> ()
    %103 = llvm.load %56 : !llvm.ptr -> i64
    %104 = arith.constant 1 : i32
    %106 = arith.extsi %104 : i32 to i64
    %105 = arith.subi %103, %106 : i64
    %107 = arith.sitofp %105 : i64 to f64
    %108 = llvm.load %60 : !llvm.ptr -> f64
    %109 = arith.divf %108, %107 : f64
    %110 = arith.constant 10000000000.0 : f32
    %111 = arith.extf %110 : f32 to f64
    %113 = arith.mulf %109, %111 : f64
    %112 = func.call @round(%113) : (f64) -> f64
    %114 = arith.divf %112, %111 : f64
    %115 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %116 = llvm.call @printf(%115, %114) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    %117 = arith.constant 0 : i32
    func.return %117 : i32
  }
}