← All problems
Problem 034
Sum of numbers equal to the sum of the factorial of their digits.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n * d)
Space complexity O(1)O(1)
Approach Flow solution Brute-force digit factorial sum
Verdict Unknown
Flow source
# Project Euler 034
# Sum of numbers equal to the sum of the factorial of their digits.
function main() -> i32 {
let fact: array<i64, 10> = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
# Upper bound: 7*9! = 2540160
let mut total: i64 = 0
let mut n: i64 = 3
while n <= 2540160 {
let mut s: i64 = 0
let mut x: i64 = n
while x > 0 {
s = s + fact[(x % 10) as i32]
x = x / 10
}
if s == n {
total = total + n
}
n = n + 1
}
printf("%lld\n", total)
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; }
int32_t main(void);
int32_t main(void) {
int64_t fact[10] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };
int64_t total = 0;
int64_t n = 3;
while (n <= 2540160) {
int64_t s = 0;
int64_t x = n;
while (x > 0) {
s = (s + (((unsigned)(((int32_t)(FLOW_CHECKED_MOD((x), (10))))) < 10) ? fact[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(((int32_t)(FLOW_CHECKED_MOD((x), (10))))), 10), flow_fault_handler("array index out of bounds"), fact[0])));
x = FLOW_CHECKED_DIV((x), (10));
}
if (s == n) {
total = (total + n);
}
n = (n + 1);
}
printf("%lld\n", total);
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 @main() -> i32 {
%1 = arith.constant 1 : i32
%2 = arith.constant 1 : i32
%3 = arith.constant 2 : i32
%4 = arith.constant 6 : i32
%5 = arith.constant 24 : i32
%6 = arith.constant 120 : i32
%7 = arith.constant 720 : i32
%8 = arith.constant 5040 : i32
%9 = arith.constant 40320 : i32
%10 = arith.constant 362880 : i32
%11 = llvm.mlir.constant(1 : i64) : i64
%12 = llvm.alloca %11 x !llvm.array<10 x i64> : (i64) -> !llvm.ptr
%13 = llvm.mlir.zero : !llvm.array<10 x i64>
llvm.store %13, %12 : !llvm.array<10 x i64>, !llvm.ptr
%14 = arith.extsi %1 : i32 to i64
%15 = arith.extsi %2 : i32 to i64
%16 = arith.extsi %3 : i32 to i64
%17 = arith.extsi %4 : i32 to i64
%18 = arith.extsi %5 : i32 to i64
%19 = arith.extsi %6 : i32 to i64
%20 = arith.extsi %7 : i32 to i64
%21 = arith.extsi %8 : i32 to i64
%22 = arith.extsi %9 : i32 to i64
%23 = arith.extsi %10 : i32 to i64
%24 = llvm.mlir.constant(0 : i64) : i64
%25 = llvm.getelementptr %12[0, %24] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %14, %25 : i64, !llvm.ptr
%26 = llvm.mlir.constant(1 : i64) : i64
%27 = llvm.getelementptr %12[0, %26] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %15, %27 : i64, !llvm.ptr
%28 = llvm.mlir.constant(2 : i64) : i64
%29 = llvm.getelementptr %12[0, %28] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %16, %29 : i64, !llvm.ptr
%30 = llvm.mlir.constant(3 : i64) : i64
%31 = llvm.getelementptr %12[0, %30] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %17, %31 : i64, !llvm.ptr
%32 = llvm.mlir.constant(4 : i64) : i64
%33 = llvm.getelementptr %12[0, %32] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %18, %33 : i64, !llvm.ptr
%34 = llvm.mlir.constant(5 : i64) : i64
%35 = llvm.getelementptr %12[0, %34] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %19, %35 : i64, !llvm.ptr
%36 = llvm.mlir.constant(6 : i64) : i64
%37 = llvm.getelementptr %12[0, %36] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %20, %37 : i64, !llvm.ptr
%38 = llvm.mlir.constant(7 : i64) : i64
%39 = llvm.getelementptr %12[0, %38] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %21, %39 : i64, !llvm.ptr
%40 = llvm.mlir.constant(8 : i64) : i64
%41 = llvm.getelementptr %12[0, %40] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %22, %41 : i64, !llvm.ptr
%42 = llvm.mlir.constant(9 : i64) : i64
%43 = llvm.getelementptr %12[0, %42] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
llvm.store %23, %43 : i64, !llvm.ptr
%44 = arith.constant 0 : i32
%45 = arith.extsi %44 : i32 to i64
%46 = llvm.mlir.constant(1 : i64) : i64
%47 = llvm.alloca %46 x i64 : (i64) -> !llvm.ptr
llvm.store %45, %47 : i64, !llvm.ptr
%48 = arith.constant 3 : i32
%49 = arith.extsi %48 : i32 to i64
%50 = llvm.mlir.constant(1 : i64) : i64
%51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
llvm.store %49, %51 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%52 = llvm.load %51 : !llvm.ptr -> i64
%53 = arith.constant 2540160 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.cmpi sle, %52, %55 : i64
cf.cond_br %54, ^bb1, ^bb2
^bb1:
%56 = arith.constant 0 : i32
%57 = arith.extsi %56 : i32 to i64
%58 = llvm.mlir.constant(1 : i64) : i64
%59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
llvm.store %57, %59 : i64, !llvm.ptr
%60 = llvm.load %51 : !llvm.ptr -> i64
%61 = llvm.mlir.constant(1 : i64) : i64
%62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
llvm.store %60, %62 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%63 = llvm.load %62 : !llvm.ptr -> i64
%64 = arith.constant 0 : i32
%66 = arith.extsi %64 : i32 to i64
%65 = arith.cmpi sgt, %63, %66 : i64
cf.cond_br %65, ^bb4, ^bb5
^bb4:
%67 = llvm.load %59 : !llvm.ptr -> i64
%69 = llvm.load %62 : !llvm.ptr -> i64
%70 = arith.constant 10 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.remsi %69, %72 : i64
%73 = arith.trunci %71 : i64 to i32
%74 = arith.extsi %73 : i32 to i64
%75 = llvm.getelementptr %12[0, %74] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i64>
%68 = llvm.load %75 : !llvm.ptr -> i64
%76 = arith.addi %67, %68 : i64
llvm.store %76, %59 : i64, !llvm.ptr
%77 = llvm.load %62 : !llvm.ptr -> i64
%78 = arith.constant 10 : i32
%80 = arith.extsi %78 : i32 to i64
%79 = arith.divsi %77, %80 : i64
llvm.store %79, %62 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%81 = llvm.load %59 : !llvm.ptr -> i64
%82 = llvm.load %51 : !llvm.ptr -> i64
%83 = arith.cmpi eq, %81, %82 : i64
cf.cond_br %83, ^bb6, ^bb7
^bb6:
%84 = llvm.load %47 : !llvm.ptr -> i64
%85 = llvm.load %51 : !llvm.ptr -> i64
%86 = arith.addi %84, %85 : i64
llvm.store %86, %47 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%87 = llvm.load %51 : !llvm.ptr -> i64
%88 = arith.constant 1 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.addi %87, %90 : i64
llvm.store %89, %51 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%91 = llvm.mlir.addressof @str_0 : !llvm.ptr
%92 = llvm.load %47 : !llvm.ptr -> i64
%93 = llvm.call @printf(%91, %92) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%94 = arith.constant 0 : i32
func.return %94 : i32
}
}