Problem 017

Letters used writing 1..1000 in British English (no spaces/hyphens).

Answer21124
Output21124
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(1)
Space complexityO(1)O(1)
ApproachFlow solutionDirect word count
VerdictSuboptimal

Flow source

# Project Euler 017
# Letters used writing 1..1000 in British English (no spaces/hyphens).

function letters_under_100(n: i32) -> i32 {
    # 1..19
    let ones: array<i32, 20> = [
        0, 3, 3, 5, 4, 4, 3, 5, 5, 4,
        3, 6, 6, 8, 8, 7, 7, 9, 8, 8
    ]
    # 20,30,...,90
    let tens: array<i32, 10> = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]

    if n < 20 {
        return ones[n]
    }
    return tens[n / 10] + ones[n % 10]
}

function letters(n: i32) -> i32 {
    if n == 1000 {
        return 11  # "onethousand"
    }
    let mut total: i32 = 0
    let hundreds: i32 = n / 100
    let rest: i32 = n % 100
    if hundreds > 0 {
        total = total + letters_under_100(hundreds) + 7  # "hundred"
        if rest > 0 {
            total = total + 3  # "and"
        }
    }
    total = total + letters_under_100(rest)
    return total
}

function main() -> i32 {
    let mut total: i64 = 0
    for n in 1..1001 {
        total = total + (letters(n) as i64)
    }
    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 letters_under_100_i32(int32_t n);
int32_t letters_i32(int32_t n);
int32_t main(void);

int32_t letters_under_100_i32(int32_t n) {
    int32_t ones[20] = { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };
    int32_t tens[10] = { 0, 0, 6, 6, 5, 5, 5, 7, 6, 6 };
    if (n < 20) {
        return (((unsigned)(n) < 20) ? ones[n] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(n), 20), flow_fault_handler("array index out of bounds"), ones[0]));
    }
    return ((((unsigned)(FLOW_CHECKED_DIV((n), (10))) < 10) ? tens[FLOW_CHECKED_DIV((n), (10))] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(FLOW_CHECKED_DIV((n), (10))), 10), flow_fault_handler("array index out of bounds"), tens[0])) + (((unsigned)(FLOW_CHECKED_MOD((n), (10))) < 20) ? ones[FLOW_CHECKED_MOD((n), (10))] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(FLOW_CHECKED_MOD((n), (10))), 20), flow_fault_handler("array index out of bounds"), ones[0])));
}

int32_t letters_i32(int32_t n) {
    if (n == 1000) {
        return 11;
    }
    int32_t total = 0;
    int32_t hundreds = FLOW_CHECKED_DIV((n), (100));
    int32_t rest = FLOW_CHECKED_MOD((n), (100));
    if (hundreds > 0) {
        total = ((total + letters_under_100_i32(hundreds)) + 7);
        if (rest > 0) {
            total = (total + 3);
        }
    }
    total = (total + letters_under_100_i32(rest));
    return total;
}

