I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Esthetic numbers

From Rosetta Code
Esthetic numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

An esthetic number is a positive integer where every adjacent digit differs from its neighbour by 1.


E.G.
  • 12 is an esthetic number. One and two differ by 1.
  • 5654 is an esthetic number. Each digit is exactly 1 away from its neighbour.
  • 890 is not an esthetic number. Nine and zero differ by 9.


These examples are nominally in base 10 but the concept extends easily to numbers in other bases. Traditionally, single digit numbers are included in esthetic numbers; zero may or may not be. For our purposes, for this task, do not include zero (0) as an esthetic number. Do not include numbers with leading zeros.

Esthetic numbers are also sometimes referred to as stepping numbers.


Task
  • Write a routine (function, procedure, whatever) to find esthetic numbers in a given base.
  • Use that routine to find esthetic numbers in bases 2 through 16 and display, here on this page, the esthectic numbers from index (base × 4) through index (base × 6), inclusive. (E.G. for base 2: 8th through 12th, for base 6: 24th through 36th, etc.)
  • Find and display, here on this page, the base 10 esthetic numbers with a magnitude between 1000 and 9999.
  • Stretch: Find and display, here on this page, the base 10 esthetic numbers with a magnitude between 1.0e8 and 1.3e8.


See also


C[edit]

Translation of: Go
#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, "");
 
// the following all use the obvious range limitations for the numbers in question
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;
}
Output:
Same as Go entry.

C++[edit]

Translation of: D
#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");
 
// the following all use the obvious range limitations for the numbers in question
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;
}
Output:
Base 2: 8th to 12th esthetic numbers:
10101010 101010101 1010101010 10101010101 101010101010
Base 3: 12th to 18th esthetic numbers:
1210 1212 2101 2121 10101 10121 12101
Base 4: 16th to 24th esthetic numbers:
323 1010 1012 1210 1212 1232 2101 2121 2123
Base 5: 20th to 30th esthetic numbers:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
Base 6: 24th to 36th esthetic numbers:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
Base 7: 28th to 42th esthetic numbers:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
Base 8: 32th to 48th esthetic numbers:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
Base 9: 36th to 54th esthetic numbers:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
Base 10: 40th to 60th esthetic numbers:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
Base 11: 44th to 66th esthetic numbers:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 A98 A9A 1010
Base 12: 48th to 72th esthetic numbers:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA BA9 BAB
Base 13: 52th to 78th esthetic numbers:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB CBA
Base 14: 56th to 84th esthetic numbers:
565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC
Base 15: 60th to 90th esthetic numbers:
567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD
Base 16: 64th to 96th esthetic numbers:
654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD DED DEF EDC

Base 10: 61 esthetic numbers between 1000 and 9999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898

Base 10: 126 esthetic numbers between 100000000 and 130000000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789


Base 10: 911 esthetic numbers between 100000000000 and 130000000000:
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010
............
123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898

Base 10: 6225 esthetic numbers between 100000000000000 and 130000000000000:
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121
............
123456789898767 123456789898787 123456789898789 123456789898987 123456789898989

Base 10: 44744 esthetic numbers between 100000000000000000 and 130000000000000000:
101010101010101010 101010101010101012 101010101010101210 101010101010101212
............
123456789898987898 123456789898989876 123456789898989878 123456789898989898

D[edit]

Translation of: Go
import std.conv;
import std.stdio;
 
ulong uabs(ulong a, ulong b) {
if (a > b) {
return a - b;
}
return b - a;
}
 
bool isEsthetic(ulong n, ulong 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;
}
 
ulong[] esths;
 
void dfs(ulong n, ulong m, ulong i) {
if (i >= n && i <= m) {
esths ~= 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(n, m, i2);
} else if (d == 9) {
dfs(n, m, i1);
} else {
dfs(n, m, i1);
dfs(n, m, i2);
}
}
 
void listEsths(ulong n, ulong n2, ulong m, long m2, int perLine, bool all) {
esths.length = 0;
for (auto i = 0; i < 10; i++) {
dfs(n2, m2, i);
}
auto le = esths.length;
writefln("Base 10: %s esthetic numbers between %s and %s:", le, n, m);
if (all) {
foreach (c, esth; esths) {
write(esth, ' ');
if ((c + 1) % perLine == 0) {
writeln;
}
}
writeln;
} else {
for (auto i = 0; i < perLine; i++) {
write(esths[i], ' ');
}
writeln("\n............");
for (auto i = le - perLine; i < le; i++) {
write(esths[i], ' ');
}
writeln;
}
writeln;
}
 
void main() {
for (auto b = 2; b <= 16; b++) {
writefln("Base %d: %dth to %dth esthetic numbers:", b, 4 * b, 6 * b);
for (auto n = 1, c = 0; c < 6 * b; n++) {
if (isEsthetic(n, b)) {
c++;
if (c >= 4 * b) {
write(to!string(n, b), ' ');
}
}
}
writeln;
}
writeln;
 
// the following all use the obvious range limitations for the numbers in question
listEsths(1000, 1010, 9999, 9898, 16, true);
listEsths(cast(ulong) 1e8, 101_010_101, 13*cast(ulong) 1e7, 123_456_789, 9, true);
listEsths(cast(ulong) 1e11, 101_010_101_010, 13*cast(ulong) 1e10, 123_456_789_898, 7, false);
listEsths(cast(ulong) 1e14, 101_010_101_010_101, 13*cast(ulong) 1e13, 123_456_789_898_989, 5, false);
listEsths(cast(ulong) 1e17, 101_010_101_010_101_010, 13*cast(ulong) 1e16, 123_456_789_898_989_898, 4, false);
}
Output:
Base 2: 8th to 12th esthetic numbers:
10101010 101010101 1010101010 10101010101 101010101010 
Base 3: 12th to 18th esthetic numbers:
1210 1212 2101 2121 10101 10121 12101 
Base 4: 16th to 24th esthetic numbers:
323 1010 1012 1210 1212 1232 2101 2121 2123 
Base 5: 20th to 30th esthetic numbers:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101 
Base 6: 24th to 36th esthetic numbers:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234 
Base 7: 28th to 42th esthetic numbers:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232 
Base 8: 32th to 48th esthetic numbers:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212 
Base 9: 36th to 54th esthetic numbers:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210 
Base 10: 40th to 60th esthetic numbers:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 
Base 11: 44th to 66th esthetic numbers:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 A98 A9A 1010 
Base 12: 48th to 72th esthetic numbers:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA BA9 BAB 
Base 13: 52th to 78th esthetic numbers:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB CBA 
Base 14: 56th to 84th esthetic numbers:
565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC 
Base 15: 60th to 90th esthetic numbers:
567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD 
Base 16: 64th to 96th esthetic numbers:
654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD DED DEF EDC 

Base 10: 61 esthetic numbers between 1000 and 9999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676 
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898 

Base 10: 126 esthetic numbers between 100000000 and 130000000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343 
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323 
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345 
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101 
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345 
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343 
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321 
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121 
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101 
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343 
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321 
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545 
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565 
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789 


Base 10: 911 esthetic numbers between 100000000000 and 130000000000:
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010 
............
123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898 

Base 10: 6225 esthetic numbers between 100000000000000 and 130000000000000:
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121 
............
123456789898767 123456789898787 123456789898789 123456789898987 123456789898989 

Base 10: 44744 esthetic numbers between 100000000000000000 and 130000000000000000:
101010101010101010 101010101010101012 101010101010101210 101010101010101212 
............
123456789898987898 123456789898989876 123456789898989878 123456789898989898 

F#[edit]

The functions[edit]

 
// Generate Esthetic Numbers. Nigel Galloway: March 21st., 2020
let rec fN Σ n g = match g with h::t -> match List.head h with
0 -> fN ((1::h)::Σ) n t
|g when g=n-1 -> fN ((g-1::h)::Σ) n t
|g -> fN ((g-1::h)::(g+1::h)::Σ) n t
|_ -> Σ
 
let Esthetic n = let Esthetic, g = fN [] n, [1..n-1] |> List.map(fun n->[n])
Seq.unfold(fun n->Some(n,Esthetic(List.rev n)))(g) |> Seq.concat
 
let EtoS n = let g = "0123456789abcdef".ToCharArray()
n |> List.map(fun n->g.[n]) |> List.rev |> Array.ofList |> System.String
 

The Tasks[edit]

Esthetic numbers in bases 2 through 16
 
[2..16]|>List.iter(fun n->printfn "\nBase %d" n; Esthetic n|>Seq.skip(4*n-1)|>Seq.take((6-4)*n+1)|>Seq.iter(EtoS >> printfn "%s"))
 
Output:
Base 2
10101010
101010101
1010101010
10101010101
101010101010

Base 3
1210
1212
2101
2121
10101
10121
12101

Base 4
323
1010
1012
1210
1212
1232
2101
2121
2123

Base 5
323
343
432
434
1010
1012
1210
1212
1232
1234
2101

Base 6
343
345
432
434
454
543
545
1010
1012
1210
1212
1232
1234

Base 7
345
432
434
454
456
543
545
565
654
656
1010
1012
1210
1212
1232

Base 8
432
434
454
456
543
545
565
567
654
656
676
765
767
1010
1012
1210
1212

Base 9
434
454
456
543
545
565
567
654
656
676
678
765
767
787
876
878
1010
1012
1210

Base 10
454
456
543
545
565
567
654
656
676
678
765
767
787
789
876
878
898
987
989
1010
1012

Base 11
456
543
545
565
567
654
656
676
678
765
767
787
789
876
878
898
89a
987
989
9a9
a98
a9a
1010

Base 12
543
545
565
567
654
656
676
678
765
767
787
789
876
878
898
89a
987
989
9a9
9ab
a98
a9a
aba
ba9
bab

Base 13
545
565
567
654
656
676
678
765
767
787
789
876
878
898
89a
987
989
9a9
9ab
a98
a9a
aba
abc
ba9
bab
bcb
cba

Base 14
565
567
654
656
676
678
765
767
787
789
876
878
898
89a
987
989
9a9
9ab
a98
a9a
aba
abc
ba9
bab
bcb
bcd
cba
cbc
cdc

Base 15
567
654
656
676
678
765
767
787
789
876
878
898
89a
987
989
9a9
9ab
a98
a9a
aba
abc
ba9
bab
bcb
bcd
cba
cbc
cdc
cde
dcb
dcd

