Problem 318

Sum of ceil(-2011 / log10(p+q-2*sqrt(pq))) for 0 < that < 1.

Answer709313889
Output709313889
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 318
# Sum of ceil(-2011 / log10(p+q-2*sqrt(pq))) for 0 < that < 1.

extern {
    function sqrt(x: f64) -> f64
    function log(x: f64) -> f64
    function ceil(x: f64) -> f64
}

function main() -> i32 {
    let limit: i64 = 2011
    let mut total: i64 = 0
    let mut p: i64 = 1
    while p < limit {
        let mut q: i64 = p + 1
        while q <= limit - p {
            let t: f64 = (p as f64) + (q as f64) - 2.0 * sqrt((p * q) as f64)
            if t > 0.0 && t < 1.0 {
                let v: f64 = -(limit as f64) / (log(t) / log(10.0))
                total = total + (ceil(v) as i64)
            }
            q = q + 1
        }
        p = p + 1
    }
    printf("%lld\n", total)
    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 = 2011;
    int64_t total = 0;
    int64_t p = 1;
    while (p < limit) {
        int64_t q = (p + 1);
        while (q <= (limit - p)) {
            double t = ((((double)(p)) + ((double)(q))) - (2.0 * sqrt(((double)((p * q))))));
            if ((t > 0.0 && t < 1.0)) {
                double v = ((-((double)(limit))) / (log(t) / log(10.0)));
                total = (total + ((int64_t)(ceil(v))));
            }
            q = (q + 1);
        }
        p = (p + 1);
    }
    printf("%lld\n", total);
    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 @sqrt(f64) -> f64
  func.func private @log(f64) -> f64
  func.func private @ceil(f64) -> f64
  func.func @main() -> i32 {
    %0 = arith.constant 2011 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 0 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %5 : i64, !llvm.ptr
    %6 = arith.constant 1 : i32
    %7 = arith.extsi %6 : i32 to i64
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
    llvm.store %7, %9 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %10 = llvm.load %9 : !llvm.ptr -> i64
    %11 = arith.cmpi slt, %10, %1 : i64
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %12 = llvm.load %9 : !llvm.ptr -> i64
      %13 = arith.constant 1 : i32
      %15 = arith.extsi %13 : i32 to i64
      %14 = arith.addi %12, %15 : i64
      %16 = llvm.mlir.constant(1 : i64) : i64
      %17 = llvm.alloca %16 x i64 : (i64) -> !llvm.ptr
      llvm.store %14, %17 : i64, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %18 = llvm.load %17 : !llvm.ptr -> i64
      %19 = llvm.load %9 : !llvm.ptr -> i64
      %20 = arith.subi %1, %19 : i64
      %21 = arith.cmpi sle, %18, %20 : i64
      cf.cond_br %21, ^bb4, ^bb5
      ^bb4:
        %22 = llvm.load %9 : !llvm.ptr -> i64
        %23 = arith.sitofp %22 : i64 to f64
        %24 = llvm.load %17 : !llvm.ptr -> i64
        %25 = arith.sitofp %24 : i64 to f64
        %26 = arith.addf %23, %25 : f64
        %27 = arith.constant 2.0 : f32
        %28 = llvm.load %9 : !llvm.ptr -> i64
        %29 = llvm.load %17 : !llvm.ptr -> i64
        %30 = arith.muli %28, %29 : i64
        %31 = arith.sitofp %30 : i64 to f64
        %32 = math.sqrt %31 : f64
        %34 = arith.extf %27 : f32 to f64
        %33 = arith.mulf %34, %32 : f64
        %35 = arith.subf %26, %33 : f64
        %36 = arith.constant 0.0 : f32
        %38 = arith.extf %36 : f32 to f64
        %37 = arith.cmpf ogt, %35, %38 : f64
        %39 = scf.if %37 -> (i1) {
          %40 = arith.constant 1.0 : f32
          %42 = arith.extf %40 : f32 to f64
          %41 = arith.cmpf olt, %35, %42 : f64
          scf.yield %41 : i1
        } else {
          %43 = arith.constant false
          scf.yield %43 : i1
        }
        cf.cond_br %39, ^bb6, ^bb7
        ^bb6:
          %44 = arith.sitofp %1 : i64 to f64
          %45 = arith.negf %44 : f64
          %46 = math.log %35 : f64
          %47 = arith.constant 10.0 : f32
          %48 = math.log %47 : f32
          %49 = arith.divf %46, %48 : f64
          %50 = arith.divf %45, %49 : f64
          %51 = llvm.load %5 : !llvm.ptr -> i64
          %52 = func.call @ceil(%50) : (f64) -> f64
          %53 = arith.fptosi %52 : f64 to i64
          %54 = arith.addi %51, %53 : i64
          llvm.store %54, %5 : i64, !llvm.ptr
          cf.br ^bb8
        ^bb7:
          cf.br ^bb8
        ^bb8:
        %55 = llvm.load %17 : !llvm.ptr -> i64
        %56 = arith.constant 1 : i32
        %58 = arith.extsi %56 : i32 to i64
        %57 = arith.addi %55, %58 : i64
        llvm.store %57, %17 : i64, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %59 = llvm.load %9 : !llvm.ptr -> i64
      %60 = arith.constant 1 : i32
      %62 = arith.extsi %60 : i32 to i64
      %61 = arith.addi %59, %62 : i64
      llvm.store %61, %9 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %63 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %64 = llvm.load %5 : !llvm.ptr -> i64
    %65 = llvm.call @printf(%63, %64) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %66 = arith.constant 0 : i32
    func.return %66 : i32
  }
}