Problem 072

Number of fractions a/b with 1 ≤ a < b ≤ 1,000,000 in lowest terms. Equals sum_{n=2..N} φ(n).

Answer303963552391
Output303963552391
StatusPASS
Native helperno
Runtime0 ms
Peak memory5024 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 totient computation
VerdictSuboptimal

Flow source

# Project Euler 072
# Number of fractions a/b with 1 ≤ a < b ≤ 1,000,000 in lowest terms.
# Equals sum_{n=2..N} φ(n).

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

function main() -> i32 {
    let n: i64 = 1000000
    let phi: ptr<i32> = calloc(n + 1, 4)
    if phi == null { return 1 }

    for i in 0..(n + 1) {
        phi[i as i32] = i as i32
    }

    for i in 2..(n + 1) {
        if phi[i as i32] == (i as i32) {
            let mut j: i64 = i
            while j <= n {
                phi[j as i32] = phi[j as i32] - phi[j as i32] / (i as i32)
                j = j + i
            }
        }
    }

    let mut total: i64 = 0
    for i in 2..(n + 1) {
        total = total + (phi[i as i32] as i64)
    }

    printf("%lld\n", total)
    free(phi)
    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 n = 1000000;
    int32_t* phi = (int32_t*)(calloc((n + 1), 4));
    if (phi == NULL) {
        return 1;
    }
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= (n + 1)) ? i < (n + 1) : i > (n + 1); i += (0 <= (n + 1)) ? 1 : -1) {
        phi[((int32_t)(i))] = ((int32_t)(i));
    }
    int32_t __flow_step_2 = 1;
    for (int32_t i = 2; (2 <= (n + 1)) ? i < (n + 1) : i > (n + 1); i += (2 <= (n + 1)) ? 1 : -1) {
        if (phi[((int32_t)(i))] == ((int32_t)(i))) {
            int64_t j = i;
            while (j <= n) {
                phi[((int32_t)(j))] = (phi[((int32_t)(j))] - FLOW_CHECKED_DIV((phi[((int32_t)(j))]), (((int32_t)(i)))));
                j = (j + i);
            }
        }
    }
    int64_t total = 0;
    int32_t __flow_step_3 = 1;
    for (int32_t i = 2; (2 <= (n + 1)) ? i < (n + 1) : i > (n + 1); i += (2 <= (n + 1)) ? 1 : -1) {
        total = (total + ((int64_t)(phi[((int32_t)(i))])));
    }
    printf("%lld\n", total);
    free(phi);
    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 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 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 0 : i32
    %12 = arith.constant 1 : i32
    %14 = arith.extsi %12 : i32 to i64
    %13 = arith.addi %1, %14 : i64
    %15 = arith.index_cast %11 : i32 to index
    %16 = arith.index_cast %13 : i32 to index
    %18 = arith.constant 1 : index
    %19 = arith.constant -1 : index
    %20 = arith.cmpi sle, %15, %16 : index
    %17 = arith.select %20, %18, %19 : index
    cf.br ^bb3(%15 : index)
    ^bb3(%21: index):
    %22 = arith.cmpi slt, %21, %16 : index
    %23 = arith.cmpi sgt, %21, %16 : index
    %24 = arith.select %20, %22, %23 : i1
    cf.cond_br %24, ^bb4(%21 : index), ^bb5(%21 : index)
    ^bb4(%25: index):
      %26 = arith.index_cast %25 : index to i32
      %27 = arith.index_cast %25 : index to i32
      %28 = arith.extsi %27 : i32 to i64
      %29 = llvm.getelementptr %2[%28] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %26, %29 : i32, !llvm.ptr
      %30 = arith.addi %25, %17 : index
      cf.br ^bb3(%30 : index)
    ^bb5(%31: index):
    %32 = arith.constant 2 : i32
    %33 = arith.constant 1 : i32
    %35 = arith.extsi %33 : i32 to i64
    %34 = arith.addi %1, %35 : i64
    %36 = arith.index_cast %32 : i32 to index
    %37 = arith.index_cast %34 : i32 to index
    %39 = arith.constant 1 : index
    %40 = arith.constant -1 : index
    %41 = arith.cmpi sle, %36, %37 : index
    %38 = arith.select %41, %39, %40 : index
    cf.br ^bb6(%36 : index)
    ^bb6(%42: index):
    %43 = arith.cmpi slt, %42, %37 : index
    %44 = arith.cmpi sgt, %42, %37 : index
    %45 = arith.select %41, %43, %44 : i1
    cf.cond_br %45, ^bb7(%42 : index), ^bb8(%42 : index)
    ^bb7(%46: index):
      %48 = arith.index_cast %46 : index to i32
      %49 = arith.extsi %48 : i32 to i64
      %50 = llvm.getelementptr %2[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %47 = llvm.load %50 : !llvm.ptr -> i32
      %51 = arith.index_cast %46 : index to i32
      %52 = arith.cmpi eq, %47, %51 : i32
      cf.cond_br %52, ^bb9, ^bb10
      ^bb9:
        %53 = arith.index_cast %46 : index to i64
        %54 = llvm.mlir.constant(1 : i64) : i64
        %55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
        llvm.store %53, %55 : i64, !llvm.ptr
        cf.br ^bb12
        ^bb12:
        %56 = llvm.load %55 : !llvm.ptr -> i64
        %57 = arith.cmpi sle, %56, %1 : i64
        cf.cond_br %57, ^bb13, ^bb14
        ^bb13:
          %59 = llvm.load %55 : !llvm.ptr -> i64
          %60 = arith.trunci %59 : i64 to i32
          %61 = arith.extsi %60 : i32 to i64
          %62 = llvm.getelementptr %2[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %58 = llvm.load %62 : !llvm.ptr -> i32
          %64 = llvm.load %55 : !llvm.ptr -> i64
          %65 = arith.trunci %64 : i64 to i32
          %66 = arith.extsi %65 : i32 to i64
          %67 = llvm.getelementptr %2[%66] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %63 = llvm.load %67 : !llvm.ptr -> i32
          %68 = arith.index_cast %46 : index to i32
          %69 = arith.divsi %63, %68 : i32
          %70 = arith.subi %58, %69 : i32
          %71 = llvm.load %55 : !llvm.ptr -> i64
          %72 = arith.trunci %71 : i64 to i32
          %73 = arith.extsi %72 : i32 to i64
          %74 = llvm.getelementptr %2[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %70, %74 : i32, !llvm.ptr
          %75 = llvm.load %55 : !llvm.ptr -> i64
          %77 = arith.trunci %75 : i64 to i32
          %78 = arith.index_cast %46 : index to i32
          %76 = arith.addi %77, %78 : i32
          %79 = arith.extsi %76 : i32 to i64
          llvm.store %79, %55 : i64, !llvm.ptr
          cf.br ^bb12
        ^bb14:
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %80 = arith.addi %46, %38 : index
      cf.br ^bb6(%80 : index)
    ^bb8(%81: index):
    %82 = arith.constant 0 : i32
    %83 = arith.extsi %82 : i32 to i64
    %84 = llvm.mlir.constant(1 : i64) : i64
    %85 = llvm.alloca %84 x i64 : (i64) -> !llvm.ptr
    llvm.store %83, %85 : i64, !llvm.ptr
    %86 = arith.constant 2 : i32
    %87 = arith.constant 1 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.addi %1, %89 : i64
    %90 = arith.index_cast %86 : i32 to index
    %91 = arith.index_cast %88 : i32 to index
    %93 = arith.constant 1 : index
    %94 = arith.constant -1 : index
    %95 = arith.cmpi sle, %90, %91 : index
    %92 = arith.select %95, %93, %94 : index
    cf.br ^bb15(%90 : index)
    ^bb15(%96: index):
    %97 = arith.cmpi slt, %96, %91 : index
    %98 = arith.cmpi sgt, %96, %91 : index
    %99 = arith.select %95, %97, %98 : i1
    cf.cond_br %99, ^bb16(%96 : index), ^bb17(%96 : index)
    ^bb16(%100: index):
      %101 = llvm.load %85 : !llvm.ptr -> i64
      %103 = arith.index_cast %100 : index to i32
      %104 = arith.extsi %103 : i32 to i64
      %105 = llvm.getelementptr %2[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %102 = llvm.load %105 : !llvm.ptr -> i32
      %106 = arith.extsi %102 : i32 to i64
      %107 = arith.addi %101, %106 : i64
      llvm.store %107, %85 : i64, !llvm.ptr
      %108 = arith.addi %100, %92 : index
      cf.br ^bb15(%108 : index)
    ^bb17(%109: index):
    %110 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %111 = llvm.load %85 : !llvm.ptr -> i64
    %112 = llvm.call @printf(%110, %111) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%2) : (!llvm.ptr) -> ()
    %114 = arith.constant 0 : i32
    func.return %114 : i32
  }
}