Base 16
654
656
676
678
765
767
787
789
876
878
898
89a
987
989
9a9
9ab
a98
a9a
aba
abc
ba9
bab
bcb
bcd
cba
cbc
cdc
cde
dcb
dcd
ded
def
edc
Base 10 esthetic numbers with a magnitude between 1000 and 9999
 
Esthetic 10|>Seq.map(EtoS>>int)|>Seq.skipWhile(fun n->n<1000)|>Seq.takeWhile(fun n->n<9999)|>Seq.iter(printfn "%d");;
 
Output:
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898
Base 10 esthetic numbers with a magnitude between 1.0e8 and 1.3e8
 
Esthetic 10|>Seq.map(EtoS>>int)|>Seq.skipWhile(fun n->n<100000000)|>Seq.takeWhile(fun n->n< 130000000)|>Seq.iter(printfn "%d")
 
Output:
101010101
101010121
101010123
101012101
101012121
101012123
101012321
101012323
101012343
101012345
101210101
101210121
101210123
101212101
101212121
101212123
101212321
101212323
101212343
101212345
101232101
101232121
101232123
101232321
101232323
101232343
101232345
101234321
101234323
101234343
101234345
101234543
101234545
101234565
101234567
121010101
121010121
121010123
121012101
121012121
121012123
121012321
121012323
121012343
121012345
121210101
121210121
121210123
121212101
121212121
121212123
121212321
121212323
121212343
121212345
121232101
121232121
121232123
121232321
121232323
121232343
121232345
121234321
121234323
121234343
121234345
121234543
121234545
121234565
121234567
123210101
123210121
123210123
123212101
123212121
123212123
123212321
123212323
123212343
123212345
123232101
123232121
123232123
123232321
123232323
123232343
123232345
123234321
123234323
123234343
123234345
123234543
123234545
123234565
123234567
123432101
123432121
123432123
123432321
123432323
123432343
123432345
123434321
123434323
123434343
123434345
123434543
123434545
123434565
123434567
123454321
123454323
123454343
123454345
123454543
123454545
123454565
123454567
123456543
123456545
123456565
123456567
123456765
123456767
123456787
123456789
Base 10 esthetic numbers with a magnitude between 1.0e11 and 1.3e11
 
Esthetic 10|>Seq.map(EtoS>>int64)|>Seq.skipWhile(fun n->n<100000000000L)|>Seq.takeWhile(fun n->n<130000000000L)|>Seq.iter(printfn "%d")
 
Output:
101010101010
101010101012
101010101210
101010101212
101010101232
101010101234
101010121010
101010121012
101010121210
101010121212
101010121232
101010121234
101010123210
101010123212
101010123232
101010123234
101010123432
101010123434
101010123454
101010123456
101012101010
101012101012
101012101210
101012101212
101012101232
101012101234
101012121010
101012121012
101012121210
101012121212
101012121232
101012121234
101012123210
101012123212
101012123232
101012123234
101012123432
101012123434
101012123454
101012123456
101012321010
101012321012
101012321210
101012321212
101012321232
101012321234
101012323210
101012323212
101012323232
101012323234
101012323432
101012323434
101012323454
101012323456
101012343210
101012343212
101012343232
101012343234
101012343432
101012343434
101012343454
101012343456
101012345432
101012345434
101012345454
101012345456
101012345654
101012345656
101012345676
101012345678
101210101010
101210101012
101210101210
101210101212
101210101232
101210101234
101210121010
101210121012
101210121210
101210121212
101210121232
101210121234
101210123210
101210123212
101210123232
101210123234
101210123432
101210123434
101210123454
101210123456
101212101010
101212101012
101212101210
101212101212
101212101232
101212101234
101212121010
101212121012
101212121210
101212121212
101212121232
101212121234
101212123210
101212123212
101212123232
101212123234
101212123432
101212123434
101212123454
101212123456
101212321010
101212321012
101212321210
101212321212
101212321232
101212321234
101212323210
101212323212
101212323232
101212323234
101212323432
101212323434
101212323454
101212323456
101212343210
101212343212
101212343232
101212343234
101212343432
101212343434
101212343454
101212343456
101212345432
101212345434
101212345454
101212345456
101212345654
101212345656
101212345676
101212345678
101232101010
101232101012
101232101210
101232101212
101232101232
101232101234
101232121010
101232121012
101232121210
101232121212
101232121232
101232121234
101232123210
101232123212
101232123232
101232123234
101232123432
101232123434
101232123454
101232123456
101232321010
101232321012
101232321210
101232321212
101232321232
101232321234
101232323210
101232323212
101232323232
101232323234
101232323432
101232323434
101232323454
101232323456
101232343210
101232343212
101232343232
101232343234
101232343432
101232343434
101232343454
101232343456
101232345432
101232345434
101232345454
101232345456
101232345654
101232345656
101232345676
101232345678
101234321010
101234321012
101234321210
101234321212
101234321232
101234321234
101234323210
101234323212
101234323232
101234323234
101234323432
101234323434
101234323454
101234323456
101234343210
101234343212
101234343232
101234343234
101234343432
101234343434
101234343454
101234343456
101234345432
101234345434
101234345454
101234345456
101234345654
101234345656
101234345676
101234345678
101234543210
101234543212
101234543232
101234543234
101234543432
101234543434
101234543454
101234543456
101234545432
101234545434
101234545454
101234545456
101234545654
101234545656
101234545676
101234545678
101234565432
101234565434
101234565454
101234565456
101234565654
101234565656
101234565676
101234565678
101234567654
101234567656
101234567676
101234567678
101234567876
101234567878
101234567898
121010101010
121010101012
121010101210
121010101212
121010101232
121010101234
121010121010
121010121012
121010121210
121010121212
121010121232
121010121234
121010123210
121010123212
121010123232
121010123234
121010123432
121010123434
121010123454
121010123456
121012101010
121012101012
121012101210
121012101212
121012101232
121012101234
121012121010
121012121012
121012121210
121012121212
121012121232
121012121234
121012123210
121012123212
121012123232
121012123234
121012123432
121012123434
121012123454
121012123456
121012321010
121012321012
121012321210
121012321212
121012321232
121012321234
121012323210
121012323212
121012323232
121012323234
121012323432
121012323434
121012323454
121012323456
121012343210
121012343212
121012343232
121012343234
121012343432
121012343434
121012343454
121012343456
121012345432
121012345434
121012345454
121012345456
121012345654
121012345656
121012345676
121012345678
121210101010
121210101012
121210101210
121210101212
121210101232
121210101234
121210121010
121210121012
121210121210
121210121212
121210121232
121210121234
121210123210
121210123212
121210123232
121210123234
121210123432
121210123434
121210123454
121210123456
121212101010
121212101012
121212101210
121212101212
121212101232
121212101234
121212121010
121212121012
121212121210
121212121212
121212121232
121212121234
121212123210
121212123212
121212123232
121212123234
121212123432
121212123434
121212123454
121212123456
121212321010
121212321012
121212321210
121212321212
121212321232
121212321234
121212323210
121212323212
121212323232
121212323234
121212323432
121212323434
121212323454
121212323456
121212343210
121212343212
121212343232
121212343234
121212343432
121212343434
121212343454
121212343456
121212345432
121212345434
121212345454
121212345456
121212345654
121212345656
121212345676
121212345678
121232101010
121232101012
121232101210
121232101212
121232101232
121232101234
121232121010
121232121012
121232121210
121232121212
121232121232
121232121234
121232123210
121232123212
121232123232
121232123234
121232123432
121232123434
121232123454
121232123456
121232321010
121232321012
121232321210
121232321212
121232321232
121232321234
121232323210
121232323212
121232323232
121232323234
121232323432
121232323434
121232323454
121232323456
121232343210
121232343212
121232343232
121232343234
121232343432
121232343434
121232343454
121232343456
121232345432
121232345434
121232345454
121232345456
121232345654
121232345656
121232345676
121232345678
121234321010
121234321012
121234321210
121234321212
121234321232
121234321234
121234323210
121234323212
121234323232
121234323234
121234323432
121234323434
121234323454
121234323456
121234343210
121234343212
121234343232
121234343234
121234343432
121234343434
121234343454
121234343456
121234345432
121234345434
121234345454
121234345456
121234345654
121234345656
121234345676
121234345678
121234543210
121234543212
121234543232
121234543234
121234543432
121234543434
121234543454
121234543456
121234545432
121234545434
121234545454
121234545456
121234545654
121234545656
121234545676
121234545678
121234565432
121234565434
121234565454
121234565456
121234565654
121234565656
121234565676
121234565678
121234567654
121234567656
121234567676
121234567678
121234567876
121234567878
121234567898
123210101010
123210101012
123210101210
123210101212
123210101232
123210101234
123210121010
123210121012
123210121210
123210121212
123210121232
123210121234
123210123210
123210123212
123210123232
123210123234
123210123432
123210123434
123210123454
123210123456
123212101010
123212101012
123212101210
123212101212
123212101232
123212101234
123212121010
123212121012
123212121210
123212121212
123212121232
123212121234
123212123210
123212123212
123212123232
123212123234
123212123432
123212123434
123212123454
123212123456
123212321010
123212321012
123212321210
123212321212
123212321232
123212321234
123212323210
123212323212
123212323232
123212323234
123212323432
123212323434
123212323454
123212323456
123212343210
123212343212
123212343232
123212343234
123212343432
123212343434
123212343454
123212343456
123212345432
123212345434
123212345454
123212345456
123212345654
123212345656
123212345676
123212345678
123232101010
123232101012
123232101210
123232101212
123232101232
123232101234
123232121010
123232121012
123232121210
123232121212
123232121232
123232121234
123232123210
123232123212
123232123232
123232123234
123232123432
123232123434
123232123454
123232123456
123232321010
123232321012
123232321210
123232321212
123232321232
123232321234
123232323210
123232323212
123232323232
123232323234
123232323432
123232323434
123232323454
123232323456
123232343210
123232343212
123232343232
123232343234
123232343432
123232343434
123232343454
123232343456
123232345432
123232345434
123232345454
123232345456
123232345654
123232345656
123232345676
123232345678
123234321010
123234321012
123234321210
123234321212
123234321232
123234321234
123234323210
123234323212
123234323232
123234323234
123234323432
123234323434
123234323454
123234323456
123234343210
123234343212
123234343232
123234343234
123234343432
123234343434
123234343454
123234343456
123234345432
123234345434
123234345454
123234345456
123234345654
123234345656
123234345676
123234345678
123234543210
123234543212
123234543232
123234543234
123234543432
123234543434
123234543454
123234543456
123234545432
123234545434
123234545454
123234545456
123234545654
123234545656
123234545676
123234545678
123234565432
123234565434
123234565454
123234565456
123234565654
123234565656
123234565676
123234565678
123234567654
123234567656
123234567676
123234567678
123234567876
123234567878
123234567898
123432101010
123432101012
123432101210
123432101212
123432101232
123432101234
123432121010
123432121012
123432121210
123432121212
123432121232
123432121234
123432123210
123432123212
123432123232
123432123234
123432123432
123432123434
123432123454
123432123456
123432321010
123432321012
123432321210
123432321212
123432321232
123432321234
123432323210
123432323212
123432323232
123432323234
123432323432
123432323434
123432323454
123432323456
123432343210
123432343212
123432343232
123432343234
123432343432
123432343434
123432343454
123432343456
123432345432
123432345434
123432345454
123432345456
123432345654
123432345656
123432345676
123432345678
123434321010
123434321012
123434321210
123434321212
123434321232
123434321234
123434323210
123434323212
123434323232
123434323234
123434323432
123434323434
123434323454
123434323456
123434343210
123434343212
123434343232
123434343234
123434343432
123434343434
123434343454
123434343456
123434345432
123434345434
123434345454
123434345456
123434345654
123434345656
123434345676
123434345678
123434543210
123434543212
123434543232
123434543234
123434543432
123434543434
123434543454
123434543456
123434545432
123434545434
123434545454
123434545456
123434545654
123434545656
123434545676
123434545678
123434565432
123434565434
123434565454
123434565456
123434565654
123434565656
123434565676
123434565678
123434567654
123434567656
123434567676
123434567678
123434567876
123434567878
123434567898
123454321010
123454321012
123454321210
123454321212
123454321232
123454321234
123454323210
123454323212
123454323232
123454323234
123454323432
123454323434
123454323454
123454323456
123454343210
123454343212
123454343232
123454343234
123454343432
123454343434
123454343454
123454343456
123454345432
123454345434
123454345454
123454345456
123454345654
123454345656
123454345676
123454345678
123454543210
123454543212
123454543232
123454543234
123454543432
123454543434
123454543454
123454543456
123454545432
123454545434
123454545454
123454545456
123454545654
123454545656
123454545676
123454545678
123454565432
123454565434
123454565454
123454565456
123454565654
123454565656
123454565676
123454565678
123454567654
123454567656
123454567676
123454567678
123454567876
123454567878
123454567898
123456543210
123456543212
123456543232
123456543234
123456543432
123456543434
123456543454
123456543456
123456545432
123456545434
123456545454
123456545456
123456545654
123456545656
123456545676
123456545678
123456565432
123456565434
123456565454
123456565456
123456565654
123456565656
123456565676
123456565678
123456567654
123456567656
123456567676
123456567678
123456567876
123456567878
123456567898
123456765432
123456765434
123456765454
123456765456
123456765654
123456765656
123456765676
123456765678
123456767654
123456767656
123456767676
123456767678
123456767876
123456767878
123456767898
123456787654
123456787656
123456787676
123456787678
123456787876
123456787878
123456787898
123456789876
123456789878
123456789898
Real: 00:00:00.322, CPU: 00:00:00.093, GC gen0: 3, gen1: 2, gen2: 1

