Problem 123

Least n such that (p_n-1)^n + (p_n+1)^n mod p_n^2 > 10^9.

Answer21035
Output21035
StatusPASS
Native helperno
Runtime0 ms
Peak memory2896 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(log n)
Space complexityO(n)O(1)
ApproachFlow solutionModular exponentiation
VerdictSuboptimal

Flow source

# Project Euler 123
# Least n such that (p_n-1)^n + (p_n+1)^n mod p_n^2 > 10^9.

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

function main() -> i32 {
    # Need primes up to roughly n~21000, p~250000
    let limit: i64 = 1000000
    let sieve: ptr<i8> = calloc(limit, 1)
    if sieve == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }
    let primes: ptr<i64> = calloc(100000, 8)
    if primes == null { free(sieve); return 1 }
    let mut pc: i32 = 0
    let mut i: i64 = 2
    while i < limit {
        if sieve[i] == 0 {
            primes[pc] = i
            pc = pc + 1
        }
        i = i + 1
    }

    let thresh: i64 = 10000000000
    let mut n: i32 = 1
    while n <= pc {
        # For odd n: rem = 2*n*p mod p^2 = 2*n*p (since 2*n*p < p^2 for n < p/2)
        # For even n: rem = 2
        if n % 2 == 1 {
            let pn: i64 = primes[n - 1]
            let rem: i64 = (2 * (n as i64) * pn) % (pn * pn)
            if rem > thresh {
                printf("%lld\n", n as i64)
                free(primes)
                free(sieve)
                return 0
            }
        }
        n = n + 1
    }
    free(primes)
    free(sieve)
    return 1
}

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* sieve = (int8_t*)(calloc(limit, 1));
    if (sieve == NULL) {
        return 1;
    }
    sieve[0] = 1;
    sieve[1] = 1;
    int64_t p = 2;
    while ((p * p) < limit) {
        if (sieve[p] == 0) {
            int64_t m = (p * p);
            while (m < limit) {
                sieve[m] = 1;
                m = (m + p);
            }
        }
        p = (p + 1);
    }
    int64_t* primes = (int64_t*)(calloc(100000, 8));
    if (primes == NULL) {
        free(sieve);
        return 1;
    }
    int32_t pc = 0;
    int64_t i = 2;
    while (i < limit) {
        if (sieve[i] == 0) {
            primes[pc] = i;
            pc = (pc + 1);
        }
        i = (i + 1);
    }
    int64_t thresh = 10000000000;
    int32_t n = 1;
    while (n <= pc) {
        if (FLOW_CHECKED_MOD((n), (2)) == 1) {
            int64_t pn = primes[(n - 1)];
            int64_t rem = FLOW_CHECKED_MOD((((2 * ((int64_t)(n))) * pn)), ((pn * pn)));
            if (rem > thresh) {
                printf("%lld\n", ((int64_t)(n)));
                free(primes);
                free(sieve);
                return 0;
            }
        }
        n = (n + 1);
    }
    free(primes);
    free(sieve);
    return 1;
}

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
    %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 1 : i32
    %9 = arith.constant 0 : i32
    %10 = arith.trunci %8 : i32 to i8
    %11 = arith.extsi %9 : i32 to i64
    %12 = llvm.getelementptr %2[%11] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %10, %12 : i8, !llvm.ptr
    %13 = arith.constant 1 : i32
    %14 = arith.constant 1 : i32
    %15 = arith.trunci %13 : i32 to i8
    %16 = arith.extsi %14 : i32 to i64
    %17 = llvm.getelementptr %2[%16] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %15, %17 : i8, !llvm.ptr
    %18 = arith.constant 2 : i32
    %19 = arith.extsi %18 : i32 to i64
    %20 = llvm.mlir.constant(1 : i64) : i64
    %21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
    llvm.store %19, %21 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %22 = llvm.load %21 : !llvm.ptr -> i64
    %23 = llvm.load %21 : !llvm.ptr -> i64
    %24 = arith.muli %22, %23 : i64
    %25 = arith.cmpi slt, %24, %1 : i64
    cf.cond_br %25, ^bb4, ^bb5
    ^bb4:
      %27 = llvm.load %21 : !llvm.ptr -> i64
      %28 = llvm.getelementptr %2[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %26 = llvm.load %28 : !llvm.ptr -> i8
      %29 = arith.constant 0 : i32
      %31 = arith.extsi %26 : i8 to i32
      %30 = arith.cmpi eq, %31, %29 : i32
      cf.cond_br %30, ^bb6, ^bb7
      ^bb6:
        %32 = llvm.load %21 : !llvm.ptr -> i64
        %33 = llvm.load %21 : !llvm.ptr -> i64
        %34 = arith.muli %32, %33 : i64
        %35 = llvm.mlir.constant(1 : i64) : i64
        %36 = llvm.alloca %35 x i64 : (i64) -> !llvm.ptr
        llvm.store %34, %36 : i64, !llvm.ptr
        cf.br ^bb9
        ^bb9:
        %37 = llvm.load %36 : !llvm.ptr -> i64
        %38 = arith.cmpi slt, %37, %1 : i64
        cf.cond_br %38, ^bb10, ^bb11
        ^bb10:
          %39 = arith.constant 1 : i32
          %40 = llvm.load %36 : !llvm.ptr -> i64
          %41 = arith.trunci %39 : i32 to i8
          %42 = llvm.getelementptr %2[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %41, %42 : i8, !llvm.ptr
          %43 = llvm.load %36 : !llvm.ptr -> i64
          %44 = llvm.load %21 : !llvm.ptr -> i64
          %45 = arith.addi %43, %44 : i64
          llvm.store %45, %36 : i64, !llvm.ptr
          cf.br ^bb9
        ^bb11:
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %46 = llvm.load %21 : !llvm.ptr -> i64
      %47 = arith.constant 1 : i32
      %49 = arith.extsi %47 : i32 to i64
      %48 = arith.addi %46, %49 : i64
      llvm.store %48, %21 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %51 = arith.constant 100000 : i32
    %52 = arith.constant 8 : i32
    %53 = arith.extsi %51 : i32 to i64
    %54 = arith.extsi %52 : i32 to i64
    %50 = func.call @calloc(%53, %54) : (i64, i64) -> !llvm.ptr
    %55 = llvm.mlir.zero : !llvm.ptr
    %56 = llvm.icmp "eq" %50, %55 : !llvm.ptr
    cf.cond_br %56, ^bb12, ^bb13
    ^bb12:
      func.call @free(%2) : (!llvm.ptr) -> ()
      %58 = arith.constant 1 : i32
      func.return %58 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %59 = arith.constant 0 : i32
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i32 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i32, !llvm.ptr
    %62 = arith.constant 2 : i32
    %63 = arith.extsi %62 : i32 to i64
    %64 = llvm.mlir.constant(1 : i64) : i64
    %65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
    llvm.store %63, %65 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %66 = llvm.load %65 : !llvm.ptr -> i64
    %67 = arith.cmpi slt, %66, %1 : i64
    cf.cond_br %67, ^bb16, ^bb17
    ^bb16:
      %69 = llvm.load %65 : !llvm.ptr -> i64
      %70 = llvm.getelementptr %2[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %68 = llvm.load %70 : !llvm.ptr -> i8
      %71 = arith.constant 0 : i32
      %73 = arith.extsi %68 : i8 to i32
      %72 = arith.cmpi eq, %73, %71 : i32
      cf.cond_br %72, ^bb18, ^bb19
      ^bb18:
        %74 = llvm.load %65 : !llvm.ptr -> i64
        %75 = llvm.load %61 : !llvm.ptr -> i32
        %76 = arith.extsi %75 : i32 to i64
        %77 = llvm.getelementptr %50[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %74, %77 : i64, !llvm.ptr
        %78 = llvm.load %61 : !llvm.ptr -> i32
        %79 = arith.constant 1 : i32
        %80 = arith.addi %78, %79 : i32
        llvm.store %80, %61 : i32, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %81 = llvm.load %65 : !llvm.ptr -> i64
      %82 = arith.constant 1 : i32
      %84 = arith.extsi %82 : i32 to i64
      %83 = arith.addi %81, %84 : i64
      llvm.store %83, %65 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %85 = arith.constant 5705032704 : i32
    %86 = arith.extsi %85 : i32 to i64
    %87 = arith.constant 1 : i32
    %88 = llvm.mlir.constant(1 : i64) : i64
    %89 = llvm.alloca %88 x i32 : (i64) -> !llvm.ptr
    llvm.store %87, %89 : i32, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %90 = llvm.load %89 : !llvm.ptr -> i32
    %91 = llvm.load %61 : !llvm.ptr -> i32
    %92 = arith.cmpi sle, %90, %91 : i32
    cf.cond_br %92, ^bb22, ^bb23
    ^bb22:
      %93 = llvm.load %89 : !llvm.ptr -> i32
      %94 = arith.constant 2 : i32
      %95 = arith.remsi %93, %94 : i32
      %96 = arith.constant 1 : i32
      %97 = arith.cmpi eq, %95, %96 : i32
      cf.cond_br %97, ^bb24, ^bb25
      ^bb24:
        %99 = llvm.load %89 : !llvm.ptr -> i32
        %100 = arith.constant 1 : i32
        %101 = arith.subi %99, %100 : i32
        %102 = arith.extsi %101 : i32 to i64
        %103 = llvm.getelementptr %50[%102] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %98 = llvm.load %103 : !llvm.ptr -> i64
        %104 = arith.constant 2 : i32
        %105 = llvm.load %89 : !llvm.ptr -> i32
        %106 = arith.extsi %105 : i32 to i64
        %108 = arith.extsi %104 : i32 to i64
        %107 = arith.muli %108, %106 : i64
        %109 = arith.muli %107, %98 : i64
        %110 = arith.muli %98, %98 : i64
        %111 = arith.remsi %109, %110 : i64
        %112 = arith.cmpi sgt, %111, %86 : i64
        cf.cond_br %112, ^bb27, ^bb28
        ^bb27:
          %113 = llvm.mlir.addressof @str_0 : !llvm.ptr
          %114 = llvm.load %89 : !llvm.ptr -> i32
          %115 = arith.extsi %114 : i32 to i64
          %116 = llvm.call @printf(%113, %115) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
          func.call @free(%50) : (!llvm.ptr) -> ()
          func.call @free(%2) : (!llvm.ptr) -> ()
          %119 = arith.constant 0 : i32
          func.return %119 : i32
        ^bb28:
          cf.br ^bb29
        ^bb29:
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %120 = llvm.load %89 : !llvm.ptr -> i32
      %121 = arith.constant 1 : i32
      %122 = arith.addi %120, %121 : i32
      llvm.store %122, %89 : i32, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    func.call @free(%50) : (!llvm.ptr) -> ()
    func.call @free(%2) : (!llvm.ptr) -> ()
    %125 = arith.constant 1 : i32
    func.return %125 : i32
  }
}