123 Numbers: F(111111111111222333) mod 123123123. 123-numbers: digits only 1,2,3, and each digit count is itself a 123-number. Enumerate by length, count valid strings, unrank within the target length. All counts fit in i64 (verified: L=38, max count ~8.5e16).
# Project Euler 698
# 123 Numbers: F(111111111111222333) mod 123123123.
# 123-numbers: digits only 1,2,3, and each digit count is itself a 123-number.
# Enumerate by length, count valid strings, unrank within the target length.
# All counts fit in i64 (verified: L=38, max count ~8.5e16).
const MOD: i64 = 123123123
const TARGET: i64 = 111111111111222333
# Allowed digit counts: 0,1,2,3,11,12,13,21,22,23,31,32,33
const N_ALLOWED: i64 = 13
function binom(n: i64, k: i64) -> i64 {
if k < 0 { return 0 }
if k > n { return 0 }
if k == 0 { return 1 }
if k > n - k { k = n - k }
let mut result: i64 = 1
let mut i: i64 = 1
while i <= k {
result = result * (n - k + i) / i
i = i + 1
}
return result
}
# Count valid strings of length L with given prefix usage (u1,u2,u3).
# A string is valid if total counts (a,b,c) with a+b+c=L, each in allowed set.
function count_completions(L: i64, u1: i64, u2: i64, u3: i64) -> i64 {
let used: i64 = u1 + u2 + u3
let r: i64 = L - used
let mut total: i64 = 0
let allowed: array<i64, 13> = [0, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33]
let mut ai: i64 = 0
while ai < N_ALLOWED {
let a: i64 = allowed[ai]
if a < u1 {
ai = ai + 1
} else {
let ra: i64 = a - u1
if ra > r {
ai = ai + 1
} else {
let mut bi: i64 = 0
while bi < N_ALLOWED {
let b: i64 = allowed[bi]
if b < u2 {
bi = bi + 1
} else {
let rb: i64 = b - u2
if ra + rb > r {
bi = bi + 1
} else {
let rc: i64 = r - ra - rb
let c: i64 = u3 + rc
# Check if c is in allowed set
let mut ok: i32 = 0
let mut ci: i64 = 0
while ci < N_ALLOWED {
if allowed[ci] == c { ok = 1 }
ci = ci + 1
}
if ok == 0 {
bi = bi + 1
} else {
if a == 0 && b == 0 && c == 0 {
bi = bi + 1
} else {
let m: i64 = binom(r, ra) * binom(r - ra, rb)
total = total + m
bi = bi + 1
}
}
}
}
}
ai = ai + 1
}
}
}
return total
}
function main() -> i32 {
let mut cumulative: i64 = 0
let mut found_L: i64 = 0
let mut rank_in_length: i64 = 0
let mut L: i64 = 1
while L < 5000 {
let cnt_L: i64 = count_completions(L, 0, 0, 0)
if cumulative + cnt_L >= TARGET {
rank_in_length = TARGET - cumulative
found_L = L
L = 5000
} else {
cumulative = cumulative + cnt_L
L = L + 1
}
}
let mut u1: i64 = 0
let mut u2: i64 = 0
let mut u3: i64 = 0
let mut answer: i64 = 0
let mut pos: i64 = 0
while pos < found_L {
let mut digit: i64 = 1
while digit <= 3 {
let nu1: i64 = u1
let nu2: i64 = u2
let nu3: i64 = u3
if digit == 1 {
let nu1b: i64 = u1 + 1
let cnt: i64 = count_completions(found_L, nu1b, nu2, nu3)
if rank_in_length > cnt {
rank_in_length = rank_in_length - cnt
digit = digit + 1
} else {
u1 = nu1b
answer = (answer * 10 + digit) % MOD
digit = 4
}
} else {
if digit == 2 {
let nu2b: i64 = u2 + 1
let cnt: i64 = count_completions(found_L, nu1, nu2b, nu3)
if rank_in_length > cnt {
rank_in_length = rank_in_length - cnt
digit = digit + 1
} else {
u2 = nu2b
answer = (answer * 10 + digit) % MOD
digit = 4
}
} else {
let nu3b: i64 = u3 + 1
let cnt: i64 = count_completions(found_L, nu1, nu2, nu3b)
if rank_in_length > cnt {
rank_in_length = rank_in_length - cnt
digit = digit + 1
} else {
u3 = nu3b
answer = (answer * 10 + digit) % MOD
digit = 4
}
}
}
}
pos = pos + 1
}
printf("%lld\n", answer)
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 binom_i64_i64(int64_t n, int64_t k);
int64_t count_completions_i64_i64_i64_i64(int64_t L, int64_t u1, int64_t u2, int64_t u3);
int32_t main(void);
static const int64_t MOD = 123123123;
static const int64_t TARGET = 111111111111222333;
static const int64_t N_ALLOWED = 13;
int64_t binom_i64_i64(int64_t n, int64_t k) {
if (k < 0) {
return 0;
}
if (k > n) {
return 0;
}
if (k == 0) {
return 1;
}
if (k > (n - k)) {
k = (n - k);
}
int64_t result = 1;
int64_t i = 1;
while (i <= k) {
result = FLOW_CHECKED_DIV(((result * ((n - k) + i))), (i));
i = (i + 1);
}
return result;
}
int64_t count_completions_i64_i64_i64_i64(int64_t L, int64_t u1, int64_t u2, int64_t u3) {
int64_t used = ((u1 + u2) + u3);
int64_t r = (L - used);
int64_t total = 0;
int64_t allowed[13] = { 0, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33 };
int64_t ai = 0;
while (ai < N_ALLOWED) {
int64_t a = (((unsigned)(ai) < 13) ? allowed[ai] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ai), 13), flow_fault_handler("array index out of bounds"), allowed[0]));
if (a < u1) {
ai = (ai + 1);
} else {
int64_t ra = (a - u1);
if (ra > r) {
ai = (ai + 1);
} else {
int64_t bi = 0;
while (bi < N_ALLOWED) {
int64_t b = (((unsigned)(bi) < 13) ? allowed[bi] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(bi), 13), flow_fault_handler("array index out of bounds"), allowed[0]));
if (b < u2) {
bi = (bi + 1);
} else {
int64_t rb = (b - u2);
if ((ra + rb) > r) {
bi = (bi + 1);
} else {
int64_t rc = ((r - ra) - rb);
int64_t c = (u3 + rc);
int32_t ok = 0;
int64_t ci = 0;
while (ci < N_ALLOWED) {
if ((((unsigned)(ci) < 13) ? allowed[ci] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ci), 13), flow_fault_handler("array index out of bounds"), allowed[0])) == c) {
ok = 1;
}
ci = (ci + 1);
}
if (ok == 0) {
bi = (bi + 1);
} else {
if (((a == 0 && b == 0) && c == 0)) {
bi = (bi + 1);
} else {
int64_t m = (binom_i64_i64(r, ra) * binom_i64_i64((r - ra), rb));
total = (total + m);
bi = (bi + 1);
}
}
}
}
}
ai = (ai + 1);
}
}
}
return total;
}
int32_t main(void) {
int64_t cumulative = 0;
int64_t found_L = 0;
int64_t rank_in_length = 0;
int64_t L = 1;
while (L < 5000) {
int64_t cnt_L = count_completions_i64_i64_i64_i64(L, 0, 0, 0);
if ((cumulative + cnt_L) >= TARGET) {
rank_in_length = (TARGET - cumulative);
found_L = L;
L = 5000;
} else {
cumulative = (cumulative + cnt_L);
L = (L + 1);
}
}
int64_t u1 = 0;
int64_t u2 = 0;
int64_t u3 = 0;
int64_t answer = 0;
int64_t pos = 0;
while (pos < found_L) {
int64_t digit = 1;
while (digit <= 3) {
int64_t nu1 = u1;
int64_t nu2 = u2;
int64_t nu3 = u3;
if (digit == 1) {
int64_t nu1b = (u1 + 1);
int64_t cnt = count_completions_i64_i64_i64_i64(found_L, nu1b, nu2, nu3);
if (rank_in_length > cnt) {
rank_in_length = (rank_in_length - cnt);
digit = (digit + 1);
} else {
u1 = nu1b;
answer = FLOW_CHECKED_MOD((((answer * 10) + digit)), (MOD));
digit = 4;
}
} else {
if (digit == 2) {
int64_t nu2b = (u2 + 1);
int64_t cnt = count_completions_i64_i64_i64_i64(found_L, nu1, nu2b, nu3);
if (rank_in_length > cnt) {
rank_in_length = (rank_in_length - cnt);
digit = (digit + 1);
} else {
u2 = nu2b;
answer = FLOW_CHECKED_MOD((((answer * 10) + digit)), (MOD));
digit = 4;
}
} else {
int64_t nu3b = (u3 + 1);
int64_t cnt = count_completions_i64_i64_i64_i64(found_L, nu1, nu2, nu3b);
if (rank_in_length > cnt) {
rank_in_length = (rank_in_length - cnt);
digit = (digit + 1);
} else {
u3 = nu3b;
answer = FLOW_CHECKED_MOD((((answer * 10) + digit)), (MOD));
digit = 4;
}
}
}
}
pos = (pos + 1);
}
printf("%lld\n", answer);
return 0;
}