Factor[edit]

The bfs word is an adaptation of the algorithm from the Geeks for Geeks reference. It has been changed to work with any base. In summary, this algorithm constructs esthetic numbers directly using a breadth first search. For example, we know the only two esthetic numbers that can be constructed from 23 are 232 and 234, or in other words, the last digit of 23 ± 1 appended to 23. This forms a tree for each digit in a given base where each node is an esthetic number and can have at most two children. This method is very fast and has been used to find the count of esthetic numbers in base 10 between zero and one quadrillion.

Works with: Factor version 0.99 2020-01-23
USING: combinators deques dlists formatting grouping io kernel
locals make math math.order math.parser math.ranges
math.text.english prettyprint sequences sorting strings ;
 
:: bfs ( from to num base -- )
DL{ } clone :> q
base 1 - :> ld
num q push-front
[ q deque-empty? ]
[
q pop-back :> step-num
step-num from to between? [ step-num , ] when
step-num zero? step-num to > or
[
step-num base mod :> last-digit
step-num base * last-digit 1 - + :> a
step-num base * last-digit 1 + + :> b
 
last-digit
{
{ 0 [ b q push-front ] }
{ ld [ a q push-front ] }
[ drop a q push-front b q push-front ]
} case
 
] unless
 
] until ;
 
:: esthetics ( from to base -- seq )
[ base <iota> [| num | from to num base bfs ] each ]
{ } make natural-sort ;
 
: .seq ( seq width -- )
group [ [ dup string? [ write ] [ pprint ] if bl ] each nl ]
each nl ;
 
:: show ( base -- )
base [ 4 * ] [ 6 * ] bi :> ( from to )
from to [ dup ordinal-suffix ] [email protected] base
"%d%s through %d%s esthetic numbers in base %d\n" printf
from to 1 + 0 5000  ! enough for base 16
base esthetics subseq [ base >base ] map 17 .seq ;
 
2 16 [a,b] [ show ] each
 
"Base 10 numbers between 1,000 and 9,999:" print
1,000 9,999 10 esthetics 16 .seq
 
"Base 10 numbers between 100,000,000 and 130,000,000:" print
100,000,000 130,000,000 10 esthetics 9 .seq
 
"Count of base 10 esthetic numbers between zero and one quadrillion:"
print 0 1e15 10 esthetics length .
Output:
8th through 12th esthetic numbers in base 2
10101010 101010101 1010101010 10101010101 101010101010 

12th through 18th esthetic numbers in base 3
1210 1212 2101 2121 10101 10121 12101 

16th through 24th esthetic numbers in base 4
323 1010 1012 1210 1212 1232 2101 2121 2123 

20th through 30th esthetic numbers in base 5
323 343 432 434 1010 1012 1210 1212 1232 1234 2101 

24th through 36th esthetic numbers in base 6
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234 

28th through 42nd esthetic numbers in base 7
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232 

32nd through 48th esthetic numbers in base 8
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212 

36th through 54th esthetic numbers in base 9
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 
1012 1210 

40th through 60th esthetic numbers in base 10
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 
987 989 1010 1012 

44th through 66th esthetic numbers in base 11
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 
987 989 9a9 a98 a9a 1010 

48th through 72nd esthetic numbers in base 12
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 
989 9a9 9ab a98 a9a aba ba9 bab 

52nd through 78th esthetic numbers in base 13
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 
9a9 9ab a98 a9a aba abc ba9 bab bcb cba 

56th through 84th esthetic numbers in base 14
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 
9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc 

60th through 90th esthetic numbers in base 15
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab 
a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd 

64th through 96th esthetic numbers in base 16
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 
a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc 

Base 10 numbers between 1,000 and 9,999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676 
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898 

Base 10 numbers between 100,000,000 and 130,000,000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343 
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323 
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345 
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101 
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345 
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343 
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321 
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121 
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101 
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343 
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321 
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545 
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565 
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789 

Count of base 10 esthetic numbers between zero and one quadrillion:
161914

Go[edit]

A simple brute force approach suffices for the first and second parts of the task.

For the last two parts (stretched up to 1.0e17/1.3e17), a 'depth first search' approach is used with the obvious range limitations imposed for extra speed.

package main
 
import (
"fmt"
"strconv"
)
 
func uabs(a, b uint64) uint64 {
if a > b {
return a - b
}
return b - a
}
 
func isEsthetic(n, b uint64) bool {
if n == 0 {
return false
}
i := n % b
n /= b
for n > 0 {
j := n % b
if uabs(i, j) != 1 {
return false
}
n /= b
i = j
}
return true
}
 
var esths []uint64
 
func dfs(n, m, i uint64) {
if i >= n && i <= m {
esths = append(esths, 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)
}
}
 
func listEsths(n, n2, m, m2 uint64, perLine int, all bool) {
esths = esths[:0]
for i := uint64(0); i < 10; i++ {
dfs(n2, m2, i)
}
le := len(esths)
fmt.Printf("Base 10: %s esthetic numbers between %s and %s:\n",
commatize(uint64(le)), commatize(n), commatize(m))
if all {
for c, esth := range esths {
fmt.Printf("%d ", esth)
if (c+1)%perLine == 0 {
fmt.Println()
}
}
} else {
for i := 0; i < perLine; i++ {
fmt.Printf("%d ", esths[i])
}
fmt.Println("\n............\n")
for i := le - perLine; i < le; i++ {
fmt.Printf("%d ", esths[i])
}
}
fmt.Println("\n")
}
 
func commatize(n uint64) string {
s := fmt.Sprintf("%d", n)
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
return s
}
 
func main() {
for b := uint64(2); b <= 16; b++ {
fmt.Printf("Base %d: %dth to %dth esthetic numbers:\n", b, 4*b, 6*b)
for n, c := uint64(1), uint64(0); c < 6*b; n++ {
if isEsthetic(n, b) {
c++
if c >= 4*b {
fmt.Printf("%s ", strconv.FormatUint(n, int(b)))
}
}
}
fmt.Println("\n")
}
 
// the following all use the obvious range limitations for the numbers in question
listEsths(1000, 1010, 9999, 9898, 16, true)
listEsths(1e8, 101_010_101, 13*1e7, 123_456_789, 9, true)
listEsths(1e11, 101_010_101_010, 13*1e10, 123_456_789_898, 7, false)
listEsths(1e14, 101_010_101_010_101, 13*1e13, 123_456_789_898_989, 5, false)
listEsths(1e17, 101_010_101_010_101_010, 13*1e16, 123_456_789_898_989_898, 4, false)
}
Output:
Base 2: 8th to 12th esthetic numbers:
10101010 101010101 1010101010 10101010101 101010101010 

Base 3: 12th to 18th esthetic numbers:
1210 1212 2101 2121 10101 10121 12101 

Base 4: 16th to 24th esthetic numbers:
323 1010 1012 1210 1212 1232 2101 2121 2123 

Base 5: 20th to 30th esthetic numbers:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101 

Base 6: 24th to 36th esthetic numbers:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234 

Base 7: 28th to 42th esthetic numbers:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232 

Base 8: 32th to 48th esthetic numbers:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212 

Base 9: 36th to 54th esthetic numbers:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210 

Base 10: 40th to 60th esthetic numbers:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 

Base 11: 44th to 66th esthetic numbers:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010 

