← All problems
Problem 401
SIGMA2(10^15) mod 10^9, where SIGMA2 = summatory of sigma_2.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n log log n)
Space complexity O(1)O(n)
Approach Flow solution Sieve-based divisor sums
Verdict Optimal
Flow source
# Project Euler 401
# SIGMA2(10^15) mod 10^9, where SIGMA2 = summatory of sigma_2.
function mulmod(a: i64, b: i64, mod: i64) -> i64 {
let x: i128 = (a % mod) as i128
let y: i128 = (b % mod) as i128
let r: i128 = (x * y) % (mod as i128)
if r < (0 as i128) {
return (r + (mod as i128)) as i64
}
return r as i64
}
# 1^2 + ... + n^2 = n(n+1)(2n+1)/6, computed mod `mod`.
function sum_sq_mod(n0: i64, mod: i64) -> i64 {
if n0 <= 0 {
return 0
}
let mut a: i64 = n0
let mut b: i64 = n0 + 1
let mut c: i64 = 2 * n0 + 1
if a % 2 == 0 {
a = a / 2
} elif b % 2 == 0 {
b = b / 2
} else {
c = c / 2
}
if a % 3 == 0 {
a = a / 3
} elif b % 3 == 0 {
b = b / 3
} else {
c = c / 3
}
return mulmod(mulmod(a, b, mod), c, mod)
}
function main() -> i32 {
let N: i64 = 1000000000000000
let MOD: i64 = 1000000000
let mut ans: i64 = 0
let mut l: i64 = 1
while l <= N {
let q: i64 = N / l
let r: i64 = N / q
let mut block: i64 = sum_sq_mod(r, MOD) - sum_sq_mod(l - 1, MOD)
if block < 0 {
block = block + MOD
}
ans = ans + mulmod(q % MOD, block, MOD)
if ans >= MOD {
ans = ans % MOD
}
l = r + 1
}
printf("%lld\n", ans)
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; }
int64_t mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t mod);
int64_t sum_sq_mod_i64_i64(int64_t n0, int64_t mod);
int32_t main(void);
int64_t mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
__int128 x = ((__int128)(FLOW_CHECKED_MOD((a), (mod))));
__int128 y = ((__int128)(FLOW_CHECKED_MOD((b), (mod))));
__int128 r = FLOW_CHECKED_MOD(((x * y)), (((__int128)(mod))));
if (r < ((__int128)(0))) {
return ((int64_t)((r + ((__int128)(mod)))));
}
return ((int64_t)(r));
}
int64_t sum_sq_mod_i64_i64(int64_t n0, int64_t mod) {
if (n0 <= 0) {
return 0;
}
int64_t a = n0;
int64_t b = (n0 + 1);
int64_t c = ((2 * n0) + 1);
if (FLOW_CHECKED_MOD((a), (2)) == 0) {
a = FLOW_CHECKED_DIV((a), (2));
} else if (FLOW_CHECKED_MOD((b), (2)) == 0) {
b = FLOW_CHECKED_DIV((b), (2));
} else {
c = FLOW_CHECKED_DIV((c), (2));
}
if (FLOW_CHECKED_MOD((a), (3)) == 0) {
a = FLOW_CHECKED_DIV((a), (3));
} else if (FLOW_CHECKED_MOD((b), (3)) == 0) {
b = FLOW_CHECKED_DIV((b), (3));
} else {
c = FLOW_CHECKED_DIV((c), (3));
}
return mulmod_i64_i64_i64(mulmod_i64_i64_i64(a, b, mod), c, mod);
}
int32_t main(void) {
int64_t N = 1000000000000000;
int64_t MOD = 1000000000;
int64_t ans = 0;
int64_t l = 1;
while (l <= N) {
int64_t q = FLOW_CHECKED_DIV((N), (l));
int64_t r = FLOW_CHECKED_DIV((N), (q));
int64_t block = (sum_sq_mod_i64_i64(r, MOD) - sum_sq_mod_i64_i64((l - 1), MOD));
if (block < 0) {
block = (block + MOD);
}
ans = (ans + mulmod_i64_i64_i64(FLOW_CHECKED_MOD((q), (MOD)), block, MOD));
if (ans >= MOD) {
ans = FLOW_CHECKED_MOD((ans), (MOD));
}
l = (r + 1);
}
printf("%lld\n", ans);
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 @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.remsi %arg0, %arg2 : i64
%1 = arith.extsi %0 : i64 to i128
%2 = arith.remsi %arg1, %arg2 : i64
%3 = arith.extsi %2 : i64 to i128
%5 = arith.trunci %1 : i128 to i64
%6 = arith.trunci %3 : i128 to i64
%4 = arith.muli %5, %6 : i64
%7 = arith.extsi %arg2 : i64 to i128
%9 = arith.trunci %7 : i128 to i64
%8 = arith.remsi %4, %9 : i64
%10 = arith.extsi %8 : i64 to i128
%11 = arith.constant 0 : i32
%12 = arith.extsi %11 : i32 to i128
%14 = arith.trunci %10 : i128 to i64
%15 = arith.trunci %12 : i128 to i64
%13 = arith.cmpi slt, %14, %15 : i64
cf.cond_br %13, ^bb0, ^bb1
^bb0:
%16 = arith.extsi %arg2 : i64 to i128
%18 = arith.trunci %10 : i128 to i64
%19 = arith.trunci %16 : i128 to i64
%17 = arith.addi %18, %19 : i64
func.return %17 : i64
^bb1:
cf.br ^bb2
^bb2:
%20 = arith.trunci %10 : i128 to i64
func.return %20 : i64
}
func.func @sum_sq_mod(%arg0: i64, %arg1: i64) -> i64 {
%21 = arith.constant 0 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.cmpi sle, %arg0, %23 : i64
cf.cond_br %22, ^bb3, ^bb4
^bb3:
%24 = arith.constant 0 : i32
%25 = arith.extsi %24 : i32 to i64
func.return %25 : i64
^bb4:
cf.br ^bb5
^bb5:
%26 = llvm.mlir.constant(1 : i64) : i64
%27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %27 : i64, !llvm.ptr
%28 = arith.constant 1 : i32
%30 = arith.extsi %28 : i32 to i64
%29 = arith.addi %arg0, %30 : i64
%31 = llvm.mlir.constant(1 : i64) : i64
%32 = llvm.alloca %31 x i64 : (i64) -> !llvm.ptr
llvm.store %29, %32 : i64, !llvm.ptr
%33 = arith.constant 2 : i32
%35 = arith.extsi %33 : i32 to i64
%34 = arith.muli %35, %arg0 : i64
%36 = arith.constant 1 : i32
%38 = arith.extsi %36 : i32 to i64
%37 = arith.addi %34, %38 : i64
%39 = llvm.mlir.constant(1 : i64) : i64
%40 = llvm.alloca %39 x i64 : (i64) -> !llvm.ptr
llvm.store %37, %40 : i64, !llvm.ptr
%41 = llvm.load %27 : !llvm.ptr -> i64
%42 = arith.constant 2 : i32
%44 = arith.extsi %42 : i32 to i64
%43 = arith.remsi %41, %44 : i64
%45 = arith.constant 0 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.cmpi eq, %43, %47 : i64
cf.cond_br %46, ^bb6, ^bb7
^bb6:
%48 = llvm.load %27 : !llvm.ptr -> i64
%49 = arith.constant 2 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.divsi %48, %51 : i64
llvm.store %50, %27 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
%52 = llvm.load %32 : !llvm.ptr -> i64
%53 = arith.constant 2 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.remsi %52, %55 : i64
%56 = arith.constant 0 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.cmpi eq, %54, %58 : i64
cf.cond_br %57, ^bb10, ^bb9
^bb10:
%59 = llvm.load %32 : !llvm.ptr -> i64
%60 = arith.constant 2 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.divsi %59, %62 : i64
llvm.store %61, %32 : i64, !llvm.ptr
cf.br ^bb8
^bb9:
%63 = llvm.load %40 : !llvm.ptr -> i64
%64 = arith.constant 2 : i32
%66 = arith.extsi %64 : i32 to i64
%65 = arith.divsi %63, %66 : i64
llvm.store %65, %40 : i64, !llvm.ptr
cf.br ^bb8
^bb8:
%67 = llvm.load %27 : !llvm.ptr -> i64
%68 = arith.constant 3 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.remsi %67, %70 : i64
%71 = arith.constant 0 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.cmpi eq, %69, %73 : i64
cf.cond_br %72, ^bb11, ^bb12
^bb11:
%74 = llvm.load %27 : !llvm.ptr -> i64
%75 = arith.constant 3 : i32
%77 = arith.extsi %75 : i32 to i64
%76 = arith.divsi %74, %77 : i64
llvm.store %76, %27 : i64, !llvm.ptr
cf.br ^bb13
^bb12:
%78 = llvm.load %32 : !llvm.ptr -> i64
%79 = arith.constant 3 : i32
%81 = arith.extsi %79 : i32 to i64
%80 = arith.remsi %78, %81 : i64
%82 = arith.constant 0 : i32
%84 = arith.extsi %82 : i32 to i64
%83 = arith.cmpi eq, %80, %84 : i64
cf.cond_br %83, ^bb15, ^bb14
^bb15:
%85 = llvm.load %32 : !llvm.ptr -> i64
%86 = arith.constant 3 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.divsi %85, %88 : i64
llvm.store %87, %32 : i64, !llvm.ptr
cf.br ^bb13
^bb14:
%89 = llvm.load %40 : !llvm.ptr -> i64
%90 = arith.constant 3 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.divsi %89, %92 : i64
llvm.store %91, %40 : i64, !llvm.ptr
cf.br ^bb13
^bb13:
%95 = llvm.load %27 : !llvm.ptr -> i64
%96 = llvm.load %32 : !llvm.ptr -> i64
%94 = func.call @mulmod(%95, %96, %arg1) : (i64, i64, i64) -> i64
%97 = llvm.load %40 : !llvm.ptr -> i64
%93 = func.call @mulmod(%94, %97, %arg1) : (i64, i64, i64) -> i64
func.return %93 : i64
}
func.func @main() -> i32 {
%98 = arith.constant 999995705032704 : i32
%99 = arith.extsi %98 : i32 to i64
%100 = arith.constant 1000000000 : i32
%101 = arith.extsi %100 : i32 to i64
%102 = arith.constant 0 : i32
%103 = arith.extsi %102 : i32 to i64
%104 = llvm.mlir.constant(1 : i64) : i64
%105 = llvm.alloca %104 x i64 : (i64) -> !llvm.ptr
llvm.store %103, %105 : i64, !llvm.ptr
%106 = arith.constant 1 : i32
%107 = arith.extsi %106 : i32 to i64
%108 = llvm.mlir.constant(1 : i64) : i64
%109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
llvm.store %107, %109 : i64, !llvm.ptr
cf.br ^bb16
^bb16:
%110 = llvm.load %109 : !llvm.ptr -> i64
%111 = arith.cmpi sle, %110, %99 : i64
cf.cond_br %111, ^bb17, ^bb18
^bb17:
%112 = llvm.load %109 : !llvm.ptr -> i64
%113 = arith.divsi %99, %112 : i64
%114 = arith.divsi %99, %113 : i64
%115 = func.call @sum_sq_mod(%114, %101) : (i64, i64) -> i64
%117 = llvm.load %109 : !llvm.ptr -> i64
%118 = arith.constant 1 : i32
%120 = arith.extsi %118 : i32 to i64
%119 = arith.subi %117, %120 : i64
%116 = func.call @sum_sq_mod(%119, %101) : (i64, i64) -> i64
%121 = arith.subi %115, %116 : i64
%122 = llvm.mlir.constant(1 : i64) : i64
%123 = llvm.alloca %122 x i64 : (i64) -> !llvm.ptr
llvm.store %121, %123 : i64, !llvm.ptr
%124 = llvm.load %123 : !llvm.ptr -> i64
%125 = arith.constant 0 : i32
%127 = arith.extsi %125 : i32 to i64
%126 = arith.cmpi slt, %124, %127 : i64
cf.cond_br %126, ^bb19, ^bb20
^bb19:
%128 = llvm.load %123 : !llvm.ptr -> i64
%129 = arith.addi %128, %101 : i64
llvm.store %129, %123 : i64, !llvm.ptr
cf.br ^bb21
^bb20:
cf.br ^bb21
^bb21:
%130 = llvm.load %105 : !llvm.ptr -> i64
%132 = arith.remsi %113, %101 : i64
%133 = llvm.load %123 : !llvm.ptr -> i64
%131 = func.call @mulmod(%132, %133, %101) : (i64, i64, i64) -> i64
%134 = arith.addi %130, %131 : i64
llvm.store %134, %105 : i64, !llvm.ptr
%135 = llvm.load %105 : !llvm.ptr -> i64
%136 = arith.cmpi sge, %135, %101 : i64
cf.cond_br %136, ^bb22, ^bb23
^bb22:
%137 = llvm.load %105 : !llvm.ptr -> i64
%138 = arith.remsi %137, %101 : i64
llvm.store %138, %105 : i64, !llvm.ptr
cf.br ^bb24
^bb23:
cf.br ^bb24
^bb24:
%139 = arith.constant 1 : i32
%141 = arith.extsi %139 : i32 to i64
%140 = arith.addi %114, %141 : i64
llvm.store %140, %109 : i64, !llvm.ptr
cf.br ^bb16
^bb18:
%142 = llvm.mlir.addressof @str_0 : !llvm.ptr
%143 = llvm.load %105 : !llvm.ptr -> i64
%144 = llvm.call @printf(%142, %143) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%145 = arith.constant 0 : i32
func.return %145 : i32
}
}