← All problems
Problem 312
C(C(C(10000))) mod 13^8 using period structure of C(n).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(1)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 312
# C(C(C(10000))) mod 13^8 using period structure of C(n).
function modpow(base: i64, exp: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base % mod
let mut e: i64 = exp
while e > 0 {
if e % 2 == 1 { r = (r * b) % mod }
b = (b * b) % mod
e = e / 2
}
return r
}
function C(n: i64, mod: i64) -> i64 {
if n == 1 || n == 2 { return 1 }
if n == 3 { return 8 % mod }
# exponent = (3^(n-2) - 3) / 2
# Need 12^exponent mod mod. Use Euler when gcd(12,mod)>1 carefully via CRT or direct.
# For our mods (6*13^k or 13^8), use recursive C definition with modular mul when n small enough
# after reductions n is small.
let mut c1: i64 = 13824 % mod
if n == 4 { return c1 }
let mut i: i64 = 4
while i < n {
# cn = (3*c1)^3 mod mod
let t: i64 = (3 * c1) % mod
let t2: i64 = (t * t) % mod
c1 = (t2 * t) % mod
i = i + 1
}
return c1
}
function C_formula(n: i64, mod: i64) -> i64 {
if n <= 2 { return 1 }
if n == 3 { return 8 % mod }
# For larger n after period reduction, recursive definition is fine if n is not huge.
# After reductions: n around hundreds to millions — recursive loop OK for n~10^6?
# C(10000) needs 10000 iterations — fine.
return C(n, mod)
}
function main() -> i32 {
let x1: i64 = 10000 % (6 * 13 * 13)
let x2: i64 = C_formula(x1, 6 * 13 * 13 * 13 * 13)
let x3: i64 = C_formula(x2, 6 * 13 * 13 * 13 * 13 * 13 * 13)
let x4: i64 = C_formula(x3, 13 * 13 * 13 * 13 * 13 * 13 * 13 * 13)
printf("%lld\n", x4)
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 modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t C_i64_i64(int64_t n, int64_t mod);
int64_t C_formula_i64_i64(int64_t n, int64_t mod);
int32_t main(void);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * b)), (mod));
}
b = FLOW_CHECKED_MOD(((b * b)), (mod));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
int64_t C_i64_i64(int64_t n, int64_t mod) {
if ((n == 1 || n == 2)) {
return 1;
}
if (n == 3) {
return FLOW_CHECKED_MOD((8), (mod));
}
int64_t c1 = FLOW_CHECKED_MOD((13824), (mod));
if (n == 4) {
return c1;
}
int64_t i = 4;
while (i < n) {
int64_t t = FLOW_CHECKED_MOD(((3 * c1)), (mod));
int64_t t2 = FLOW_CHECKED_MOD(((t * t)), (mod));
c1 = FLOW_CHECKED_MOD(((t2 * t)), (mod));
i = (i + 1);
}
return c1;
}
int64_t C_formula_i64_i64(int64_t n, int64_t mod) {
if (n <= 2) {
return 1;
}
if (n == 3) {
return FLOW_CHECKED_MOD((8), (mod));
}
return C_i64_i64(n, mod);
}
int32_t main(void) {
int64_t x1 = FLOW_CHECKED_MOD((10000), (((6 * 13) * 13)));
int64_t x2 = C_formula_i64_i64(x1, ((((6 * 13) * 13) * 13) * 13));
int64_t x3 = C_formula_i64_i64(x2, ((((((6 * 13) * 13) * 13) * 13) * 13) * 13));
int64_t x4 = C_formula_i64_i64(x3, (((((((13 * 13) * 13) * 13) * 13) * 13) * 13) * 13));
printf("%lld\n", x4);
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 @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
%4 = arith.remsi %arg0, %arg2 : i64
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
llvm.store %4, %6 : i64, !llvm.ptr
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%9 = llvm.load %8 : !llvm.ptr -> i64
%10 = arith.constant 0 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.cmpi sgt, %9, %12 : i64
cf.cond_br %11, ^bb1, ^bb2
^bb1:
%13 = llvm.load %8 : !llvm.ptr -> i64
%14 = arith.constant 2 : i32
%16 = arith.extsi %14 : i32 to i64
%15 = arith.remsi %13, %16 : i64
%17 = arith.constant 1 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi eq, %15, %19 : i64
cf.cond_br %18, ^bb3, ^bb4
^bb3:
%20 = llvm.load %3 : !llvm.ptr -> i64
%21 = llvm.load %6 : !llvm.ptr -> i64
%22 = arith.muli %20, %21 : i64
%23 = arith.remsi %22, %arg2 : i64
llvm.store %23, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%24 = llvm.load %6 : !llvm.ptr -> i64
%25 = llvm.load %6 : !llvm.ptr -> i64
%26 = arith.muli %24, %25 : i64
%27 = arith.remsi %26, %arg2 : i64
llvm.store %27, %6 : i64, !llvm.ptr
%28 = llvm.load %8 : !llvm.ptr -> i64
%29 = arith.constant 2 : i32
%31 = arith.extsi %29 : i32 to i64
%30 = arith.divsi %28, %31 : i64
llvm.store %30, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%32 = llvm.load %3 : !llvm.ptr -> i64
func.return %32 : i64
}
func.func @C(%arg0: i64, %arg1: i64) -> i64 {
%33 = arith.constant 1 : i32
%35 = arith.extsi %33 : i32 to i64
%34 = arith.cmpi eq, %arg0, %35 : i64
%36 = scf.if %34 -> (i1) {
%37 = arith.constant true
scf.yield %37 : i1
} else {
%38 = arith.constant 2 : i32
%40 = arith.extsi %38 : i32 to i64
%39 = arith.cmpi eq, %arg0, %40 : i64
scf.yield %39 : i1
}
cf.cond_br %36, ^bb6, ^bb7
^bb6:
%41 = arith.constant 1 : i32
%42 = arith.extsi %41 : i32 to i64
func.return %42 : i64
^bb7:
cf.br ^bb8
^bb8:
%43 = arith.constant 3 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.cmpi eq, %arg0, %45 : i64
cf.cond_br %44, ^bb9, ^bb10
^bb9:
%46 = arith.constant 8 : i32
%48 = arith.extsi %46 : i32 to i64
%47 = arith.remsi %48, %arg1 : i64
func.return %47 : i64
^bb10:
cf.br ^bb11
^bb11:
%49 = arith.constant 13824 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.remsi %51, %arg1 : i64
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
llvm.store %50, %53 : i64, !llvm.ptr
%54 = arith.constant 4 : i32
%56 = arith.extsi %54 : i32 to i64
%55 = arith.cmpi eq, %arg0, %56 : i64
cf.cond_br %55, ^bb12, ^bb13
^bb12:
%57 = llvm.load %53 : !llvm.ptr -> i64
func.return %57 : i64
^bb13:
cf.br ^bb14
^bb14:
%58 = arith.constant 4 : i32
%59 = arith.extsi %58 : i32 to i64
%60 = llvm.mlir.constant(1 : i64) : i64
%61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
llvm.store %59, %61 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%62 = llvm.load %61 : !llvm.ptr -> i64
%63 = arith.cmpi slt, %62, %arg0 : i64
cf.cond_br %63, ^bb16, ^bb17
^bb16:
%64 = arith.constant 3 : i32
%65 = llvm.load %53 : !llvm.ptr -> i64
%67 = arith.extsi %64 : i32 to i64
%66 = arith.muli %67, %65 : i64
%68 = arith.remsi %66, %arg1 : i64
%69 = arith.muli %68, %68 : i64
%70 = arith.remsi %69, %arg1 : i64
%71 = arith.muli %70, %68 : i64
%72 = arith.remsi %71, %arg1 : i64
llvm.store %72, %53 : i64, !llvm.ptr
%73 = llvm.load %61 : !llvm.ptr -> i64
%74 = arith.constant 1 : i32
%76 = arith.extsi %74 : i32 to i64
%75 = arith.addi %73, %76 : i64
llvm.store %75, %61 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%77 = llvm.load %53 : !llvm.ptr -> i64
func.return %77 : i64
}
func.func @C_formula(%arg0: i64, %arg1: i64) -> i64 {
%78 = arith.constant 2 : i32
%80 = arith.extsi %78 : i32 to i64
%79 = arith.cmpi sle, %arg0, %80 : i64
cf.cond_br %79, ^bb18, ^bb19
^bb18:
%81 = arith.constant 1 : i32
%82 = arith.extsi %81 : i32 to i64
func.return %82 : i64
^bb19:
cf.br ^bb20
^bb20:
%83 = arith.constant 3 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.cmpi eq, %arg0, %85 : i64
cf.cond_br %84, ^bb21, ^bb22
^bb21:
%86 = arith.constant 8 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.remsi %88, %arg1 : i64
func.return %87 : i64
^bb22:
cf.br ^bb23
^bb23:
%89 = func.call @C(%arg0, %arg1) : (i64, i64) -> i64
func.return %89 : i64
}
func.func @main() -> i32 {
%90 = arith.constant 10000 : i32
%91 = arith.constant 6 : i32
%92 = arith.constant 13 : i32
%93 = arith.muli %91, %92 : i32
%94 = arith.constant 13 : i32
%95 = arith.muli %93, %94 : i32
%96 = arith.remsi %90, %95 : i32
%97 = arith.extsi %96 : i32 to i64
%99 = arith.constant 6 : i32
%100 = arith.constant 13 : i32
%101 = arith.muli %99, %100 : i32
%102 = arith.constant 13 : i32
%103 = arith.muli %101, %102 : i32
%104 = arith.constant 13 : i32
%105 = arith.muli %103, %104 : i32
%106 = arith.constant 13 : i32
%107 = arith.muli %105, %106 : i32
%108 = arith.extsi %107 : i32 to i64
%98 = func.call @C_formula(%97, %108) : (i64, i64) -> i64
%110 = arith.constant 6 : i32
%111 = arith.constant 13 : i32
%112 = arith.muli %110, %111 : i32
%113 = arith.constant 13 : i32
%114 = arith.muli %112, %113 : i32
%115 = arith.constant 13 : i32
%116 = arith.muli %114, %115 : i32
%117 = arith.constant 13 : i32
%118 = arith.muli %116, %117 : i32
%119 = arith.constant 13 : i32
%120 = arith.muli %118, %119 : i32
%121 = arith.constant 13 : i32
%122 = arith.muli %120, %121 : i32
%123 = arith.extsi %122 : i32 to i64
%109 = func.call @C_formula(%98, %123) : (i64, i64) -> i64
%125 = arith.constant 13 : i32
%126 = arith.constant 13 : i32
%127 = arith.muli %125, %126 : i32
%128 = arith.constant 13 : i32
%129 = arith.muli %127, %128 : i32
%130 = arith.constant 13 : i32
%131 = arith.muli %129, %130 : i32
%132 = arith.constant 13 : i32
%133 = arith.muli %131, %132 : i32
%134 = arith.constant 13 : i32
%135 = arith.muli %133, %134 : i32
%136 = arith.constant 13 : i32
%137 = arith.muli %135, %136 : i32
%138 = arith.constant 13 : i32
%139 = arith.muli %137, %138 : i32
%140 = arith.extsi %139 : i32 to i64
%124 = func.call @C_formula(%109, %140) : (i64, i64) -> i64
%141 = llvm.mlir.addressof @str_0 : !llvm.ptr
%142 = llvm.call @printf(%141, %124) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%143 = arith.constant 0 : i32
func.return %143 : i32
}
}