Problem 160

Last five non-zero digits of 10^12!.

Answer16576
Output16576
StatusPASS
Native helperno
Runtime10 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n)
Space complexityO(1)O(n)
ApproachFlow solutionBig-integer arithmetic
VerdictOptimal

Flow source

# Project Euler 160
# Last five non-zero digits of 10^12!.

function count_factors(end0: i64, n: i64) -> i64 {
    let mut end: i64 = end0
    let mut total: i64 = 0
    while end > 0 {
        end = end / n
        total = total + end
    }
    return total
}

function factorial_coprime(n0: i64) -> i64 {
    let n: i64 = n0 % 100000
    let mut product: i64 = 1
    let mut i: i64 = 1
    while i <= n {
        if (i % 2) != 0 && (i % 5) != 0 {
            product = (i * product) % 100000
        }
        i = i + 1
    }
    return product
}

function odd_factorialish(n: i64) -> i64 {
    if n == 0 { return 1 }
    return (odd_factorialish(n / 5) * factorial_coprime(n)) % 100000
}

function factorialish(n: i64) -> i64 {
    if n == 0 { return 1 }
    # even_factorialish(n) = factorialish(n/2)
    return (factorialish(n / 2) * odd_factorialish(n)) % 100000
}

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut base: i64 = base0 % mod
    let mut exp: i64 = exp0
    let mut r: i64 = 1
    while exp > 0 {
        if (exp & 1) == 1 { r = (r * base) % mod }
        base = (base * base) % mod
        exp = exp >> 1
    }
    return r
}

function factorial_suffix(n: i64) -> i64 {
    let mut twos: i64 = count_factors(n, 2) - count_factors(n, 5)
    if twos >= 2505 {
        twos = (twos - 5) % 2500 + 5
    }
    return (factorialish(n) * modpow(2, twos, 100000)) % 100000
}

function main() -> i32 {
    printf("%lld\n", factorial_suffix(1000000000000))
    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 count_factors_i64_i64(int64_t end0, int64_t n);
int64_t factorial_coprime_i64(int64_t n0);
int64_t odd_factorialish_i64(int64_t n);
int64_t factorialish_i64(int64_t n);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t factorial_suffix_i64(int64_t n);
int32_t main(void);

int64_t count_factors_i64_i64(int64_t end0, int64_t n) {
    int64_t end = end0;
    int64_t total = 0;
    while (end > 0) {
        end = FLOW_CHECKED_DIV((end), (n));
        total = (total + end);
    }
    return total;
}

int64_t factorial_coprime_i64(int64_t n0) {
    int64_t n = FLOW_CHECKED_MOD((n0), (100000));
    int64_t product = 1;
    int64_t i = 1;
    while (i <= n) {
        if ((FLOW_CHECKED_MOD((i), (2)) != 0 && FLOW_CHECKED_MOD((i), (5)) != 0)) {
            product = FLOW_CHECKED_MOD(((i * product)), (100000));
        }
        i = (i + 1);
    }
    return product;
}

int64_t odd_factorialish_i64(int64_t n) {
    if (n == 0) {
        return 1;
    }
    return FLOW_CHECKED_MOD(((odd_factorialish_i64(FLOW_CHECKED_DIV((n), (5))) * factorial_coprime_i64(n))), (100000));
}

int64_t factorialish_i64(int64_t n) {
    if (n == 0) {
        return 1;
    }
    return FLOW_CHECKED_MOD(((factorialish_i64(FLOW_CHECKED_DIV((n), (2))) * odd_factorialish_i64(n))), (100000));
}

int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    int64_t base = FLOW_CHECKED_MOD((base0), (mod));
    int64_t exp = exp0;
    int64_t r = 1;
    while (exp > 0) {
        if ((exp & 1) == 1) {
            r = FLOW_CHECKED_MOD(((r * base)), (mod));
        }
        base = FLOW_CHECKED_MOD(((base * base)), (mod));
        exp = FLOW_CHECKED_SHR((exp), (1));
    }
    return r;
}

int64_t factorial_suffix_i64(int64_t n) {
    int64_t twos = (count_factors_i64_i64(n, 2) - count_factors_i64_i64(n, 5));
    if (twos >= 2505) {
        twos = (FLOW_CHECKED_MOD(((twos - 5)), (2500)) + 5);
    }
    return FLOW_CHECKED_MOD(((factorialish_i64(n) * modpow_i64_i64_i64(2, twos, 100000))), (100000));
}