Base 12: 48th to 72th esthetic numbers:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab 

Base 13: 52th to 78th esthetic numbers:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba 

Base 14: 56th to 84th esthetic numbers:
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc 

Base 15: 60th to 90th esthetic numbers:
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd 

Base 16: 64th to 96th esthetic numbers:
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc 

Base 10: 61 esthetic numbers between 1,000 and 9,999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676 
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898 

Base 10: 126 esthetic numbers between 100,000,000 and 130,000,000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343 
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323 
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345 
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101 
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345 
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343 
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321 
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121 
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101 
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343 
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321 
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545 
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565 
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789 


Base 10: 911 esthetic numbers between 100,000,000,000 and 130,000,000,000:
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010 
............

123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898 

Base 10: 6,225 esthetic numbers between 100,000,000,000,000 and 130,000,000,000,000:
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121 
............

123456789898767 123456789898787 123456789898789 123456789898987 123456789898989 

Base 10: 44,744 esthetic numbers between 100,000,000,000,000,000 and 130,000,000,000,000,000:
101010101010101010 101010101010101012 101010101010101210 101010101010101212 
............

123456789898987898 123456789898989876 123456789898989878 123456789898989898 

Julia[edit]

Illustrates both brute force and iterative methods of generating numbers in the sequence.

using Formatting
import Base.iterate, Base.IteratorSize, Base.IteratorEltype
 
"""
struct Esthetic
 
Used for iteration of esthetic numbers
"""
struct Esthetic{T}
lowerlimit::T where T <: Integer
base::T
upperlimit::T
Esthetic{T}(n, bas, m=typemax(T)) where T = new{T}(nextesthetic(n, bas), bas, m)
end
 
Base.IteratorSize(n::Esthetic) = Base.IsInfinite()
Base.IteratorEltype(n::Esthetic) = Integer
function Base.iterate(es::Esthetic, state=typeof(es.lowerlimit)[])
state = isempty(state) ? digits(es.lowerlimit, base=es.base) : increment!(state, es.base, 1)
n = toInt(state, es.base)
return n <= es.upperlimit ? (n, state) : nothing
end
 
isesthetic(n, b) = (d = digits(n, base=b); all(i -> abs(d[i] - d[i + 1]) == 1, 1:length(d)-1))
toInt(dig, bas) = foldr((i, j) -> i + bas * j, dig)
nextesthetic(n, b) = for i in n:typemax(typeof(n)) if isesthetic(i, b) return i end; end
 
""" Fill the digits below pos in vector with the least esthetic number fitting there """
function filldecreasing!(vec, pos)
if pos > 1
n = vec[pos]
for i in pos-1:-1:1
n = (n == 0) ? 1 : n - 1
vec[i] = n
end
end
return vec
end
 
""" Get the next esthetic number's digits from the previous number's digits """
function increment!(vec, bas, startpos = 1)
len = length(vec)
if len == 1
if vec[1] < bas - 1
vec[1] += 1
else
vec[1] = 0
push!(vec, 1)
end
else
pos = findfirst(i -> vec[i] < vec[i + 1], startpos:len-1)
if pos == nothing
if vec[end] >= bas - 1
push!(vec, 1)
filldecreasing!(vec, len + 1)
else
vec[end] += 1
filldecreasing!(vec, len)
end
else
for i in pos:len
if i == len
if vec[i] < bas - 1
vec[i] += 1
filldecreasing!(vec, i)
else
push!(vec, 1)
filldecreasing!(vec, len + 1)
end
elseif vec[i] < vec[i + 1] && vec[i] < bas - 2
vec[i] += 2
filldecreasing!(vec, i)
break
end
end
end
end
return vec
end
 
for b in 2:16
println("For base $b, the esthetic numbers indexed from $(4b) to $(6b) are:")
printed = 0
for (i, n) in enumerate(Iterators.take(Esthetic{Int}(1, b), 6b))
if i >= 4b
printed += 1
print(string(n, base=b), printed % 21 == 20 ? "\n" : " ")
end
end
println("\n")
end
 
for (bottom, top, cols, T) in [[1000, 9999, 16, Int], [100_000_000, 130_000_000, 8, Int],
[101_010_000_000, 130_000_000_000, 6, Int], [101_010_101_010_000_000, 130_000_000_000_000_000, 4, Int],
[101_010_101_010_101_000_000, 130_000_000_000_000_000_000, 4, Int128]]
esth, count = Esthetic{T}(bottom, 10, top), 0
println("\nBase 10 esthetic numbers between $(format(bottom, commas=true)) and $(format(top, commas=true)):")
for n in esth
count += 1
if count == 64
println(" ...")
elseif count < 64
print(format(n, commas=true), count % cols == 0 ? "\n" : " ")
end
end
println("\nTotal esthetic numbers in interval: $count")
end
 
Output:
For base 2, the esthetic numbers indexed from 8 to 12 are:
10101010 101010101 1010101010 10101010101 101010101010

For base 3, the esthetic numbers indexed from 12 to 18 are:
1210 1212 2101 2121 10101 10121 12101

For base 4, the esthetic numbers indexed from 16 to 24 are:
323 1010 1012 1210 1212 1232 2101 2121 2123

For base 5, the esthetic numbers indexed from 20 to 30 are:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101

For base 6, the esthetic numbers indexed from 24 to 36 are:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234

For base 7, the esthetic numbers indexed from 28 to 42 are:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232

For base 8, the esthetic numbers indexed from 32 to 48 are:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212

For base 9, the esthetic numbers indexed from 36 to 54 are:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210

For base 10, the esthetic numbers indexed from 40 to 60 are:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010
1012

For base 11, the esthetic numbers indexed from 44 to 66 are:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9
a98 a9a 1010

For base 12, the esthetic numbers indexed from 48 to 72 are:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab
a98 a9a aba ba9 bab

For base 13, the esthetic numbers indexed from 52 to 78 are:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98
a9a aba abc ba9 bab bcb cba

For base 14, the esthetic numbers indexed from 56 to 84 are:
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a
aba abc ba9 bab bcb bcd cba cbc cdc

For base 15, the esthetic numbers indexed from 60 to 90 are:
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba
abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd

For base 16, the esthetic numbers indexed from 64 to 96 are:
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc
ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc


Base 10 esthetic numbers between 1,000 and 9,999:
1,010 1,012 1,210 1,212 1,232 1,234 2,101 2,121 2,123 2,321 2,323 2,343 2,345 3,210 3,212 3,232
3,234 3,432 3,434 3,454 3,456 4,321 4,323 4,343 4,345 4,543 4,545 4,565 4,567 5,432 5,434 5,454
5,456 5,654 5,656 5,676 5,678 6,543 6,545 6,565 6,567 6,765 6,767 6,787 6,789 7,654 7,656 7,676
7,678 7,876 7,878 7,898 8,765 8,767 8,787 8,789 8,987 8,989 9,876 9,878 9,898
Total esthetic numbers in interval: 61

Base 10 esthetic numbers between 100,000,000 and 130,000,000:
101,010,101 101,010,121 101,010,123 101,012,101 101,012,121 101,012,123 101,012,321 101,012,323
101,012,343 101,012,345 101,210,101 101,210,121 101,210,123 101,212,101 101,212,121 101,212,123
101,212,321 101,212,323 101,212,343 101,212,345 101,232,101 101,232,121 101,232,123 101,232,321
101,232,323 101,232,343 101,232,345 101,234,321 101,234,323 101,234,343 101,234,345 101,234,543
101,234,545 101,234,565 101,234,567 121,010,101 121,010,121 121,010,123 121,012,101 121,012,121
121,012,123 121,012,321 121,012,323 121,012,343 121,012,345 121,210,101 121,210,121 121,210,123
121,212,101 121,212,121 121,212,123 121,212,321 121,212,323 121,212,343 121,212,345 121,232,101
121,232,121 121,232,123 121,232,321 121,232,323 121,232,343 121,232,345 121,234,321  ...

Total esthetic numbers in interval: 126

Base 10 esthetic numbers between 101,010,000,000 and 130,000,000,000:
101,010,101,010 101,010,101,012 101,010,101,210 101,010,101,212 101,010,101,232 101,010,101,234
101,010,121,010 101,010,121,012 101,010,121,210 101,010,121,212 101,010,121,232 101,010,121,234
101,010,123,210 101,010,123,212 101,010,123,232 101,010,123,234 101,010,123,432 101,010,123,434
101,010,123,454 101,010,123,456 101,012,101,010 101,012,101,012 101,012,101,210 101,012,101,212
101,012,101,232 101,012,101,234 101,012,121,010 101,012,121,012 101,012,121,210 101,012,121,212
101,012,121,232 101,012,121,234 101,012,123,210 101,012,123,212 101,012,123,232 101,012,123,234
101,012,123,432 101,012,123,434 101,012,123,454 101,012,123,456 101,012,321,010 101,012,321,012
101,012,321,210 101,012,321,212 101,012,321,232 101,012,321,234 101,012,323,210 101,012,323,212
101,012,323,232 101,012,323,234 101,012,323,432 101,012,323,434 101,012,323,454 101,012,323,456
101,012,343,210 101,012,343,212 101,012,343,232 101,012,343,234 101,012,343,432 101,012,343,434
101,012,343,454 101,012,343,456 101,012,345,432  ...

Total esthetic numbers in interval: 911

Base 10 esthetic numbers between 101,010,101,010,000,000 and 130,000,000,000,000,000:
101,010,101,010,101,010 101,010,101,010,101,012 101,010,101,010,101,210 101,010,101,010,101,212
101,010,101,010,101,232 101,010,101,010,101,234 101,010,101,010,121,010 101,010,101,010,121,012
101,010,101,010,121,210 101,010,101,010,121,212 101,010,101,010,121,232 101,010,101,010,121,234
101,010,101,010,123,210 101,010,101,010,123,212 101,010,101,010,123,232 101,010,101,010,123,234
101,010,101,010,123,432 101,010,101,010,123,434 101,010,101,010,123,454 101,010,101,010,123,456
101,010,101,012,101,010 101,010,101,012,101,012 101,010,101,012,101,210 101,010,101,012,101,212
101,010,101,012,101,232 101,010,101,012,101,234 101,010,101,012,121,010 101,010,101,012,121,012
101,010,101,012,121,210 101,010,101,012,121,212 101,010,101,012,121,232 101,010,101,012,121,234
101,010,101,012,123,210 101,010,101,012,123,212 101,010,101,012,123,232 101,010,101,012,123,234
101,010,101,012,123,432 101,010,101,012,123,434 101,010,101,012,123,454 101,010,101,012,123,456
101,010,101,012,321,010 101,010,101,012,321,012 101,010,101,012,321,210 101,010,101,012,321,212
101,010,101,012,321,232 101,010,101,012,321,234 101,010,101,012,323,210 101,010,101,012,323,212
101,010,101,012,323,232 101,010,101,012,323,234 101,010,101,012,323,432 101,010,101,012,323,434
101,010,101,012,323,454 101,010,101,012,323,456 101,010,101,012,343,210 101,010,101,012,343,212
101,010,101,012,343,232 101,010,101,012,343,234 101,010,101,012,343,432 101,010,101,012,343,434
101,010,101,012,343,454 101,010,101,012,343,456 101,010,101,012,345,432  ...