int32_t main(void) {
    int64_t total = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t n = 1; (1 <= 1001) ? n < 1001 : n > 1001; n += (1 <= 1001) ? 1 : -1) {
        total = (total + ((int64_t)(letters_i32(n))));
    }
    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 @letters_under_100(%arg0: i32) -> i32 {
    %1 = arith.constant 0 : i32
    %2 = arith.constant 3 : i32
    %3 = arith.constant 3 : i32
    %4 = arith.constant 5 : i32
    %5 = arith.constant 4 : i32
    %6 = arith.constant 4 : i32
    %7 = arith.constant 3 : i32
    %8 = arith.constant 5 : i32
    %9 = arith.constant 5 : i32
    %10 = arith.constant 4 : i32
    %11 = arith.constant 3 : i32
    %12 = arith.constant 6 : i32
    %13 = arith.constant 6 : i32
    %14 = arith.constant 8 : i32
    %15 = arith.constant 8 : i32
    %16 = arith.constant 7 : i32
    %17 = arith.constant 7 : i32
    %18 = arith.constant 9 : i32
    %19 = arith.constant 8 : i32
    %20 = arith.constant 8 : i32
    %21 = llvm.mlir.constant(1 : i64) : i64
    %22 = llvm.alloca %21 x !llvm.array<20 x i32> : (i64) -> !llvm.ptr
    %23 = llvm.mlir.zero : !llvm.array<20 x i32>
    llvm.store %23, %22 : !llvm.array<20 x i32>, !llvm.ptr
    %24 = llvm.mlir.constant(0 : i64) : i64
    %25 = llvm.getelementptr %22[0, %24] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %1, %25 : i32, !llvm.ptr
    %26 = llvm.mlir.constant(1 : i64) : i64
    %27 = llvm.getelementptr %22[0, %26] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %2, %27 : i32, !llvm.ptr
    %28 = llvm.mlir.constant(2 : i64) : i64
    %29 = llvm.getelementptr %22[0, %28] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %3, %29 : i32, !llvm.ptr
    %30 = llvm.mlir.constant(3 : i64) : i64
    %31 = llvm.getelementptr %22[0, %30] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %4, %31 : i32, !llvm.ptr
    %32 = llvm.mlir.constant(4 : i64) : i64
    %33 = llvm.getelementptr %22[0, %32] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %5, %33 : i32, !llvm.ptr
    %34 = llvm.mlir.constant(5 : i64) : i64
    %35 = llvm.getelementptr %22[0, %34] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %6, %35 : i32, !llvm.ptr
    %36 = llvm.mlir.constant(6 : i64) : i64
    %37 = llvm.getelementptr %22[0, %36] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %7, %37 : i32, !llvm.ptr
    %38 = llvm.mlir.constant(7 : i64) : i64
    %39 = llvm.getelementptr %22[0, %38] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %8, %39 : i32, !llvm.ptr
    %40 = llvm.mlir.constant(8 : i64) : i64
    %41 = llvm.getelementptr %22[0, %40] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %9, %41 : i32, !llvm.ptr
    %42 = llvm.mlir.constant(9 : i64) : i64
    %43 = llvm.getelementptr %22[0, %42] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %10, %43 : i32, !llvm.ptr
    %44 = llvm.mlir.constant(10 : i64) : i64
    %45 = llvm.getelementptr %22[0, %44] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %11, %45 : i32, !llvm.ptr
    %46 = llvm.mlir.constant(11 : i64) : i64
    %47 = llvm.getelementptr %22[0, %46] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %12, %47 : i32, !llvm.ptr
    %48 = llvm.mlir.constant(12 : i64) : i64
    %49 = llvm.getelementptr %22[0, %48] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %13, %49 : i32, !llvm.ptr
    %50 = llvm.mlir.constant(13 : i64) : i64
    %51 = llvm.getelementptr %22[0, %50] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %14, %51 : i32, !llvm.ptr
    %52 = llvm.mlir.constant(14 : i64) : i64
    %53 = llvm.getelementptr %22[0, %52] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %15, %53 : i32, !llvm.ptr
    %54 = llvm.mlir.constant(15 : i64) : i64
    %55 = llvm.getelementptr %22[0, %54] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %16, %55 : i32, !llvm.ptr
    %56 = llvm.mlir.constant(16 : i64) : i64
    %57 = llvm.getelementptr %22[0, %56] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %17, %57 : i32, !llvm.ptr
    %58 = llvm.mlir.constant(17 : i64) : i64
    %59 = llvm.getelementptr %22[0, %58] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %18, %59 : i32, !llvm.ptr
    %60 = llvm.mlir.constant(18 : i64) : i64
    %61 = llvm.getelementptr %22[0, %60] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %19, %61 : i32, !llvm.ptr
    %62 = llvm.mlir.constant(19 : i64) : i64
    %63 = llvm.getelementptr %22[0, %62] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    llvm.store %20, %63 : i32, !llvm.ptr
    %65 = arith.constant 0 : i32
    %66 = arith.constant 0 : i32
    %67 = arith.constant 6 : i32
    %68 = arith.constant 6 : i32
    %69 = arith.constant 5 : i32
    %70 = arith.constant 5 : i32
    %71 = arith.constant 5 : i32
    %72 = arith.constant 7 : i32
    %73 = arith.constant 6 : i32
    %74 = arith.constant 6 : i32
    %75 = llvm.mlir.constant(1 : i64) : i64
    %76 = llvm.alloca %75 x !llvm.array<10 x i32> : (i64) -> !llvm.ptr
    %77 = llvm.mlir.zero : !llvm.array<10 x i32>
    llvm.store %77, %76 : !llvm.array<10 x i32>, !llvm.ptr
    %78 = llvm.mlir.constant(0 : i64) : i64
    %79 = llvm.getelementptr %76[0, %78] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %65, %79 : i32, !llvm.ptr
    %80 = llvm.mlir.constant(1 : i64) : i64
    %81 = llvm.getelementptr %76[0, %80] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %66, %81 : i32, !llvm.ptr
    %82 = llvm.mlir.constant(2 : i64) : i64
    %83 = llvm.getelementptr %76[0, %82] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %67, %83 : i32, !llvm.ptr
    %84 = llvm.mlir.constant(3 : i64) : i64
    %85 = llvm.getelementptr %76[0, %84] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %68, %85 : i32, !llvm.ptr
    %86 = llvm.mlir.constant(4 : i64) : i64
    %87 = llvm.getelementptr %76[0, %86] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %69, %87 : i32, !llvm.ptr
    %88 = llvm.mlir.constant(5 : i64) : i64
    %89 = llvm.getelementptr %76[0, %88] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %70, %89 : i32, !llvm.ptr
    %90 = llvm.mlir.constant(6 : i64) : i64
    %91 = llvm.getelementptr %76[0, %90] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %71, %91 : i32, !llvm.ptr
    %92 = llvm.mlir.constant(7 : i64) : i64
    %93 = llvm.getelementptr %76[0, %92] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %72, %93 : i32, !llvm.ptr
    %94 = llvm.mlir.constant(8 : i64) : i64
    %95 = llvm.getelementptr %76[0, %94] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %73, %95 : i32, !llvm.ptr
    %96 = llvm.mlir.constant(9 : i64) : i64
    %97 = llvm.getelementptr %76[0, %96] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    llvm.store %74, %97 : i32, !llvm.ptr
    %98 = arith.constant 20 : i32
    %99 = arith.cmpi slt, %arg0, %98 : i32
    cf.cond_br %99, ^bb0, ^bb1
    ^bb0:
      %101 = arith.extsi %arg0 : i32 to i64
      %102 = llvm.getelementptr %22[0, %101] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
      %100 = llvm.load %102 : !llvm.ptr -> i32
      func.return %100 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %104 = arith.constant 10 : i32
    %105 = arith.divsi %arg0, %104 : i32
    %106 = arith.extsi %105 : i32 to i64
    %107 = llvm.getelementptr %76[0, %106] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<10 x i32>
    %103 = llvm.load %107 : !llvm.ptr -> i32
    %109 = arith.constant 10 : i32
    %110 = arith.remsi %arg0, %109 : i32
    %111 = arith.extsi %110 : i32 to i64
    %112 = llvm.getelementptr %22[0, %111] : (!llvm.ptr, i64) -> !llvm.ptr, !llvm.array<20 x i32>
    %108 = llvm.load %112 : !llvm.ptr -> i32
    %113 = arith.addi %103, %108 : i32
    func.return %113 : i32
  }
  func.func @letters(%arg0: i32) -> i32 {
    %114 = arith.constant 1000 : i32
    %115 = arith.cmpi eq, %arg0, %114 : i32
    cf.cond_br %115, ^bb3, ^bb4
    ^bb3:
      %116 = arith.constant 11 : i32
      func.return %116 : i32
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %117 = arith.constant 0 : i32
    %118 = llvm.mlir.constant(1 : i64) : i64
    %119 = llvm.alloca %118 x i32 : (i64) -> !llvm.ptr
    llvm.store %117, %119 : i32, !llvm.ptr
    %120 = arith.constant 100 : i32
    %121 = arith.divsi %arg0, %120 : i32
    %122 = arith.constant 100 : i32
    %123 = arith.remsi %arg0, %122 : i32
    %124 = arith.constant 0 : i32
    %125 = arith.cmpi sgt, %121, %124 : i32
    cf.cond_br %125, ^bb6, ^bb7
    ^bb6:
      %126 = llvm.load %119 : !llvm.ptr -> i32
      %127 = func.call @letters_under_100(%121) : (i32) -> i32
      %128 = arith.addi %126, %127 : i32
      %129 = arith.constant 7 : i32
      %130 = arith.addi %128, %129 : i32
      llvm.store %130, %119 : i32, !llvm.ptr
      %131 = arith.constant 0 : i32
      %132 = arith.cmpi sgt, %123, %131 : i32
      cf.cond_br %132, ^bb9, ^bb10
      ^bb9:
        %133 = llvm.load %119 : !llvm.ptr -> i32
        %134 = arith.constant 3 : i32
        %135 = arith.addi %133, %134 : i32
        llvm.store %135, %119 : i32, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      cf.br ^bb8
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %136 = llvm.load %119 : !llvm.ptr -> i32
    %137 = func.call @letters_under_100(%123) : (i32) -> i32
    %138 = arith.addi %136, %137 : i32
    llvm.store %138, %119 : i32, !llvm.ptr
    %139 = llvm.load %119 : !llvm.ptr -> i32
    func.return %139 : i32
  }
  func.func @main() -> i32 {
    %140 = arith.constant 0 : i32
    %141 = arith.extsi %140 : i32 to i64
    %142 = llvm.mlir.constant(1 : i64) : i64
    %143 = llvm.alloca %142 x i64 : (i64) -> !llvm.ptr
    llvm.store %141, %143 : i64, !llvm.ptr
    %144 = arith.constant 1 : i32
    %145 = arith.constant 1001 : i32
    %146 = arith.index_cast %144 : i32 to index
    %147 = arith.index_cast %145 : i32 to index
    %149 = arith.constant 1 : index
    %150 = arith.constant -1 : index
    %151 = arith.cmpi sle, %146, %147 : index
    %148 = arith.select %151, %149, %150 : index
    cf.br ^bb12(%146 : index)
    ^bb12(%152: index):
    %153 = arith.cmpi slt, %152, %147 : index
    %154 = arith.cmpi sgt, %152, %147 : index
    %155 = arith.select %151, %153, %154 : i1
    cf.cond_br %155, ^bb13(%152 : index), ^bb14(%152 : index)
    ^bb13(%156: index):
      %157 = llvm.load %143 : !llvm.ptr -> i64
      %159 = arith.index_cast %156 : index to i32
      %158 = func.call @letters(%159) : (i32) -> i32
      %160 = arith.extsi %158 : i32 to i64
      %161 = arith.addi %157, %160 : i64
      llvm.store %161, %143 : i64, !llvm.ptr
      %162 = arith.addi %156, %148 : index
      cf.br ^bb12(%162 : index)
    ^bb14(%163: index):
    %164 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %165 = llvm.load %143 : !llvm.ptr -> i64
    %166 = llvm.call @printf(%164, %165) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %167 = arith.constant 0 : i32
    func.return %167 : i32
  }
}