int32_t main(void) {
    printf("%lld\n", factorial_suffix_i64(1000000000000));
    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 @count_factors(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = arith.constant 0 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %5 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %6 = llvm.load %1 : !llvm.ptr -> i64
    %7 = arith.constant 0 : i32
    %9 = arith.extsi %7 : i32 to i64
    %8 = arith.cmpi sgt, %6, %9 : i64
    cf.cond_br %8, ^bb1, ^bb2
    ^bb1:
      %10 = llvm.load %1 : !llvm.ptr -> i64
      %11 = arith.divsi %10, %arg1 : i64
      llvm.store %11, %1 : i64, !llvm.ptr
      %12 = llvm.load %5 : !llvm.ptr -> i64
      %13 = llvm.load %1 : !llvm.ptr -> i64
      %14 = arith.addi %12, %13 : i64
      llvm.store %14, %5 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %15 = llvm.load %5 : !llvm.ptr -> i64
    func.return %15 : i64
  }
  func.func @factorial_coprime(%arg0: i64) -> i64 {
    %16 = arith.constant 100000 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.remsi %arg0, %18 : i64
    %19 = arith.constant 1 : i32
    %20 = arith.extsi %19 : i32 to i64
    %21 = llvm.mlir.constant(1 : i64) : i64
    %22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
    llvm.store %20, %22 : i64, !llvm.ptr
    %23 = arith.constant 1 : i32
    %24 = arith.extsi %23 : i32 to i64
    %25 = llvm.mlir.constant(1 : i64) : i64
    %26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
    llvm.store %24, %26 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %27 = llvm.load %26 : !llvm.ptr -> i64
    %28 = arith.cmpi sle, %27, %17 : i64
    cf.cond_br %28, ^bb4, ^bb5
    ^bb4:
      %29 = llvm.load %26 : !llvm.ptr -> i64
      %30 = arith.constant 2 : i32
      %32 = arith.extsi %30 : i32 to i64
      %31 = arith.remsi %29, %32 : i64
      %33 = arith.constant 0 : i32
      %35 = arith.extsi %33 : i32 to i64
      %34 = arith.cmpi ne, %31, %35 : i64
      %36 = scf.if %34 -> (i1) {
        %37 = llvm.load %26 : !llvm.ptr -> i64
        %38 = arith.constant 5 : i32
        %40 = arith.extsi %38 : i32 to i64
        %39 = arith.remsi %37, %40 : i64
        %41 = arith.constant 0 : i32
        %43 = arith.extsi %41 : i32 to i64
        %42 = arith.cmpi ne, %39, %43 : i64
        scf.yield %42 : i1
      } else {
        %44 = arith.constant false
        scf.yield %44 : i1
      }
      cf.cond_br %36, ^bb6, ^bb7
      ^bb6:
        %45 = llvm.load %26 : !llvm.ptr -> i64
        %46 = llvm.load %22 : !llvm.ptr -> i64
        %47 = arith.muli %45, %46 : i64
        %48 = arith.constant 100000 : i32
        %50 = arith.extsi %48 : i32 to i64
        %49 = arith.remsi %47, %50 : i64
        llvm.store %49, %22 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %51 = llvm.load %26 : !llvm.ptr -> i64
      %52 = arith.constant 1 : i32
      %54 = arith.extsi %52 : i32 to i64
      %53 = arith.addi %51, %54 : i64
      llvm.store %53, %26 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %55 = llvm.load %22 : !llvm.ptr -> i64
    func.return %55 : i64
  }
  func.func @odd_factorialish(%arg0: i64) -> i64 {
    %56 = arith.constant 0 : i32
    %58 = arith.extsi %56 : i32 to i64
    %57 = arith.cmpi eq, %arg0, %58 : i64
    cf.cond_br %57, ^bb9, ^bb10
    ^bb9:
      %59 = arith.constant 1 : i32
      %60 = arith.extsi %59 : i32 to i64
      func.return %60 : i64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %62 = arith.constant 5 : i32
    %64 = arith.extsi %62 : i32 to i64
    %63 = arith.divsi %arg0, %64 : i64
    %61 = func.call @odd_factorialish(%63) : (i64) -> i64
    %65 = func.call @factorial_coprime(%arg0) : (i64) -> i64
    %66 = arith.muli %61, %65 : i64
    %67 = arith.constant 100000 : i32
    %69 = arith.extsi %67 : i32 to i64
    %68 = arith.remsi %66, %69 : i64
    func.return %68 : i64
  }
  func.func @factorialish(%arg0: i64) -> i64 {
    %70 = arith.constant 0 : i32
    %72 = arith.extsi %70 : i32 to i64
    %71 = arith.cmpi eq, %arg0, %72 : i64
    cf.cond_br %71, ^bb12, ^bb13
    ^bb12:
      %73 = arith.constant 1 : i32
      %74 = arith.extsi %73 : i32 to i64
      func.return %74 : i64
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %76 = arith.constant 2 : i32
    %78 = arith.extsi %76 : i32 to i64
    %77 = arith.divsi %arg0, %78 : i64
    %75 = func.call @factorialish(%77) : (i64) -> i64
    %79 = func.call @odd_factorialish(%arg0) : (i64) -> i64
    %80 = arith.muli %75, %79 : i64
    %81 = arith.constant 100000 : i32
    %83 = arith.extsi %81 : i32 to i64
    %82 = arith.remsi %80, %83 : i64
    func.return %82 : i64
  }
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %84 = arith.remsi %arg0, %arg2 : i64
    %85 = llvm.mlir.constant(1 : i64) : i64
    %86 = llvm.alloca %85 x i64 : (i64) -> !llvm.ptr
    llvm.store %84, %86 : i64, !llvm.ptr
    %87 = llvm.mlir.constant(1 : i64) : i64
    %88 = llvm.alloca %87 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %88 : i64, !llvm.ptr
    %89 = arith.constant 1 : i32
    %90 = arith.extsi %89 : i32 to i64
    %91 = llvm.mlir.constant(1 : i64) : i64
    %92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
    llvm.store %90, %92 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %93 = llvm.load %88 : !llvm.ptr -> i64
    %94 = arith.constant 0 : i32
    %96 = arith.extsi %94 : i32 to i64
    %95 = arith.cmpi sgt, %93, %96 : i64
    cf.cond_br %95, ^bb16, ^bb17
    ^bb16:
      %97 = llvm.load %88 : !llvm.ptr -> i64
      %98 = arith.constant 1 : i32
      %100 = arith.extsi %98 : i32 to i64
      %99 = arith.andi %97, %100 : i64
      %101 = arith.constant 1 : i32
      %103 = arith.extsi %101 : i32 to i64
      %102 = arith.cmpi eq, %99, %103 : i64
      cf.cond_br %102, ^bb18, ^bb19
      ^bb18:
        %104 = llvm.load %92 : !llvm.ptr -> i64
        %105 = llvm.load %86 : !llvm.ptr -> i64
        %106 = arith.muli %104, %105 : i64
        %107 = arith.remsi %106, %arg2 : i64
        llvm.store %107, %92 : i64, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %108 = llvm.load %86 : !llvm.ptr -> i64
      %109 = llvm.load %86 : !llvm.ptr -> i64
      %110 = arith.muli %108, %109 : i64
      %111 = arith.remsi %110, %arg2 : i64
      llvm.store %111, %86 : i64, !llvm.ptr
      %112 = llvm.load %88 : !llvm.ptr -> i64
      %113 = arith.constant 1 : i32
      %115 = arith.extsi %113 : i32 to i64
      %114 = arith.shrsi %112, %115 : i64
      llvm.store %114, %88 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %116 = llvm.load %92 : !llvm.ptr -> i64
    func.return %116 : i64
  }
  func.func @factorial_suffix(%arg0: i64) -> i64 {
    %118 = arith.constant 2 : i32
    %119 = arith.extsi %118 : i32 to i64
    %117 = func.call @count_factors(%arg0, %119) : (i64, i64) -> i64
    %121 = arith.constant 5 : i32
    %122 = arith.extsi %121 : i32 to i64
    %120 = func.call @count_factors(%arg0, %122) : (i64, i64) -> i64
    %123 = arith.subi %117, %120 : i64
    %124 = llvm.mlir.constant(1 : i64) : i64
    %125 = llvm.alloca %124 x i64 : (i64) -> !llvm.ptr
    llvm.store %123, %125 : i64, !llvm.ptr
    %126 = llvm.load %125 : !llvm.ptr -> i64
    %127 = arith.constant 2505 : i32
    %129 = arith.extsi %127 : i32 to i64
    %128 = arith.cmpi sge, %126, %129 : i64
    cf.cond_br %128, ^bb21, ^bb22
    ^bb21:
      %130 = llvm.load %125 : !llvm.ptr -> i64
      %131 = arith.constant 5 : i32
      %133 = arith.extsi %131 : i32 to i64
      %132 = arith.subi %130, %133 : i64
      %134 = arith.constant 2500 : i32
      %136 = arith.extsi %134 : i32 to i64
      %135 = arith.remsi %132, %136 : i64
      %137 = arith.constant 5 : i32
      %139 = arith.extsi %137 : i32 to i64
      %138 = arith.addi %135, %139 : i64
      llvm.store %138, %125 : i64, !llvm.ptr
      cf.br ^bb23
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %140 = func.call @factorialish(%arg0) : (i64) -> i64
    %142 = arith.constant 2 : i32
    %143 = llvm.load %125 : !llvm.ptr -> i64
    %144 = arith.constant 100000 : i32
    %145 = arith.extsi %142 : i32 to i64
    %146 = arith.extsi %144 : i32 to i64
    %141 = func.call @modpow(%145, %143, %146) : (i64, i64, i64) -> i64
    %147 = arith.muli %140, %141 : i64
    %148 = arith.constant 100000 : i32
    %150 = arith.extsi %148 : i32 to i64
    %149 = arith.remsi %147, %150 : i64
    func.return %149 : i64
  }
  func.func @main() -> i32 {
    %151 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %153 = arith.constant 995705032704 : i32
    %154 = arith.extsi %153 : i32 to i64
    %152 = func.call @factorial_suffix(%154) : (i64) -> i64
    %155 = llvm.call @printf(%151, %152) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %156 = arith.constant 0 : i32
    func.return %156 : i32
  }
}