Task: Esthetic numbers

C: #include <stdio.h> 
#include <string.h>
#include <locale.h>

typedef int bool;
typedef unsigned long long ull;

#define TRUE 1
#define FALSE 0

char as_digit(int d) { 
    return (d >= 0 && d <= 9) ? d + '0' : d - 10 + 'a';  
}

void revstr(char *str) { 
    int i, len = strlen(str);
    char t; 
    for (i = 0; i < len/2; ++i) { 
        t = str[i]; 
        str[i] = str[len - i - 1]; 
        str[len - i - 1] = t; 
    } 
}  

char* to_base(char s[], ull n, int b) { 
    int i = 0; 
    while (n) { 
        s[i++] = as_digit(n % b); 
        n /= b; 
    } 
    s[i] = '\0'; 
    revstr(s);
    return s;  
} 

ull uabs(ull a, ull  b) {
    return a > b ? a - b : b - a;
}

bool is_esthetic(ull n, int b) {
    int i, j;
    if (!n) return FALSE;
    i = n % b;
    n /= b;
    while (n) {
        j = n % b;
        if (uabs(i, j) != 1) return FALSE;
        n /= b;
        i = j;
    }
    return TRUE;
}

ull esths[45000];
int le = 0;

void dfs(ull n, ull m, ull i) {
    ull d, i1, i2;
    if (i >= n && i <= m) esths[le++] = i;
    if (i == 0 || i > m) return; 
    d = i % 10;
    i1 = i * 10 + d - 1;
    i2 = i1 + 2;
    if (d == 0) {
        dfs(n, m, i2);
    } else if (d == 9) {
        dfs(n, m, i1);
    } else {
        dfs(n, m, i1);
        dfs(n, m, i2);
    }
}

void list_esths(ull n, ull n2, ull m, ull m2, int per_line, bool all) {
    int i;
    le = 0;
    for (i = 0; i < 10; ++i) {
        dfs(n2, m2, i);
    }
    printf("Base 10: %'d esthetic numbers between %'llu and %'llu:\n", le, n, m);
    if (all) {
        for (i = 0; i < le; ++i) {
            printf("%llu ", esths[i]);
            if (!(i+1)%per_line) printf("\n");
        }
    } else {
        for (i = 0; i < per_line; ++i) printf("%llu ", esths[i]);
        printf("\n............\n");
        for (i = le - per_line; i < le; ++i) printf("%llu ", esths[i]);
    }
    printf("\n\n");
}

int main() {
    ull n;
    int b, c;
    char ch[15] = {0};
    for (b = 2; b <= 16; ++b) {
        printf("Base %d: %dth to %dth esthetic numbers:\n", b, 4*b, 6*b);
        for (n = 1, c = 0; c < 6 * b; ++n) {
            if (is_esthetic(n, b)) {
                if (++c >= 4 * b) printf("%s ", to_base(ch, n, b));
            }
        }
        printf("\n\n");
    }
    char *oldLocale = setlocale(LC_NUMERIC, NULL);
    setlocale(LC_NUMERIC, ""); 

    
    list_esths(1000, 1010, 9999, 9898, 16, TRUE);
    list_esths(1e8, 101010101, 13*1e7, 123456789, 9, TRUE);
    list_esths(1e11, 101010101010, 13*1e10, 123456789898, 7, FALSE);
    list_esths(1e14, 101010101010101, 13*1e13, 123456789898989, 5, FALSE);
    list_esths(1e17, 101010101010101010, 13*1e16, 123456789898989898, 4, FALSE);
    setlocale(LC_NUMERIC, oldLocale);
    return 0;
}


C++: #include <functional>
#include <iostream>
#include <sstream>
#include <vector>

std::string to(int n, int b) {
    static auto BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    std::stringstream ss;
    while (n > 0) {
        auto rem = n % b;
        n = n / b;
        ss << BASE[rem];
    }

    auto fwd = ss.str();
    return std::string(fwd.rbegin(), fwd.rend());
}

uint64_t uabs(uint64_t a, uint64_t b) {
    if (a < b) {
        return b - a;
    }
    return a - b;
}

bool isEsthetic(uint64_t n, uint64_t b) {
    if (n == 0) {
        return false;
    }
    auto i = n % b;
    n /= b;
    while (n > 0) {
        auto j = n % b;
        if (uabs(i, j) != 1) {
            return false;
        }
        n /= b;
        i = j;
    }
    return true;
}

void listEsths(uint64_t n, uint64_t n2, uint64_t m, uint64_t m2, int perLine, bool all) {
    std::vector<uint64_t> esths;
    const auto dfs = [&esths](uint64_t n, uint64_t m, uint64_t i) {
        auto dfs_impl = [&esths](uint64_t n, uint64_t m, uint64_t i, auto &dfs_ref) {
            if (i >= n && i <= m) {
                esths.push_back(i);
            }
            if (i == 0 || i > m) {
                return;
            }
            auto d = i % 10;
            auto i1 = i * 10 + d - 1;
            auto i2 = i1 + 2;
            if (d == 0) {
                dfs_ref(n, m, i2, dfs_ref);
            } else if (d == 9) {
                dfs_ref(n, m, i1, dfs_ref);
            } else {
                dfs_ref(n, m, i1, dfs_ref);
                dfs_ref(n, m, i2, dfs_ref);
            }
        };
        dfs_impl(n, m, i, dfs_impl);
    };

    for (int i = 0; i < 10; i++) {
        dfs(n2, m2, i);
    }
    auto le = esths.size();
    printf("Base 10: %d esthetic numbers between %llu and %llu:\n", le, n, m);
    if (all) {
        for (size_t c = 0; c < esths.size(); c++) {
            auto esth = esths[c];
            printf("%llu ", esth);
            if ((c + 1) % perLine == 0) {
                printf("\n");
            }
        }
        printf("\n");
    } else {
        for (int c = 0; c < perLine; c++) {
            auto esth = esths[c];
            printf("%llu ", esth);
        }
        printf("\n............\n");
        for (size_t i = le - perLine; i < le; i++) {
            auto esth = esths[i];
            printf("%llu ", esth);
        }
        printf("\n");
    }
    printf("\n");
}

int main() {
    for (int b = 2; b <= 16; b++) {
        printf("Base %d: %dth to %dth esthetic numbers:\n", b, 4 * b, 6 * b);
        for (int n = 1, c = 0; c < 6 * b; n++) {
            if (isEsthetic(n, b)) {
                c++;
                if (c >= 4 * b) {
                    std::cout << to(n, b) << ' ';
                }
            }
        }
        printf("\n");
    }
    printf("\n");

    
    listEsths(1000, 1010, 9999, 9898, 16, true);
    listEsths((uint64_t)1e8, 101'010'101, 13 * (uint64_t)1e7, 123'456'789, 9, true);
    listEsths((uint64_t)1e11, 101'010'101'010, 13 * (uint64_t)1e10, 123'456'789'898, 7, false);
    listEsths((uint64_t)1e14, 101'010'101'010'101, 13 * (uint64_t)1e13, 123'456'789'898'989, 5, false);
    listEsths((uint64_t)1e17, 101'010'101'010'101'010, 13 * (uint64_t)1e16, 123'456'789'898'989'898, 4, false);
    return 0;
}