Problem 101

Sum of FITs for the degree-10 generating function u_n = 1 - n + n^2 - ... + n^10.

Answer37076114526
Output37076114526
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(1)O(n)
ApproachFlow solutionPolynomial interpolation
VerdictOptimal

Flow source

# Project Euler 101
# Sum of FITs for the degree-10 generating function u_n = 1 - n + n^2 - ... + n^10.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function u(n: i64) -> i64 {
    # sum_{k=0}^{10} (-n)^k
    let mut term: i64 = 1
    let mut s: i64 = 0
    let mut k: i64 = 0
    while k <= 10 {
        s = s + term
        term = term * (-n)
        k = k + 1
    }
    return s
}

# Lagrange interpolation of first k terms, evaluated at x = k+1.
function fit(k: i32, ys: ptr<i64>) -> i64 {
    let x: i64 = (k + 1) as i64
    let mut res: i64 = 0
    let mut i: i32 = 0
    while i < k {
        let mut num: i64 = 1
        let mut den: i64 = 1
        let mut j: i32 = 0
        while j < k {
            if i != j {
                num = num * (x - ((j + 1) as i64))
                den = den * (((i + 1) as i64) - ((j + 1) as i64))
            }
            j = j + 1
        }
        res = res + ys[i] * num / den
        i = i + 1
    }
    return res
}

