Problem 179

Count n in (1, 10^7) where n and n+1 have the same number of divisors.

Answer986262
Output986262
StatusPASS
Native helperno
Runtime570 ms
Peak memory40160 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-based divisor sums
VerdictSuboptimal

Flow source

# Project Euler 179
# Count n in (1, 10^7) where n and n+1 have the same number of divisors.

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

function main() -> i32 {
    let LIMIT: i64 = 10000000
    let divs: ptr<i32> = calloc(LIMIT + 1, 4)
    if divs == null { return 1 }
    let mut i: i64 = 1
    while i <= LIMIT {
        let mut j: i64 = i
        while j <= LIMIT {
            divs[j] = divs[j] + 1
            j = j + i
        }
        i = i + 1
    }
    let mut count: i64 = 0
    let mut n: i64 = 2
    while n < LIMIT {
        if divs[n] == divs[n + 1] {
            count = count + 1
        }
        n = n + 1
    }
    printf("%lld\n", count)
    free(divs)
    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 = 10000000;
    int32_t* divs = (int32_t*)(calloc((LIMIT + 1), 4));
    if (divs == NULL) {
        return 1;
    }
    int64_t i = 1;
    while (i <= LIMIT) {
        int64_t j = i;
        while (j <= LIMIT) {
            divs[j] = (divs[j] + 1);
            j = (j + i);
        }
        i = (i + 1);
    }
    int64_t count = 0;
    int64_t n = 2;
    while (n < LIMIT) {
        if (divs[n] == divs[(n + 1)]) {
            count = (count + 1);
        }
        n = (n + 1);
    }
    printf("%lld\n", count);
    free(divs);
    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 10000000 : 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 4 : 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.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.cmpi sle, %15, %1 : i64
    cf.cond_br %16, ^bb4, ^bb5
    ^bb4:
      %17 = llvm.load %14 : !llvm.ptr -> i64
      %18 = llvm.mlir.constant(1 : i64) : i64
      %19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
      llvm.store %17, %19 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %20 = llvm.load %19 : !llvm.ptr -> i64
      %21 = arith.cmpi sle, %20, %1 : i64
      cf.cond_br %21, ^bb7, ^bb8
      ^bb7:
        %23 = llvm.load %19 : !llvm.ptr -> i64
        %24 = llvm.getelementptr %2[%23] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %22 = llvm.load %24 : !llvm.ptr -> i32
        %25 = arith.constant 1 : i32
        %26 = arith.addi %22, %25 : i32
        %27 = llvm.load %19 : !llvm.ptr -> i64
        %28 = llvm.getelementptr %2[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %26, %28 : i32, !llvm.ptr
        %29 = llvm.load %19 : !llvm.ptr -> i64
        %30 = llvm.load %14 : !llvm.ptr -> i64
        %31 = arith.addi %29, %30 : i64
        llvm.store %31, %19 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %32 = llvm.load %14 : !llvm.ptr -> i64
      %33 = arith.constant 1 : i32
      %35 = arith.extsi %33 : i32 to i64
      %34 = arith.addi %32, %35 : i64
      llvm.store %34, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %36 = arith.constant 0 : i32
    %37 = arith.extsi %36 : i32 to i64
    %38 = llvm.mlir.constant(1 : i64) : i64
    %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
    llvm.store %37, %39 : i64, !llvm.ptr
    %40 = arith.constant 2 : i32
    %41 = arith.extsi %40 : i32 to i64
    %42 = llvm.mlir.constant(1 : i64) : i64
    %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
    llvm.store %41, %43 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %44 = llvm.load %43 : !llvm.ptr -> i64
    %45 = arith.cmpi slt, %44, %1 : i64
    cf.cond_br %45, ^bb10, ^bb11
    ^bb10:
      %47 = llvm.load %43 : !llvm.ptr -> i64
      %48 = llvm.getelementptr %2[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %46 = llvm.load %48 : !llvm.ptr -> i32
      %50 = llvm.load %43 : !llvm.ptr -> i64
      %51 = arith.constant 1 : i32
      %53 = arith.extsi %51 : i32 to i64
      %52 = arith.addi %50, %53 : i64
      %54 = llvm.getelementptr %2[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %49 = llvm.load %54 : !llvm.ptr -> i32
      %55 = arith.cmpi eq, %46, %49 : i32
      cf.cond_br %55, ^bb12, ^bb13
      ^bb12:
        %56 = llvm.load %39 : !llvm.ptr -> i64
        %57 = arith.constant 1 : i32
        %59 = arith.extsi %57 : i32 to i64
        %58 = arith.addi %56, %59 : i64
        llvm.store %58, %39 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %60 = llvm.load %43 : !llvm.ptr -> i64
      %61 = arith.constant 1 : i32
      %63 = arith.extsi %61 : i32 to i64
      %62 = arith.addi %60, %63 : i64
      llvm.store %62, %43 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %64 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %65 = llvm.load %39 : !llvm.ptr -> i64
    %66 = llvm.call @printf(%64, %65) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %68 = arith.constant 0 : i32
    func.return %68 : i32
  }
}