Total esthetic numbers in interval: 44744

Base 10 esthetic numbers between 101,010,101,010,101,000,000 and 130,000,000,000,000,000,000:
101,010,101,010,101,010,101 101,010,101,010,101,010,121 101,010,101,010,101,010,123 101,010,101,010,101,012,101
101,010,101,010,101,012,121 101,010,101,010,101,012,123 101,010,101,010,101,012,321 101,010,101,010,101,012,323
101,010,101,010,101,012,343 101,010,101,010,101,012,345 101,010,101,010,101,210,101 101,010,101,010,101,210,121
101,010,101,010,101,210,123 101,010,101,010,101,212,101 101,010,101,010,101,212,121 101,010,101,010,101,212,123
101,010,101,010,101,212,321 101,010,101,010,101,212,323 101,010,101,010,101,212,343 101,010,101,010,101,212,345
101,010,101,010,101,232,101 101,010,101,010,101,232,121 101,010,101,010,101,232,123 101,010,101,010,101,232,321
101,010,101,010,101,232,323 101,010,101,010,101,232,343 101,010,101,010,101,232,345 101,010,101,010,101,234,321
101,010,101,010,101,234,323 101,010,101,010,101,234,343 101,010,101,010,101,234,345 101,010,101,010,101,234,543
101,010,101,010,101,234,545 101,010,101,010,101,234,565 101,010,101,010,101,234,567 101,010,101,010,121,010,101
101,010,101,010,121,010,121 101,010,101,010,121,010,123 101,010,101,010,121,012,101 101,010,101,010,121,012,121
101,010,101,010,121,012,123 101,010,101,010,121,012,321 101,010,101,010,121,012,323 101,010,101,010,121,012,343
101,010,101,010,121,012,345 101,010,101,010,121,210,101 101,010,101,010,121,210,121 101,010,101,010,121,210,123
101,010,101,010,121,212,101 101,010,101,010,121,212,121 101,010,101,010,121,212,123 101,010,101,010,121,212,321
101,010,101,010,121,212,323 101,010,101,010,121,212,343 101,010,101,010,121,212,345 101,010,101,010,121,232,101
101,010,101,010,121,232,121 101,010,101,010,121,232,123 101,010,101,010,121,232,321 101,010,101,010,121,232,323
101,010,101,010,121,232,343 101,010,101,010,121,232,345 101,010,101,010,121,234,321  ...

Total esthetic numbers in interval: 312019

Kotlin[edit]

Translation of: D
import kotlin.math.abs
 
fun isEsthetic(n: Long, b: Long): Boolean {
if (n == 0L) {
return false
}
var i = n % b
var n2 = n / b
while (n2 > 0) {
val j = n2 % b
if (abs(i - j) != 1L) {
return false
}
n2 /= b
i = j
}
return true
}
 
fun listEsths(n: Long, n2: Long, m: Long, m2: Long, perLine: Int, all: Boolean) {
val esths = mutableListOf<Long>()
fun dfs(n: Long, m: Long, i: Long) {
if (i in n..m) {
esths.add(i)
}
if (i == 0L || i > m) {
return
}
val d = i % 10
val i1 = i * 10 + d - 1
val i2 = i1 + 2
when (d) {
0L -> {
dfs(n, m, i2)
}
9L -> {
dfs(n, m, i1)
}
else -> {
dfs(n, m, i1)
dfs(n, m, i2)
}
}
}
 
for (i in 0L until 10L) {
dfs(n2, m2, i)
}
 
val le = esths.size
println("Base 10: $le esthetic numbers between $n and $m:")
if (all) {
for (c_esth in esths.withIndex()) {
print("${c_esth.value} ")
if ((c_esth.index + 1) % perLine == 0) {
println()
}
}
println()
} else {
for (i in 0 until perLine) {
print("${esths[i]} ")
}
println()
println("............")
for (i in le - perLine until le) {
print("${esths[i]} ")
}
println()
}
println()
}
 
fun main() {
for (b in 2..16) {
println("Base $b: ${4 * b}th to ${6 * b}th esthetic numbers:")
var n = 1L
var c = 0L
while (c < 6 * b) {
if (isEsthetic(n, b.toLong())) {
c++
if (c >= 4 * b) {
print("${n.toString(b)} ")
}
}
n++
}
println()
}
println()
 
// the following all use the obvious range limitations for the numbers in question
listEsths(1000, 1010, 9999, 9898, 16, true);
listEsths(1e8.toLong(), 101_010_101, 13 * 1e7.toLong(), 123_456_789, 9, true);
listEsths(1e11.toLong(), 101_010_101_010, 13 * 1e10.toLong(), 123_456_789_898, 7, false);
listEsths(1e14.toLong(), 101_010_101_010_101, 13 * 1e13.toLong(), 123_456_789_898_989, 5, false);
listEsths(1e17.toLong(), 101_010_101_010_101_010, 13 * 1e16.toLong(), 123_456_789_898_989_898, 4, false);
}
Output:
Base 2: 8th to 12th esthetic numbers:
10101010 101010101 1010101010 10101010101 101010101010 
Base 3: 12th to 18th esthetic numbers:
1210 1212 2101 2121 10101 10121 12101 
Base 4: 16th to 24th esthetic numbers:
323 1010 1012 1210 1212 1232 2101 2121 2123 
Base 5: 20th to 30th esthetic numbers:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101 
Base 6: 24th to 36th esthetic numbers:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234 
Base 7: 28th to 42th esthetic numbers:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232 
Base 8: 32th to 48th esthetic numbers:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212 
Base 9: 36th to 54th esthetic numbers:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210 
Base 10: 40th to 60th esthetic numbers:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 
Base 11: 44th to 66th esthetic numbers:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010 
Base 12: 48th to 72th esthetic numbers:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab 
Base 13: 52th to 78th esthetic numbers:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba 
Base 14: 56th to 84th esthetic numbers:
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc 
Base 15: 60th to 90th esthetic numbers:
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd 
Base 16: 64th to 96th esthetic numbers:
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc 

Base 10: 61 esthetic numbers between 1000 and 9999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676 
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898 

Base 10: 126 esthetic numbers between 100000000 and 130000000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343 
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323 
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345 
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101 
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345 
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343 
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321 
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121 
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101 
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343 
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321 
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545 
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565 
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789 


Base 10: 911 esthetic numbers between 100000000000 and 130000000000:
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010 
............
123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898 

Base 10: 6225 esthetic numbers between 100000000000000 and 130000000000000:
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121 
............
123456789898767 123456789898787 123456789898789 123456789898987 123456789898989 

Base 10: 44744 esthetic numbers between 100000000000000000 and 130000000000000000:
101010101010101010 101010101010101012 101010101010101210 101010101010101212 
............
123456789898987898 123456789898989876 123456789898989878 123456789898989898 

Pascal[edit]

Simple brute force, but fast and simple counting for complete first digit ranges.

program Esthetic;
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$codealign proc=16}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils,//IntToStr
strutils;//Numb2USA aka commatize
const
ConvBase :array[0..15] of char= '0123456789ABCDEF';
maxBase = 16;
type
tErg = string[63];
tCnt = array[0..maxBase-1] of UInt64;
tDgtcnt = array[0..64] of tCnt;
 
//global
var
Dgtcnt :tDgtcnt;
 