function main() -> i32 {
    let ys: ptr<i64> = calloc(11, 8)
    if ys == null { return 1 }
    let mut n: i64 = 1
    while n <= 10 {
        ys[n - 1] = u(n)
        n = n + 1
    }

    let mut total: i64 = 0
    let mut k: i32 = 1
    while k <= 10 {
        total = total + fit(k, ys)
        k = k + 1
    }
    printf("%lld\n", total)
    free(ys)
    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 u_i64(int64_t n);
int64_t fit_i32_ptr_i64(int32_t k, int64_t* ys);
int32_t main(void);



int64_t u_i64(int64_t n) {
    int64_t term = 1;
    int64_t s = 0;
    int64_t k = 0;
    while (k <= 10) {
        s = (s + term);
        term = (term * (-n));
        k = (k + 1);
    }
    return s;
}

int64_t fit_i32_ptr_i64(int32_t k, int64_t* ys) {
    int64_t x = ((int64_t)((k + 1)));
    int64_t res = 0;
    int32_t i = 0;
    while (i < k) {
        int64_t num = 1;
        int64_t den = 1;
        int32_t j = 0;
        while (j < k) {
            if (i != j) {
                num = (num * (x - ((int64_t)((j + 1)))));
                den = (den * (((int64_t)((i + 1))) - ((int64_t)((j + 1)))));
            }
            j = (j + 1);
        }
        res = (res + FLOW_CHECKED_DIV(((ys[i] * num)), (den)));
        i = (i + 1);
    }
    return res;
}

int32_t main(void) {
    int64_t* ys = (int64_t*)(calloc(11, 8));
    if (ys == NULL) {
        return 1;
    }
    int64_t n = 1;
    while (n <= 10) {
        ys[(n - 1)] = u_i64(n);
        n = (n + 1);
    }
    int64_t total = 0;
    int32_t k = 1;
    while (k <= 10) {
        total = (total + fit_i32_ptr_i64(k, ys));
        k = (k + 1);
    }
    printf("%lld\n", total);
    free(ys);
    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 private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @u(%arg0: 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.constant 0 : i32
    %5 = arith.extsi %4 : i32 to i64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i64, !llvm.ptr
    %8 = arith.constant 0 : i32
    %9 = arith.extsi %8 : i32 to i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %12 = llvm.load %11 : !llvm.ptr -> i64
    %13 = arith.constant 10 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi sle, %12, %15 : i64
    cf.cond_br %14, ^bb1, ^bb2
    ^bb1:
      %16 = llvm.load %7 : !llvm.ptr -> i64
      %17 = llvm.load %3 : !llvm.ptr -> i64
      %18 = arith.addi %16, %17 : i64
      llvm.store %18, %7 : i64, !llvm.ptr
      %19 = llvm.load %3 : !llvm.ptr -> i64
      %21 = arith.constant 0 : i64
      %20 = arith.subi %21, %arg0 : i64
      %22 = arith.muli %19, %20 : i64
      llvm.store %22, %3 : i64, !llvm.ptr
      %23 = llvm.load %11 : !llvm.ptr -> i64
      %24 = arith.constant 1 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.addi %23, %26 : i64
      llvm.store %25, %11 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %27 = llvm.load %7 : !llvm.ptr -> i64
    func.return %27 : i64
  }
  func.func @fit(%arg0: i32, %arg1: !llvm.ptr) -> i64 {
    %28 = arith.constant 1 : i32
    %29 = arith.addi %arg0, %28 : i32
    %30 = arith.extsi %29 : i32 to i64
    %31 = arith.constant 0 : i32
    %32 = arith.extsi %31 : i32 to i64
    %33 = llvm.mlir.constant(1 : i64) : i64
    %34 = llvm.alloca %33 x i64 : (i64) -> !llvm.ptr
    llvm.store %32, %34 : i64, !llvm.ptr
    %35 = arith.constant 0 : i32
    %36 = llvm.mlir.constant(1 : i64) : i64
    %37 = llvm.alloca %36 x i32 : (i64) -> !llvm.ptr
    llvm.store %35, %37 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %38 = llvm.load %37 : !llvm.ptr -> i32
    %39 = arith.cmpi slt, %38, %arg0 : i32
    cf.cond_br %39, ^bb4, ^bb5
    ^bb4:
      %40 = arith.constant 1 : i32
      %41 = arith.extsi %40 : i32 to i64
      %42 = llvm.mlir.constant(1 : i64) : i64
      %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
      llvm.store %41, %43 : i64, !llvm.ptr
      %44 = arith.constant 1 : 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 0 : i32
      %49 = llvm.mlir.constant(1 : i64) : i64
      %50 = llvm.alloca %49 x i32 : (i64) -> !llvm.ptr
      llvm.store %48, %50 : i32, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %51 = llvm.load %50 : !llvm.ptr -> i32
      %52 = arith.cmpi slt, %51, %arg0 : i32
      cf.cond_br %52, ^bb7, ^bb8
      ^bb7:
        %53 = llvm.load %37 : !llvm.ptr -> i32
        %54 = llvm.load %50 : !llvm.ptr -> i32
        %55 = arith.cmpi ne, %53, %54 : i32
        cf.cond_br %55, ^bb9, ^bb10
        ^bb9:
          %56 = llvm.load %43 : !llvm.ptr -> i64
          %57 = llvm.load %50 : !llvm.ptr -> i32
          %58 = arith.constant 1 : i32
          %59 = arith.addi %57, %58 : i32
          %60 = arith.extsi %59 : i32 to i64
          %61 = arith.subi %30, %60 : i64
          %62 = arith.muli %56, %61 : i64
          llvm.store %62, %43 : i64, !llvm.ptr
          %63 = llvm.load %47 : !llvm.ptr -> i64
          %64 = llvm.load %37 : !llvm.ptr -> i32
          %65 = arith.constant 1 : i32
          %66 = arith.addi %64, %65 : i32
          %67 = arith.extsi %66 : i32 to i64
          %68 = llvm.load %50 : !llvm.ptr -> i32
          %69 = arith.constant 1 : i32
          %70 = arith.addi %68, %69 : i32
          %71 = arith.extsi %70 : i32 to i64
          %72 = arith.subi %67, %71 : i64
          %73 = arith.muli %63, %72 : i64
          llvm.store %73, %47 : i64, !llvm.ptr
          cf.br ^bb11
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %74 = llvm.load %50 : !llvm.ptr -> i32
        %75 = arith.constant 1 : i32
        %76 = arith.addi %74, %75 : i32
        llvm.store %76, %50 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %77 = llvm.load %34 : !llvm.ptr -> i64
      %79 = llvm.load %37 : !llvm.ptr -> i32
      %80 = arith.extsi %79 : i32 to i64
      %81 = llvm.getelementptr %arg1[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %78 = llvm.load %81 : !llvm.ptr -> i64
      %82 = llvm.load %43 : !llvm.ptr -> i64
      %83 = arith.muli %78, %82 : i64
      %84 = llvm.load %47 : !llvm.ptr -> i64
      %85 = arith.divsi %83, %84 : i64
      %86 = arith.addi %77, %85 : i64
      llvm.store %86, %34 : i64, !llvm.ptr
      %87 = llvm.load %37 : !llvm.ptr -> i32
      %88 = arith.constant 1 : i32
      %89 = arith.addi %87, %88 : i32
      llvm.store %89, %37 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %90 = llvm.load %34 : !llvm.ptr -> i64
    func.return %90 : i64
  }
  func.func @main() -> i32 {
    %92 = arith.constant 11 : i32
    %93 = arith.constant 8 : i32
    %94 = arith.extsi %92 : i32 to i64
    %95 = arith.extsi %93 : i32 to i64
    %91 = func.call @calloc(%94, %95) : (i64, i64) -> !llvm.ptr
    %96 = llvm.mlir.zero : !llvm.ptr
    %97 = llvm.icmp "eq" %91, %96 : !llvm.ptr
    cf.cond_br %97, ^bb12, ^bb13
    ^bb12:
      %98 = arith.constant 1 : i32
      func.return %98 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %99 = arith.constant 1 : i32
    %100 = arith.extsi %99 : i32 to i64
    %101 = llvm.mlir.constant(1 : i64) : i64
    %102 = llvm.alloca %101 x i64 : (i64) -> !llvm.ptr
    llvm.store %100, %102 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %103 = llvm.load %102 : !llvm.ptr -> i64
    %104 = arith.constant 10 : i32
    %106 = arith.extsi %104 : i32 to i64
    %105 = arith.cmpi sle, %103, %106 : i64
    cf.cond_br %105, ^bb16, ^bb17
    ^bb16:
      %108 = llvm.load %102 : !llvm.ptr -> i64
      %107 = func.call @u(%108) : (i64) -> i64
      %109 = llvm.load %102 : !llvm.ptr -> i64
      %110 = arith.constant 1 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.subi %109, %112 : i64
      %113 = llvm.getelementptr %91[%111] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %107, %113 : i64, !llvm.ptr
      %114 = llvm.load %102 : !llvm.ptr -> i64
      %115 = arith.constant 1 : i32
      %117 = arith.extsi %115 : i32 to i64
      %116 = arith.addi %114, %117 : i64
      llvm.store %116, %102 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %118 = arith.constant 0 : i32
    %119 = arith.extsi %118 : i32 to i64
    %120 = llvm.mlir.constant(1 : i64) : i64
    %121 = llvm.alloca %120 x i64 : (i64) -> !llvm.ptr
    llvm.store %119, %121 : i64, !llvm.ptr
    %122 = arith.constant 1 : i32
    %123 = llvm.mlir.constant(1 : i64) : i64
    %124 = llvm.alloca %123 x i32 : (i64) -> !llvm.ptr
    llvm.store %122, %124 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %125 = llvm.load %124 : !llvm.ptr -> i32
    %126 = arith.constant 10 : i32
    %127 = arith.cmpi sle, %125, %126 : i32
    cf.cond_br %127, ^bb19, ^bb20
    ^bb19:
      %128 = llvm.load %121 : !llvm.ptr -> i64
      %130 = llvm.load %124 : !llvm.ptr -> i32
      %129 = func.call @fit(%130, %91) : (i32, !llvm.ptr) -> i64
      %131 = arith.addi %128, %129 : i64
      llvm.store %131, %121 : i64, !llvm.ptr
      %132 = llvm.load %124 : !llvm.ptr -> i32
      %133 = arith.constant 1 : i32
      %134 = arith.addi %132, %133 : i32
      llvm.store %134, %124 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %135 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %136 = llvm.load %121 : !llvm.ptr -> i64
    %137 = llvm.call @printf(%135, %136) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%91) : (!llvm.ptr) -> ()
    %139 = arith.constant 0 : i32
    func.return %139 : i32
  }
}