← All problems
Problem 854
Pisano Periods 2 — product of M(p) for p<=1e6 mod 1234567891.
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 854
# Pisano Periods 2 — product of M(p) for p<=1e6 mod 1234567891.
const MOD: i64 = 1234567891
const TARGET: i64 = 1000000
function solve() -> i64 {
let mut res: i64 = 2 % MOD
let max_m: i64 = TARGET / 2
let mut f_prev: i64 = 0
let mut f_cur: i64 = 1
let mut l_prev: i64 = 2 % MOD
let mut l_cur: i64 = 1 % MOD
let mut m: i64 = 2
while m <= max_m {
let f_next: i64 = (f_prev + f_cur) % MOD
f_prev = f_cur
f_cur = f_next
let l_next: i64 = (l_prev + l_cur) % MOD
l_prev = l_cur
l_cur = l_next
if m >= 3 {
if (m & 1) == 0 {
res = ((res as i128) * (f_cur as i128) % (MOD as i128)) as i64
} else {
res = ((res as i128) * (l_cur as i128) % (MOD as i128)) as i64
}
}
m = m + 1
}
return res
}
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; }
int64_t solve(void);
int32_t main(void);
static const int64_t MOD = 1234567891;
static const int64_t TARGET = 1000000;
int64_t solve(void) {
int64_t res = FLOW_CHECKED_MOD((2), (MOD));
int64_t max_m = FLOW_CHECKED_DIV((TARGET), (2));
int64_t f_prev = 0;
int64_t f_cur = 1;
int64_t l_prev = FLOW_CHECKED_MOD((2), (MOD));
int64_t l_cur = FLOW_CHECKED_MOD((1), (MOD));
int64_t m = 2;
while (m <= max_m) {
int64_t f_next = FLOW_CHECKED_MOD(((f_prev + f_cur)), (MOD));
f_prev = f_cur;
f_cur = f_next;
int64_t l_next = FLOW_CHECKED_MOD(((l_prev + l_cur)), (MOD));
l_prev = l_cur;
l_cur = l_next;
if (m >= 3) {
if ((m & 1) == 0) {
res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(res)) * ((__int128)(f_cur)))), (((__int128)(MOD))))));
} else {
res = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(res)) * ((__int128)(l_cur)))), (((__int128)(MOD))))));
}
}
m = (m + 1);
}
return res;
}
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>
// Constant: MOD
llvm.mlir.global internal constant @MOD(1234567891 : i64) : i64
// Constant: TARGET
llvm.mlir.global internal constant @TARGET(1000000 : i64) : i64
func.func @solve() -> i64 {
%0 = arith.constant 2 : i32
%1 = llvm.mlir.addressof @MOD : !llvm.ptr
%2 = llvm.load %1 : !llvm.ptr -> i64
%4 = arith.extsi %0 : i32 to i64
%3 = arith.remsi %4, %2 : i64
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
llvm.store %3, %6 : i64, !llvm.ptr
%7 = llvm.mlir.addressof @TARGET : !llvm.ptr
%8 = llvm.load %7 : !llvm.ptr -> i64
%9 = arith.constant 2 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.divsi %8, %11 : i64
%12 = arith.constant 0 : i32
%13 = arith.extsi %12 : i32 to i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i64, !llvm.ptr
%16 = arith.constant 1 : i32
%17 = arith.extsi %16 : i32 to i64
%18 = llvm.mlir.constant(1 : i64) : i64
%19 = llvm.alloca %18 x i64 : (i64) -> !llvm.ptr
llvm.store %17, %19 : i64, !llvm.ptr
%20 = arith.constant 2 : i32
%21 = llvm.mlir.addressof @MOD : !llvm.ptr
%22 = llvm.load %21 : !llvm.ptr -> i64
%24 = arith.extsi %20 : i32 to i64
%23 = arith.remsi %24, %22 : i64
%25 = llvm.mlir.constant(1 : i64) : i64
%26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
llvm.store %23, %26 : i64, !llvm.ptr
%27 = arith.constant 1 : i32
%28 = llvm.mlir.addressof @MOD : !llvm.ptr
%29 = llvm.load %28 : !llvm.ptr -> i64
%31 = arith.extsi %27 : i32 to i64
%30 = arith.remsi %31, %29 : i64
%32 = llvm.mlir.constant(1 : i64) : i64
%33 = llvm.alloca %32 x i64 : (i64) -> !llvm.ptr
llvm.store %30, %33 : i64, !llvm.ptr
%34 = arith.constant 2 : i32
%35 = arith.extsi %34 : i32 to i64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%38 = llvm.load %37 : !llvm.ptr -> i64
%39 = arith.cmpi sle, %38, %10 : i64
cf.cond_br %39, ^bb1, ^bb2
^bb1:
%40 = llvm.load %15 : !llvm.ptr -> i64
%41 = llvm.load %19 : !llvm.ptr -> i64
%42 = arith.addi %40, %41 : i64
%43 = llvm.mlir.addressof @MOD : !llvm.ptr
%44 = llvm.load %43 : !llvm.ptr -> i64
%45 = arith.remsi %42, %44 : i64
%46 = llvm.load %19 : !llvm.ptr -> i64
llvm.store %46, %15 : i64, !llvm.ptr
llvm.store %45, %19 : i64, !llvm.ptr
%47 = llvm.load %26 : !llvm.ptr -> i64
%48 = llvm.load %33 : !llvm.ptr -> i64
%49 = arith.addi %47, %48 : i64
%50 = llvm.mlir.addressof @MOD : !llvm.ptr
%51 = llvm.load %50 : !llvm.ptr -> i64
%52 = arith.remsi %49, %51 : i64
%53 = llvm.load %33 : !llvm.ptr -> i64
llvm.store %53, %26 : i64, !llvm.ptr
llvm.store %52, %33 : i64, !llvm.ptr
%54 = llvm.load %37 : !llvm.ptr -> i64
%55 = arith.constant 3 : i32
%57 = arith.extsi %55 : i32 to i64
%56 = arith.cmpi sge, %54, %57 : i64
cf.cond_br %56, ^bb3, ^bb4
^bb3:
%58 = llvm.load %37 : !llvm.ptr -> i64
%59 = arith.constant 1 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.andi %58, %61 : i64
%62 = arith.constant 0 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.cmpi eq, %60, %64 : i64
cf.cond_br %63, ^bb6, ^bb7
^bb6:
%65 = llvm.load %6 : !llvm.ptr -> i64
%66 = arith.extsi %65 : i64 to i128
%67 = llvm.load %19 : !llvm.ptr -> i64
%68 = arith.extsi %67 : i64 to i128
%70 = arith.trunci %66 : i128 to i64
%71 = arith.trunci %68 : i128 to i64
%69 = arith.muli %70, %71 : i64
%72 = llvm.mlir.addressof @MOD : !llvm.ptr
%73 = llvm.load %72 : !llvm.ptr -> i64
%74 = arith.extsi %73 : i64 to i128
%76 = arith.trunci %74 : i128 to i64
%75 = arith.remsi %69, %76 : i64
llvm.store %75, %6 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
%77 = llvm.load %6 : !llvm.ptr -> i64
%78 = arith.extsi %77 : i64 to i128
%79 = llvm.load %33 : !llvm.ptr -> i64
%80 = arith.extsi %79 : i64 to i128
%82 = arith.trunci %78 : i128 to i64
%83 = arith.trunci %80 : i128 to i64
%81 = arith.muli %82, %83 : i64
%84 = llvm.mlir.addressof @MOD : !llvm.ptr
%85 = llvm.load %84 : !llvm.ptr -> i64
%86 = arith.extsi %85 : i64 to i128
%88 = arith.trunci %86 : i128 to i64
%87 = arith.remsi %81, %88 : i64
llvm.store %87, %6 : i64, !llvm.ptr
cf.br ^bb8
^bb8:
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%89 = llvm.load %37 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
llvm.store %91, %37 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%93 = llvm.load %6 : !llvm.ptr -> i64
func.return %93 : i64
}
func.func @main() -> i32 {
%94 = llvm.mlir.addressof @str_0 : !llvm.ptr
%95 = func.call @solve() : () -> i64
%96 = llvm.call @printf(%94, %95) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%97 = arith.constant 0 : i32
func.return %97 : i32
}
}