procedure CalcDgtCnt(base:NativeInt;var Dgtcnt :tDgtcnt);
var
pCnt0,
pCnt1 : ^tCnt;
i,j,SumCarry: NativeUInt;
begin
fillchar(Dgtcnt,SizeOf(Dgtcnt),#0);
pCnt0 := @Dgtcnt[0];
//building count for every first digit of digitcount:
//example :count numbers starting "1" of lenght 13
For i := 0 to Base-1 do
pCnt0^[i] := 1;
For j := 1 to High(Dgtcnt) do
Begin
pCnt1 := @Dgtcnt[j];
//0 -> followed only by solutions of 1
pCnt1^[0] := pCnt0^[1];
//base-1 -> followed only by solutions of Base-2
pCnt1^[base-1] := pCnt0^[base-2];
//followed by solutions for i-1 and i+1
For i := 1 to base-2 do
pCnt1^[i]:= pCnt0^[i-1]+pCnt0^[i+1];
//next row aka digitcnt
pCnt0:= pCnt1;
end;
 
//converting to sum up each digit
//example :count numbers starting "1" of lenght 13
//-> count of all est. numbers from 1 to "1" with max lenght 13
 
//delete leading "0"
For j := 0 to High(Dgtcnt) do //High(Dgtcnt)
Dgtcnt[j,0] := 0;
 
SumCarry := Uint64(0);
For j := 0 to High(Dgtcnt) do
Begin
pCnt0 := @Dgtcnt[j];
For i := 0 to base-1 do
begin
SumCarry +=pCnt0^[i];
pCnt0^[i] :=SumCarry;
end;
end;
end;
 
function ConvToBaseStr(n,base:NativeUint):tErg;
var
idx,dgt,rst : Uint64;
Begin
IF n = 0 then
Begin
result := ConvBase[0];
EXIT;
end;
idx := High(result);
repeat
rst := n div base;
dgt := n-rst*base;
result[idx] := ConvBase[dgt];
dec(idx);
n := rst;
until n=0;
rst := High(result)-idx;
move(result[idx+1],result[1],rst);
setlength(result,rst);
end;
 
function isEsthetic(n,base:Uint64):boolean;
var
lastdgt,
dgt,
rst : Uint64;
Begin
result := true;
IF n >= Base then
Begin
rst := n div base;
Lastdgt := n-rst*base;
n := rst;
repeat
rst := n div base;
dgt := n-rst*base;
IF sqr(lastDgt-dgt)<> 1 then
Begin
result := false;
EXIT;
end;
lastDgt := dgt;
n := rst;
until n = 0;
end;
end;
 
procedure Task1;
var
i,base,cnt : NativeInt;
Begin
cnt := 0;
For base := 2 to 16 do
Begin
CalcDgtCnt(base,Dgtcnt);
writeln(4*base,'th through ',6*base,'th esthetic numbers in base ',base);
cnt := 0;
i := 0;
repeat
inc(i);
if isEsthetic(i,base) then
inc(cnt);
until cnt >= 4*base;
 
repeat
if isEsthetic(i,base) then
Begin
write(ConvToBaseStr(i,base),' ');
inc(cnt);
end;
inc(i);
until cnt > 6*base;
writeln;
end;
writeln;
end;
 
procedure Task2;
var
i : NativeInt;
begin
write(' There are ',Dgtcnt[4][0]-Dgtcnt[3][0],' esthetic numbers');
writeln(' between 1000 and 9999 ');
For i := 1000 to 9999 do
Begin
if isEsthetic(i,10) then
write(i:5);
end;
writeln;writeln;
end;
 
procedure Task3(Pot10: NativeInt);
//calculating esthetic numbers starting with "1" and Pot10+1 digits
var
i : NativeInt;
begin
write(' There are ',Numb2USA(IntToStr(Dgtcnt[Pot10][1]-Dgtcnt[Pot10][0])):26,' esthetic numbers');
writeln(' between 1e',Pot10,' and 1.3e',Pot10);
if Pot10 = 8 then
Begin
For i := 100*1000*1000 to 110*1000*1000-1 do
Begin
if isEsthetic(i,10) then
write(i:10);
end;
writeln;
//Jump over "11"
For i := 120*1000*1000 to 130*1000*1000-1 do
Begin
if isEsthetic(i,10) then
write(i:10);
end;
writeln;writeln;
end;
end;
 
var
i:NativeInt;
BEGIN
Task1;
//now only base 10 is used
CalcDgtCnt(10,Dgtcnt);
Task2;
For i := 2 to 20 do
Task3(3*i+2);
writeln;
write(' There are ',Numb2USA(IntToStr(Dgtcnt[64][0])),' esthetic numbers');
writeln(' with max 65 digits ');
writeln;
writeln(' The count of numbers with 64 digits like https://oeis.org/A090994');
writeln(Numb2USA(IntToStr(Dgtcnt[64][0]-Dgtcnt[63][0])):28);
end.
 
Output:
8th through 12th esthetic numbers in base 2
10101010 101010101 1010101010 10101010101 101010101010
12th through 18th esthetic numbers in base 3
1210 1212 2101 2121 10101 10121 12101
16th through 24th esthetic numbers in base 4
323 1010 1012 1210 1212 1232 2101 2121 2123
20th through 30th esthetic numbers in base 5
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
24th through 36th esthetic numbers in base 6
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
28th through 42th esthetic numbers in base 7
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
32th through 48th esthetic numbers in base 8
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
36th through 54th esthetic numbers in base 9
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012
1210
40th through 60th esthetic numbers in base 10
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989
1010 1012
44th through 66th esthetic numbers in base 11
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989
9A9 A98 A9A 1010
48th through 72th esthetic numbers in base 12
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9
9AB A98 A9A ABA BA9 BAB
52th through 78th esthetic numbers in base 13
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB
A98 A9A ABA ABC BA9 BAB BCB CBA
56th through 84th esthetic numbers in base 14
565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98
A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC
60th through 90th esthetic numbers in base 15
567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A
ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD
64th through 96th esthetic numbers in base 16
654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB A98 A9A ABA
ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD DED DEF EDC

 There are 61 esthetic numbers between 1000 and 9999
 1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212
 3232 3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432
 5434 5454 5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789
 7654 7656 7676 7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878
 9898

 There are                        126 esthetic numbers between 1e8 and 1.3e8
 101010101 101010121 101010123 101012101 101012121 101012123 101012321
 101012323 101012343 101012345 101210101 101210121 101210123 101212101
 101212121 101212123 101212321 101212323 101212343 101212345 101232101
 101232121 101232123 101232321 101232323 101232343 101232345 101234321
 101234323 101234343 101234345 101234543 101234545 101234565 101234567
 121010101 121010121 121010123 121012101 121012121 121012123 121012321
 121012323 121012343 121012345 121210101 121210121 121210123 121212101
 121212121 121212123 121212321 121212323 121212343 121212345 121232101
 121232121 121232123 121232321 121232323 121232343 121232345 121234321
 121234323 121234343 121234345 121234543 121234545 121234565 121234567
 123210101 123210121 123210123 123212101 123212121 123212123 123212321
 123212323 123212343 123212345 123232101 123232121 123232123 123232321
 123232323 123232343 123232345 123234321 123234323 123234343 123234345
 123234543 123234545 123234565 123234567 123432101 123432121 123432123
 123432321 123432323 123432343 123432345 123434321 123434323 123434343
 123434345 123434543 123434545 123434565 123434567 123454321 123454323
 123454343 123454345 123454543 123454545 123454565 123454567 123456543
 123456545 123456565 123456567 123456765 123456767 123456787 123456789
 There are                        911 esthetic numbers between 1e11 and 1.3e11
 There are                      6,225 esthetic numbers between 1e14 and 1.3e14
 There are                     44,744 esthetic numbers between 1e17 and 1.3e17
 There are                    312,019 esthetic numbers between 1e20 and 1.3e20
 There are                  2,223,504 esthetic numbers between 1e23 and 1.3e23
 There are                 15,621,426 esthetic numbers between 1e26 and 1.3e26
 There are                110,820,165 esthetic numbers between 1e29 and 1.3e29
 There are                781,074,572 esthetic numbers between 1e32 and 1.3e32
 There are              5,529,362,694 esthetic numbers between 1e35 and 1.3e35
 There are             39,027,676,220 esthetic numbers between 1e38 and 1.3e38
 There are            276,017,648,570 esthetic numbers between 1e41 and 1.3e41
 There are          1,949,472,483,601 esthetic numbers between 1e44 and 1.3e44
 There are         13,781,324,308,298 esthetic numbers between 1e47 and 1.3e47
 There are         97,364,252,272,077 esthetic numbers between 1e50 and 1.3e50
 There are        688,156,000,065,766 esthetic numbers between 1e53 and 1.3e53
 There are      4,862,434,535,536,899 esthetic numbers between 1e56 and 1.3e56
 There are     34,363,852,859,396,807 esthetic numbers between 1e59 and 1.3e59
 There are    242,826,004,764,166,201 esthetic numbers between 1e62 and 1.3e62

 There are 12,010,980,988,448,085,902 esthetic numbers with max 65 digits

 The count of numbers with 64 digits like https://oeis.org/A090994
   5,751,957,590,040,265,961

Perl[edit]

Library: ntheory
Translation of: Sidef
use 5.020;
use warnings;
use experimental qw(signatures);
 
use ntheory qw(fromdigits todigitstring);
 
sub generate_esthetic ($root, $upto, $callback, $base = 10) {
 
my $v = fromdigits($root, $base);
 
return if ($v > $upto);
$callback->($v);
 
my $t = $root->[-1];
 
__SUB__->([@$root, $t + 1], $upto, $callback, $base) if ($t + 1 < $base);
__SUB__->([@$root, $t - 1], $upto, $callback, $base) if ($t - 1 >= 0);
}
 
sub between_esthetic ($from, $upto, $base = 10) {
my @list;
foreach my $k (1 .. $base - 1) {
generate_esthetic([$k], $upto,
sub($n) { push(@list, $n) if ($n >= $from) }, $base);
}
sort { $a <=> $b } @list;
}
 
sub first_n_esthetic ($n, $base = 10) {
for (my $m = $n * $n ; 1 ; $m *= $base) {
my @list = between_esthetic(1, $m, $base);
return @list[0 .. $n - 1] if @list >= $n;
}
}
 
foreach my $base (2 .. 16) {
say "\n$base-esthetic numbers at indices ${\(4*$base)}..${\(6*$base)}:";
my @list = first_n_esthetic(6 * $base, $base);
say join(' ', map { todigitstring($_, $base) } @list[4*$base-1 .. $#list]);
}
 
say "\nBase 10 esthetic numbers between 1,000 and 9,999:";
for (my @list = between_esthetic(1e3, 1e4) ; @list ;) {
say join(' ', splice(@list, 0, 20));
}
 
say "\nBase 10 esthetic numbers between 100,000,000 and 130,000,000:";
for (my @list = between_esthetic(1e8, 1.3e8) ; @list ;) {
say join(' ', splice(@list, 0, 9));
}
Output:
2-esthetic numbers at indices 8..12:
10101010 101010101 1010101010 10101010101 101010101010

3-esthetic numbers at indices 12..18:
1210 1212 2101 2121 10101 10121 12101

4-esthetic numbers at indices 16..24:
323 1010 1012 1210 1212 1232 2101 2121 2123

5-esthetic numbers at indices 20..30:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101

6-esthetic numbers at indices 24..36:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234

7-esthetic numbers at indices 28..42:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232

8-esthetic numbers at indices 32..48:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212

9-esthetic numbers at indices 36..54:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210

10-esthetic numbers at indices 40..60:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012

11-esthetic numbers at indices 44..66:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010

12-esthetic numbers at indices 48..72:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab

13-esthetic numbers at indices 52..78:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba

14-esthetic numbers at indices 56..84:
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc

15-esthetic numbers at indices 60..90:
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd

16-esthetic numbers at indices 64..96:
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc

Base 10 esthetic numbers between 1,000 and 9,999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 3234 3432 3434 3454
3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 5456 5654 5656 5676 5678 6543 6545 6565
6567 6765 6767 6787 6789 7654 7656 7676 7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878
9898

Base 10 esthetic numbers between 100,000,000 and 130,000,000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789

Phix[edit]

Simple string based approach, very fast, manages stretch goal and much further in the blink of an eye.

constant aleph = "0123456789ABCDEF"
 
function efill(string s, integer ch, i)
-- min-fill, like 10101 or 54321 or 32101
s[i] = ch
for j=i+1 to length(s) do
ch = iff(ch>='1'?iff(ch='A'?'9':ch-1):'1')
s[j] = ch
end for
return s
end function
 
function esthetic(string s, integer base = 10)
-- generate the next esthetic number after s
-- (nb unpredictable results if s is not esthetic, or "")
for i=length(s) to 1 by -1 do
integer ch = s[i], cp = iff(i>1?s[i-1]:'0')
if ch<cp and cp<aleph[base] then
return efill(s,aleph[find(ch,aleph)+2],i)
elsif i=1 and ch<aleph[base] then
return efill(s,iff(ch='9'?'A':ch+1),i)
end if
end for
return efill("1"&s,'1',1)
end function
 
string s
sequence res
for base=2 to 16 do
integer hi = base*6,
lo = base*4
{s,res} = {"",{}}
for i=1 to hi do
s = esthetic(s, base)
if i>=lo then
res = append(res,s)
end if
end for
res = join(shorten(res,"numbers",4))
printf(1,"Base %d esthetic numbers[%d..%d]: %s\n",{base,lo,hi,res})
end for
 
{s,res} = {efill("1000",'1',1),{}}
while length(s)=4 do
res = append(res,s)
s = esthetic(s)
end while
res = {join(shorten(res,"numbers",5))}
printf(1,"\nBase 10 esthetic numbers between 1,000 and 9,999: %s\n\n",res)
 
function comma(string s)
for i=length(s)-2 to 2 by -3 do
s[i..i-1] = ","
end for
return s
end function
 
for k=7 to 19 by 3 do
string f = "10"&repeat('0',k),
t = "13"&repeat('0',k)
{s,res} = {efill(f,'1',1),{}}
while s<t do
res = append(res,s)
s = esthetic(s)
end while
res = join(shorten(res,"numbers",1))
printf(1,"Base 10 esthetic numbers between %s and %s: %s\n",
{comma(f),comma(t),res})
end for
Output:
Base 2 esthetic numbers[8..12]: 10101010 101010101 1010101010 10101010101 101010101010
Base 3 esthetic numbers[12..18]: 1210 1212 2101 2121 10101 10121 12101
Base 4 esthetic numbers[16..24]: 323 1010 1012 1210 1212 1232 2101 2121 2123
Base 5 esthetic numbers[20..30]: 323 343 432 434 ... 1212 1232 1234 2101  (11 numbers)
Base 6 esthetic numbers[24..36]: 343 345 432 434 ... 1210 1212 1232 1234  (13 numbers)
Base 7 esthetic numbers[28..42]: 345 432 434 454 ... 1012 1210 1212 1232  (15 numbers)
Base 8 esthetic numbers[32..48]: 432 434 454 456 ... 1010 1012 1210 1212  (17 numbers)
Base 9 esthetic numbers[36..54]: 434 454 456 543 ... 878 1010 1012 1210  (19 numbers)
Base 10 esthetic numbers[40..60]: 454 456 543 545 ... 987 989 1010 1012  (21 numbers)
Base 11 esthetic numbers[44..66]: 456 543 545 565 ... 9A9 A98 A9A 1010  (23 numbers)
Base 12 esthetic numbers[48..72]: 543 545 565 567 ... A9A ABA BA9 BAB  (25 numbers)
Base 13 esthetic numbers[52..78]: 545 565 567 654 ... BA9 BAB BCB CBA  (27 numbers)
Base 14 esthetic numbers[56..84]: 565 567 654 656 ... BCD CBA CBC CDC  (29 numbers)
Base 15 esthetic numbers[60..90]: 567 654 656 676 ... CDC CDE DCB DCD  (31 numbers)
Base 16 esthetic numbers[64..96]: 654 656 676 678 ... DCD DED DEF EDC  (33 numbers)

Base 10 esthetic numbers between 1,000 and 9,999: 1010 1012 1210 1212 1232 ... 8987 8989 9876 9878 9898  (61 numbers)

Base 10 esthetic numbers between 100,000,000 and 130,000,000: 101010101 ... 123456789  (126 numbers)
Base 10 esthetic numbers between 100,000,000,000 and 130,000,000,000: 101010101010 ... 123456789898  (911 numbers)
Base 10 esthetic numbers between 100,000,000,000,000 and 130,000,000,000,000: 101010101010101 ... 123456789898989  (6,225 numbers)
Base 10 esthetic numbers between 100,000,000,000,000,000 and 130,000,000,000,000,000: 101010101010101010 ... 123456789898989898  (44,744 numbers)
Base 10 esthetic numbers between 100,000,000,000,000,000,000 and 130,000,000,000,000,000,000: 101010101010101010101 ... 123456789898989898989  (312,019 numbers)

Raku[edit]

Works with: Rakudo version 2020.02
use Lingua::EN::Numbers;
 
sub esthetic($base = 10) {
my @s = ^$base .map: -> \s {
((s - 1).base($base) if s > 0), ((s + 1).base($base) if s < $base - 1)
}
 
flat [ (1 .. $base - 1)».base($base) ],
{ [ flat .map: { $_ xx * Z~ flat @s[.comb.tail.parse-base($base)] } ] }*
}
 
for 2 .. 16 -> $b {
put "\n{(4 × $b).&ordinal-digit} through {(6 × $b).&ordinal-digit} esthetic numbers in base $b";
put esthetic($b)[(4 × $b - 1) .. (6 × $b - 1)]».fmt('%3s').batch(16).join: "\n"
}
 
my @e10 = esthetic;
put "\nBase 10 esthetic numbers between 1,000 and 9,999:";
put @e10.&between(1000, 9999).batch(20).join: "\n";
 
put "\nBase 10 esthetic numbers between {1e8.Int.&comma} and {1.3e8.Int.&comma}:";
put @e10.&between(1e8.Int, 1.3e8.Int).batch(9).join: "\n";
 
sub between (@array, Int $lo, Int $hi) {
my $top = @array.first: * > $hi, :k;
@array[^$top].grep: * > $lo
}
Output:
8th through 12th esthetic numbers in base 2
10101010 101010101 1010101010 10101010101 101010101010

12th through 18th esthetic numbers in base 3
1210 1212 2101 2121 10101 10121 12101

16th through 24th esthetic numbers in base 4
323 1010 1012 1210 1212 1232 2101 2121 2123

20th through 30th esthetic numbers in base 5
323 343 432 434 1010 1012 1210 1212 1232 1234 2101

24th through 36th esthetic numbers in base 6
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234

28th through 42nd esthetic numbers in base 7
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232

32nd through 48th esthetic numbers in base 8
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210
1212

36th through 54th esthetic numbers in base 9
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878
1010 1012 1210

40th through 60th esthetic numbers in base 10
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878
898 987 989 1010 1012

44th through 66th esthetic numbers in base 11
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898
89A 987 989 9A9 A98 A9A 1010

48th through 72nd esthetic numbers in base 12
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A
987 989 9A9 9AB A98 A9A ABA BA9 BAB

52nd through 78th esthetic numbers in base 13
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987
989 9A9 9AB A98 A9A ABA ABC BA9 BAB BCB CBA

56th through 84th esthetic numbers in base 14
565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989
9A9 9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC

60th through 90th esthetic numbers in base 15
567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9
9AB A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD

64th through 96th esthetic numbers in base 16
654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 9AB
A98 A9A ABA ABC BA9 BAB BCB BCD CBA CBC CDC CDE DCB DCD DED DEF
EDC

Base 10 esthetic numbers between 1,000 and 9,999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 3234 3432 3434 3454
3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 5456 5654 5656 5676 5678 6543 6545 6565
6567 6765 6767 6787 6789 7654 7656 7676 7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878
9898

Base 10 esthetic numbers between 100,000,000 and 130,000,000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789

REXX[edit]

/*REXX pgm lists a bunch of esthetic numbers in bases 2 ──► 16, & base 10 in two ranges.*/
parse arg baseL baseH range /*obtain optional arguments from the CL*/
if baseL=='' | baseL=="," then baseL= 2 /*Not specified? Then use the default.*/
if baseH=='' | baseH=="," then baseH=16 /* " " " " " " */
if range=='' | range=="," then range=1000..9999 /* " " " " " " */
 
do radix=baseL to baseH; #= 0; if radix<2 then iterate /*process the bases. */
start= radix * 4; stop = radix * 6
$=; do i=1 until #==stop; y= base(i, radix) /*convert I to base Y*/
if \esthetic(y, radix) then iterate /*not esthetic? Skip*/
#= # + 1; if #<start then iterate /*is # below range?*/
$= $ y /*append # to $ list.*/
end /*i*/
say
say center(' base ' radix", the" th(start) '──►' th(stop) ,
"esthetic numbers ", max(80, length($) - 1), '─')
say strip($)
end /*radix*/
say; g= 25
parse var range start '..' stop
say center(' base 10 esthetic numbers between' start "and" stop '(inclusive) ', g*5-1,"─")
#= 0; $=
do k=start to stop; if \esthetic(k, 10) then iterate; #= # + 1; $= $ k
if #//g==0 then do; say strip($); $=; end
end /*k*/
say strip($); say; say # ' esthetic numbers listed.'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
PorA: _= pos(z, @u); p= substr(@u, _-1, 1); a= substr(@u, _+1, 1); return
th: parse arg th; return th || word('th st nd rd', 1+(th//10)*(th//100%10\==1)*(th//10<4))
vv: parse arg v,_; vr= x2d(v) + _; if vr==-1 then vr= r; return d2x(vr)
/*──────────────────────────────────────────────────────────────────────────────────────*/
base: procedure expose @u; arg x 1 #,toB,inB,,y /*Y is assigned a "null" value. */
if tob=='' then tob= 10 /*maybe assign a default for TObase. */
if inb=='' then inb= 10 /* " " " " " INbase. */
@l= '0123456789abcdef'; @u= @l; upper @u /*two versions of hexadecimal digits. */
if inB\==10 then do; #= 0 /*only convert if not base 10. */
do j=1 for length(x) /*convert X: base inB ──► base 10. */
#= # * inB + pos(substr(x, j, 1), @u) -1 /*build new number.*/
end /*j*/ /* [↑] this also verifies digits. */
end
if toB==10 then return # /*if TOB is ten, then simply return #.*/
do while # >= toB /*convert #: base 10 ──► base toB.*/
y= substr(@l, (# // toB) + 1, 1)y /*construct the output number. */
#= # % toB /* ··· and whittle # down also. */
end /*while*/ /* [↑] algorithm may leave a residual.*/
return substr(@l, # + 1, 1)y /*prepend the residual, if any. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
esthetic: procedure expose @u; arg x,r; L= length(x); if L==1 then return 1
if x<2 then return 0
do d=0 to r-1; _= d2x(d); if pos(_ || _, x)\==0 then return 0
end /*d*/ /* [↑] check for a duplicated digits. */
do j=1 for L; @.j= substr(x, j, 1) /*assign (base) digits to stemmed array*/
end /*j*/
if L==2 then do; z= @.1; call PorA; if @.2==p | @.2==a then nop
else return 0
end
do e=2 to L-1; z= @.e; pe= e - 1; ae= e + 1
if (z==vv(@.pe,-1)|z==vv(@.pe,1))&(z==vv(@.ae,-1)|z==vv(@.ae,1)) then iterate
return 0
end /*e*/; return 1
output   when using the default inputs:
───────────────── base  2,  the 8th ──► 12th esthetic numbers ──────────────────
10101010 101010101 1010101010 10101010101 101010101010

───────────────── base  3,  the 12th ──► 18th esthetic numbers ─────────────────
1210 1212 2101 2121 10101 10121 12101

───────────────── base  4,  the 16th ──► 24th esthetic numbers ─────────────────
323 1010 1012 1210 1212 1232 2101 2121 2123

───────────────── base  5,  the 20th ──► 30th esthetic numbers ─────────────────
323 343 432 434 1010 1012 1210 1212 1232 1234 2101

───────────────── base  6,  the 24th ──► 36th esthetic numbers ─────────────────
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234

───────────────── base  7,  the 28th ──► 42nd esthetic numbers ─────────────────
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232

───────────────── base  8,  the 32nd ──► 48th esthetic numbers ─────────────────
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212

───────────────── base  9,  the 36th ──► 54th esthetic numbers ─────────────────
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210

─────────────────── base  10,  the 40th ──► 60th esthetic numbers ───────────────────
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012

────────────────────── base  11,  the 44th ──► 66th esthetic numbers ───────────────────────
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010

────────────────────────── base  12,  the 48th ──► 72nd esthetic numbers ──────────────────────────
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab

────────────────────────────── base  13,  the 52nd ──► 78th esthetic numbers ──────────────────────────────
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba

────────────────────────────────── base  14,  the 56th ──► 84th esthetic numbers ──────────────────────────────────
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc

────────────────────────────────────── base  15,  the 60th ──► 90th esthetic numbers ──────────────────────────────────────
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd

────────────────────────────────────────── base  16,  the 64th ──► 96th esthetic numbers ──────────────────────────────────────────
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc

──────────────────────────────── base 10 esthetic numbers between 1000 and 9999 (inclusive) ────────────────────────────────
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 3234 3432 3434 3454 3456 4321 4323 4343 4345
4543 4545 4565 4567 5432 5434 5454 5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676 7678 7876
7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898

61  esthetic numbers listed.

Sidef[edit]

func generate_esthetic(root, upto, callback, b=10) {
 
var v = root.digits2num(b)
 
return nil if (v > upto)
callback(v)
 
var t = root.head
 
__FUNC__([t+1, root...], upto, callback, b) if (t+1 < b)
__FUNC__([t-1, root...], upto, callback, b) if (t-1 >= 0)
}
 
func between_esthetic(from, upto, b=10) {
gather {
for k in (1..^b) {
generate_esthetic([k], upto, { take(_) if (_ >= from) }, b)
}
}.sort
}
 
func first_n_esthetic(n, b=10) {
for (var m = n**2; true ; m *= b) {
var list = between_esthetic(1, m, b)
return list.first(n) if (list.len >= n)
}
}
 
for b in (2..16) {
say "\n#{b}-esthetic numbers with indices #{4*b}..#{6*b}: "
say first_n_esthetic(6*b, b).last(6*b - 4*b + 1).map{.base(b)}.join(' ')
}
 
say "\nBase 10 esthetic numbers between 1,000 and 9,999:"
between_esthetic(1e3, 1e4).slices(20).each { .join(' ').say }
 
say "\nBase 10 esthetic numbers between 100,000,000 and 130,000,000:"
between_esthetic(1e8, 13e7).slices(9).each { .join(' ').say }
Output:
2-esthetic numbers with indices 8..12: 
10101010 101010101 1010101010 10101010101 101010101010

3-esthetic numbers with indices 12..18: 
1210 1212 2101 2121 10101 10121 12101

4-esthetic numbers with indices 16..24: 
323 1010 1012 1210 1212 1232 2101 2121 2123

5-esthetic numbers with indices 20..30: 
323 343 432 434 1010 1012 1210 1212 1232 1234 2101

6-esthetic numbers with indices 24..36: 
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234

7-esthetic numbers with indices 28..42: 
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232

8-esthetic numbers with indices 32..48: 
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212

9-esthetic numbers with indices 36..54: 
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210

10-esthetic numbers with indices 40..60: 
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012

11-esthetic numbers with indices 44..66: 
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010

12-esthetic numbers with indices 48..72: 
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab

13-esthetic numbers with indices 52..78: 
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba

14-esthetic numbers with indices 56..84: 
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc

15-esthetic numbers with indices 60..90: 
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd

16-esthetic numbers with indices 64..96: 
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc

Base 10 esthetic numbers between 1,000 and 9,999:
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 3234 3432 3434 3454
3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 5456 5654 5656 5676 5678 6543 6545 6565
6567 6765 6767 6787 6789 7654 7656 7676 7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878
9898

Base 10 esthetic numbers between 100,000,000 and 130,000,000:
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789

Wren[edit]

Translation of: Go
Library: Wren-fmt
import "/fmt" for Conv, Fmt
 
var isEsthetic = Fn.new { |n, b|
if (n == 0) return false
var i = n % b
n = (n/b).floor
while (n > 0) {
var j = n % b
if ((i - j).abs != 1) return false
n = (n/b).floor
i = j
}
return true
}
 
var esths = []
 
var dfs // recursive function
dfs = Fn.new { |n, m, i|
if (i >= n && i <= m) esths.add(i)
if (i == 0 || i > m) return
var d = i % 10
var i1 = i*10 + d - 1
var i2 = i1 + 2
System.write("") // fixes a VM recursion bug
if (d == 0) {
dfs.call(n, m, i2)
} else if (d == 9) {
dfs.call(n, m, i1)
} else {
dfs.call(n, m, i1)
dfs.call(n, m, i2)
}
}
 
var listEsths = Fn.new { |n, n2, m, m2, perLine, all|
esths.clear()
for (i in 0..9) dfs.call(n2, m2, i)
var le = esths.count
System.print("Base 10: %(Fmt.dc(0, le)) esthetic numbers between %(Fmt.dc(0, n)) and %(Fmt.dc(0, m))")
if (all) {
var c = 0
for (esth in esths) {
System.write("%(esth) ")
if ((c+1)%perLine == 0) System.print()
c = c + 1
}
} else {
for (i in 0...perLine) System.write("%(Conv.dec(esths[i])) ")
System.print("\n............\n")
for (i in le-perLine...le) System.write("%(Conv.dec(esths[i])) ")
}
System.print("\n")
}
 
for (b in 2..16) {
System.print("Base %(b): %(4*b)th to %(6*b)th esthetic numbers:")
var n = 1
var c = 0
while (c < 6*b) {
if (isEsthetic.call(n, b)) {
c = c + 1
if (c >= 4*b) System.write("%(Conv.itoa(n, b)) ")
}
n = n + 1
}
System.print("\n")
}
 
// the following all use the obvious range limitations for the numbers in question
listEsths.call(1000, 1010, 9999, 9898, 16, true)
listEsths.call(1e8, 101010101, 13*1e7, 123456789, 9, true)
listEsths.call(1e11, 101010101010, 13*1e10, 123456789898, 7, false)
listEsths.call(1e14, 101010101010101, 13*1e13, 123456789898989, 5, false)
Output:
Base 2: 8th to 12th esthetic numbers:
10101010 101010101 1010101010 10101010101 101010101010 

Base 3: 12th to 18th esthetic numbers:
1210 1212 2101 2121 10101 10121 12101 

Base 4: 16th to 24th esthetic numbers:
323 1010 1012 1210 1212 1232 2101 2121 2123 

Base 5: 20th to 30th esthetic numbers:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101 

Base 6: 24th to 36th esthetic numbers:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234 

Base 7: 28th to 42th esthetic numbers:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232 

Base 8: 32th to 48th esthetic numbers:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212 

Base 9: 36th to 54th esthetic numbers:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210 

Base 10: 40th to 60th esthetic numbers:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012 

Base 11: 44th to 66th esthetic numbers:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010 

Base 12: 48th to 72th esthetic numbers:
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab 

Base 13: 52th to 78th esthetic numbers:
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba 

Base 14: 56th to 84th esthetic numbers:
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc 

Base 15: 60th to 90th esthetic numbers:
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd 

Base 16: 64th to 96th esthetic numbers:
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc 

Base 10: 61 esthetic numbers between 1,000 and 9,999
1010 1012 1210 1212 1232 1234 2101 2121 2123 2321 2323 2343 2345 3210 3212 3232 
3234 3432 3434 3454 3456 4321 4323 4343 4345 4543 4545 4565 4567 5432 5434 5454 
5456 5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676 
7678 7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898 

Base 10: 126 esthetic numbers between 100,000,000 and 130,000,000
101010101 101010121 101010123 101012101 101012121 101012123 101012321 101012323 101012343 
101012345 101210101 101210121 101210123 101212101 101212121 101212123 101212321 101212323 
101212343 101212345 101232101 101232121 101232123 101232321 101232323 101232343 101232345 
101234321 101234323 101234343 101234345 101234543 101234545 101234565 101234567 121010101 
121010121 121010123 121012101 121012121 121012123 121012321 121012323 121012343 121012345 
121210101 121210121 121210123 121212101 121212121 121212123 121212321 121212323 121212343 
121212345 121232101 121232121 121232123 121232321 121232323 121232343 121232345 121234321 
121234323 121234343 121234345 121234543 121234545 121234565 121234567 123210101 123210121 
123210123 123212101 123212121 123212123 123212321 123212323 123212343 123212345 123232101 
123232121 123232123 123232321 123232323 123232343 123232345 123234321 123234323 123234343 
123234345 123234543 123234545 123234565 123234567 123432101 123432121 123432123 123432321 
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545 
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565 
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789 


Base 10: 911 esthetic numbers between 100,000,000,000 and 130,000,000,000
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010 
............

123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898 

Base 10: 6,225 esthetic numbers between 100,000,000,000,000 and 130,000,000,000,000
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121 
............

123456789898767 123456789898787 123456789898789 123456789898987 123456789898989