Problem 817

Digits in Squares — sum M(p, p-d) for d=1..1e5 (i128 for large intervals).

Answer93158936107011
Output93158936107011
StatusPASS
Native helperno
Runtime180 ms
Peak memory1104 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 817
# Digits in Squares — sum M(p, p-d) for d=1..1e5 (i128 for large intervals).

function isqrt128(n: i128) -> i128 {
    if n < 2 { return n }
    let mut x: i128 = n
    let mut y: i128 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function ceil_isqrt128(n: i128) -> i128 {
    if n <= 0 { return 0 }
    return isqrt128(n - 1) + 1
}

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    let mut e: i64 = exp0
    while e > 0 {
        if (e & 1) != 0 {
            r = ((r as i128) * (b as i128) % (mod as i128)) as i64
        }
        b = ((b as i128) * (b as i128) % (mod as i128)) as i64
        e = e >> 1
    }
    return r
}

function M_pd(p: i64, d: i64, exp_leg: i64, exp_sqrt: i64, p2: i128) -> i64 {
    let a: i64 = p - d
    let ls: i64 = modpow(a, exp_leg, p)
    if ls == 1 {
        let mut r: i64 = modpow(a, exp_sqrt, p)
        if r > p - r { r = p - r }
        return r
    }
    let dp: i128 = (d as i128) * (p as i128)
    let mut L: i128 = p2 - dp
    let p128: i128 = p as i128
    while 1 == 1 {
        let m: i128 = ceil_isqrt128(L)
        if m * m <= L + (p128 - 1) {
            return m as i64
        }
        L = L + p2
    }
    return 0
}

function solve() -> i64 {
    let p: i64 = 1000000007
    let exp_leg: i64 = (p - 1) / 2
    let exp_sqrt: i64 = (p + 1) / 4
    let p2: i128 = (p as i128) * (p as i128)
    let mut total: i64 = 0
    let mut d: i64 = 1
    while d <= 100000 {
        total = total + M_pd(p, d, exp_leg, exp_sqrt, p2)
        d = d + 1
    }
    return total
}

function main() -> i32 {
    printf("%lld\n", solve())
    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; }

__int128 isqrt128_i128(__int128 n);
__int128 ceil_isqrt128_i128(__int128 n);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t M_pd_i64_i64_i64_i64_i128(int64_t p, int64_t d, int64_t exp_leg, int64_t exp_sqrt, __int128 p2);
int64_t solve(void);
int32_t main(void);

__int128 isqrt128_i128(__int128 n) {
    if (n < 2) {
        return n;
    }
    __int128 x = n;
    __int128 y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

__int128 ceil_isqrt128_i128(__int128 n) {
    if (n <= 0) {
        return 0;
    }
    return (isqrt128_i128((n - 1)) + 1);
}

int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base0), (mod));
    int64_t e = exp0;
    while (e > 0) {
        if ((e & 1) != 0) {
            r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
        }
        b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return r;
}

int64_t M_pd_i64_i64_i64_i64_i128(int64_t p, int64_t d, int64_t exp_leg, int64_t exp_sqrt, __int128 p2) {
    int64_t a = (p - d);
    int64_t ls = modpow_i64_i64_i64(a, exp_leg, p);
    if (ls == 1) {
        int64_t r = modpow_i64_i64_i64(a, exp_sqrt, p);
        if (r > (p - r)) {
            r = (p - r);
        }
        return r;
    }
    __int128 dp = (((__int128)(d)) * ((__int128)(p)));
    __int128 L = (p2 - dp);
    __int128 p128 = ((__int128)(p));
    while (1 == 1) {
        __int128 m = ceil_isqrt128_i128(L);
        if ((m * m) <= (L + (p128 - 1))) {
            return ((int64_t)(m));
        }
        L = (L + p2);
    }
    return 0;
}

int64_t solve(void) {
    int64_t p = 1000000007;
    int64_t exp_leg = FLOW_CHECKED_DIV(((p - 1)), (2));
    int64_t exp_sqrt = FLOW_CHECKED_DIV(((p + 1)), (4));
    __int128 p2 = (((__int128)(p)) * ((__int128)(p)));
    int64_t total = 0;
    int64_t d = 1;
    while (d <= 100000) {
        total = (total + M_pd_i64_i64_i64_i64_i128(p, d, exp_leg, exp_sqrt, p2));
        d = (d + 1);
    }
    return total;
}

int32_t main(void) {
    printf("%lld\n", solve());
    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 @isqrt128(%arg0: i128) -> i128 {
    %0 = arith.constant 2 : i32
    %2 = arith.trunci %arg0 : i128 to i64
    %3 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi slt, %2, %3 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      func.return %arg0 : i128
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i128 : (i64) -> !llvm.ptr
    llvm.store %arg0, %5 : i128, !llvm.ptr
    %6 = llvm.load %5 : !llvm.ptr -> i128
    %7 = arith.constant 1 : i32
    %9 = arith.trunci %6 : i128 to i64
    %10 = arith.extsi %7 : i32 to i64
    %8 = arith.addi %9, %10 : i64
    %11 = arith.constant 2 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.divsi %8, %13 : i64
    %14 = arith.extsi %12 : i64 to i128
    %15 = llvm.mlir.constant(1 : i64) : i64
    %16 = llvm.alloca %15 x i128 : (i64) -> !llvm.ptr
    llvm.store %14, %16 : i128, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %17 = llvm.load %16 : !llvm.ptr -> i128
    %18 = llvm.load %5 : !llvm.ptr -> i128
    %20 = arith.trunci %17 : i128 to i64
    %21 = arith.trunci %18 : i128 to i64
    %19 = arith.cmpi slt, %20, %21 : i64
    cf.cond_br %19, ^bb4, ^bb5
    ^bb4:
      %22 = llvm.load %16 : !llvm.ptr -> i128
      llvm.store %22, %5 : i128, !llvm.ptr
      %23 = llvm.load %5 : !llvm.ptr -> i128
      %24 = llvm.load %5 : !llvm.ptr -> i128
      %26 = arith.trunci %arg0 : i128 to i64
      %27 = arith.trunci %24 : i128 to i64
      %25 = arith.divsi %26, %27 : i64
      %29 = arith.trunci %23 : i128 to i64
      %28 = arith.addi %29, %25 : i64
      %30 = arith.constant 2 : i32
      %32 = arith.extsi %30 : i32 to i64
      %31 = arith.divsi %28, %32 : i64
      %33 = arith.extsi %31 : i64 to i128
      llvm.store %33, %16 : i128, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %34 = llvm.load %5 : !llvm.ptr -> i128
    func.return %34 : i128
  }
  func.func @ceil_isqrt128(%arg0: i128) -> i128 {
    %35 = arith.constant 0 : i32
    %37 = arith.trunci %arg0 : i128 to i64
    %38 = arith.extsi %35 : i32 to i64
    %36 = arith.cmpi sle, %37, %38 : i64
    cf.cond_br %36, ^bb6, ^bb7
    ^bb6:
      %39 = arith.constant 0 : i32
      %40 = arith.extsi %39 : i32 to i128
      func.return %40 : i128
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %42 = arith.constant 1 : i32
    %44 = arith.trunci %arg0 : i128 to i64
    %45 = arith.extsi %42 : i32 to i64
    %43 = arith.subi %44, %45 : i64
    %46 = arith.extsi %43 : i64 to i128
    %41 = func.call @isqrt128(%46) : (i128) -> i128
    %47 = arith.constant 1 : i32
    %49 = arith.trunci %41 : i128 to i64
    %50 = arith.extsi %47 : i32 to i64
    %48 = arith.addi %49, %50 : i64
    %51 = arith.extsi %48 : i64 to i128
    func.return %51 : i128
  }
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %52 = arith.constant 1 : i32
    %53 = arith.extsi %52 : i32 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
    %56 = arith.remsi %arg0, %arg2 : i64
    %57 = llvm.mlir.constant(1 : i64) : i64
    %58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
    llvm.store %56, %58 : i64, !llvm.ptr
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %60 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %61 = llvm.load %60 : !llvm.ptr -> i64
    %62 = arith.constant 0 : i32
    %64 = arith.extsi %62 : i32 to i64
    %63 = arith.cmpi sgt, %61, %64 : i64
    cf.cond_br %63, ^bb10, ^bb11
    ^bb10:
      %65 = llvm.load %60 : !llvm.ptr -> i64
      %66 = arith.constant 1 : i32
      %68 = arith.extsi %66 : i32 to i64
      %67 = arith.andi %65, %68 : i64
      %69 = arith.constant 0 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.cmpi ne, %67, %71 : i64
      cf.cond_br %70, ^bb12, ^bb13
      ^bb12:
        %72 = llvm.load %55 : !llvm.ptr -> i64
        %73 = arith.extsi %72 : i64 to i128
        %74 = llvm.load %58 : !llvm.ptr -> i64
        %75 = arith.extsi %74 : i64 to i128
        %77 = arith.trunci %73 : i128 to i64
        %78 = arith.trunci %75 : i128 to i64
        %76 = arith.muli %77, %78 : i64
        %79 = arith.extsi %arg2 : i64 to i128
        %81 = arith.trunci %79 : i128 to i64
        %80 = arith.remsi %76, %81 : i64
        llvm.store %80, %55 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %82 = llvm.load %58 : !llvm.ptr -> i64
      %83 = arith.extsi %82 : i64 to i128
      %84 = llvm.load %58 : !llvm.ptr -> i64
      %85 = arith.extsi %84 : i64 to i128
      %87 = arith.trunci %83 : i128 to i64
      %88 = arith.trunci %85 : i128 to i64
      %86 = arith.muli %87, %88 : i64
      %89 = arith.extsi %arg2 : i64 to i128
      %91 = arith.trunci %89 : i128 to i64
      %90 = arith.remsi %86, %91 : i64
      llvm.store %90, %58 : i64, !llvm.ptr
      %92 = llvm.load %60 : !llvm.ptr -> i64
      %93 = arith.constant 1 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.shrsi %92, %95 : i64
      llvm.store %94, %60 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %96 = llvm.load %55 : !llvm.ptr -> i64
    func.return %96 : i64
  }
  func.func @M_pd(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i64, %arg4: i128) -> i64 {
    %97 = arith.subi %arg0, %arg1 : i64
    %98 = func.call @modpow(%97, %arg2, %arg0) : (i64, i64, i64) -> i64
    %99 = arith.constant 1 : i32
    %101 = arith.extsi %99 : i32 to i64
    %100 = arith.cmpi eq, %98, %101 : i64
    cf.cond_br %100, ^bb15, ^bb16
    ^bb15:
      %102 = func.call @modpow(%97, %arg3, %arg0) : (i64, i64, i64) -> i64
      %103 = llvm.mlir.constant(1 : i64) : i64
      %104 = llvm.alloca %103 x i64 : (i64) -> !llvm.ptr
      llvm.store %102, %104 : i64, !llvm.ptr
      %105 = llvm.load %104 : !llvm.ptr -> i64
      %106 = llvm.load %104 : !llvm.ptr -> i64
      %107 = arith.subi %arg0, %106 : i64
      %108 = arith.cmpi sgt, %105, %107 : i64
      cf.cond_br %108, ^bb18, ^bb19
      ^bb18:
        %109 = llvm.load %104 : !llvm.ptr -> i64
        %110 = arith.subi %arg0, %109 : i64
        llvm.store %110, %104 : i64, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %111 = llvm.load %104 : !llvm.ptr -> i64
      func.return %111 : i64
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %112 = arith.extsi %arg1 : i64 to i128
    %113 = arith.extsi %arg0 : i64 to i128
    %115 = arith.trunci %112 : i128 to i64
    %116 = arith.trunci %113 : i128 to i64
    %114 = arith.muli %115, %116 : i64
    %117 = arith.extsi %114 : i64 to i128
    %119 = arith.trunci %arg4 : i128 to i64
    %120 = arith.trunci %117 : i128 to i64
    %118 = arith.subi %119, %120 : i64
    %121 = arith.extsi %118 : i64 to i128
    %122 = llvm.mlir.constant(1 : i64) : i64
    %123 = llvm.alloca %122 x i128 : (i64) -> !llvm.ptr
    llvm.store %121, %123 : i128, !llvm.ptr
    %124 = arith.extsi %arg0 : i64 to i128
    cf.br ^bb21
    ^bb21:
    %125 = arith.constant 1 : i32
    %126 = arith.constant 1 : i32
    %127 = arith.cmpi eq, %125, %126 : i32
    cf.cond_br %127, ^bb22, ^bb23
    ^bb22:
      %129 = llvm.load %123 : !llvm.ptr -> i128
      %128 = func.call @ceil_isqrt128(%129) : (i128) -> i128
      %131 = arith.trunci %128 : i128 to i64
      %132 = arith.trunci %128 : i128 to i64
      %130 = arith.muli %131, %132 : i64
      %133 = llvm.load %123 : !llvm.ptr -> i128
      %134 = arith.constant 1 : i32
      %136 = arith.trunci %124 : i128 to i64
      %137 = arith.extsi %134 : i32 to i64
      %135 = arith.subi %136, %137 : i64
      %139 = arith.trunci %133 : i128 to i64
      %138 = arith.addi %139, %135 : i64
      %140 = arith.cmpi sle, %130, %138 : i64
      cf.cond_br %140, ^bb24, ^bb25
      ^bb24:
        %141 = arith.trunci %128 : i128 to i64
        func.return %141 : i64
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %142 = llvm.load %123 : !llvm.ptr -> i128
      %144 = arith.trunci %142 : i128 to i64
      %145 = arith.trunci %arg4 : i128 to i64
      %143 = arith.addi %144, %145 : i64
      %146 = arith.extsi %143 : i64 to i128
      llvm.store %146, %123 : i128, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %147 = arith.constant 0 : i32
    %148 = arith.extsi %147 : i32 to i64
    func.return %148 : i64
  }
  func.func @solve() -> i64 {
    %149 = arith.constant 1000000007 : i32
    %150 = arith.extsi %149 : i32 to i64
    %151 = arith.constant 1 : i32
    %153 = arith.extsi %151 : i32 to i64
    %152 = arith.subi %150, %153 : i64
    %154 = arith.constant 2 : i32
    %156 = arith.extsi %154 : i32 to i64
    %155 = arith.divsi %152, %156 : i64
    %157 = arith.constant 1 : i32
    %159 = arith.extsi %157 : i32 to i64
    %158 = arith.addi %150, %159 : i64
    %160 = arith.constant 4 : i32
    %162 = arith.extsi %160 : i32 to i64
    %161 = arith.divsi %158, %162 : i64
    %163 = arith.extsi %150 : i64 to i128
    %164 = arith.extsi %150 : i64 to i128
    %166 = arith.trunci %163 : i128 to i64
    %167 = arith.trunci %164 : i128 to i64
    %165 = arith.muli %166, %167 : i64
    %168 = arith.extsi %165 : i64 to i128
    %169 = arith.constant 0 : i32
    %170 = arith.extsi %169 : i32 to i64
    %171 = llvm.mlir.constant(1 : i64) : i64
    %172 = llvm.alloca %171 x i64 : (i64) -> !llvm.ptr
    llvm.store %170, %172 : i64, !llvm.ptr
    %173 = arith.constant 1 : i32
    %174 = arith.extsi %173 : i32 to i64
    %175 = llvm.mlir.constant(1 : i64) : i64
    %176 = llvm.alloca %175 x i64 : (i64) -> !llvm.ptr
    llvm.store %174, %176 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %177 = llvm.load %176 : !llvm.ptr -> i64
    %178 = arith.constant 100000 : i32
    %180 = arith.extsi %178 : i32 to i64
    %179 = arith.cmpi sle, %177, %180 : i64
    cf.cond_br %179, ^bb28, ^bb29
    ^bb28:
      %181 = llvm.load %172 : !llvm.ptr -> i64
      %183 = llvm.load %176 : !llvm.ptr -> i64
      %182 = func.call @M_pd(%150, %183, %155, %161, %168) : (i64, i64, i64, i64, i128) -> i64
      %184 = arith.addi %181, %182 : i64
      llvm.store %184, %172 : i64, !llvm.ptr
      %185 = llvm.load %176 : !llvm.ptr -> i64
      %186 = arith.constant 1 : i32
      %188 = arith.extsi %186 : i32 to i64
      %187 = arith.addi %185, %188 : i64
      llvm.store %187, %176 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %189 = llvm.load %172 : !llvm.ptr -> i64
    func.return %189 : i64
  }
  func.func @main() -> i32 {
    %190 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %191 = func.call @solve() : () -> i64
    %192 = llvm.call @printf(%190, %191) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %193 = arith.constant 0 : i32
    func.return %193 : i32
  }
}