Esthetic numbers: Difference between revisions

Added Easylang
(Added Algol 68)
(Added Easylang)
(42 intermediate revisions by 15 users not shown)
Line 37:
:;*[https://www.geeksforgeeks.org/stepping-numbers/ Geeks for Geeks - Stepping numbers]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F isEsthetic(=n, b)
I n == 0 {R 0B}
V i = n % b
n I/= b
L n > 0
V j = n % b
I abs(i - j) != 1
R 0B
n I/= b
i = j
R 1B
 
F listEsths(Int64 n1, n2, m1, m2; perLine, all)
[Int64] esths
 
F dfs(Int64 n, m, i) -> N
I i C n .. m
@esths.append(i)
I i == 0 | i > m {R}
V d = i % 10
V i1 = i * 10 + d - 1
V i2 = i1 + 2
I d == 0
@dfs(n, m, i2)
E I d == 9
@dfs(n, m, i1)
E
@dfs(n, m, i1)
@dfs(n, m, i2)
 
L(i) 10
dfs(n2, m2, i)
 
print(‘Base 10: ’esths.len‘ esthetic numbers between ’n1‘ and ’m1‘:’)
I all
L(esth) esths
print(esth, end' I (L.index + 1) % perLine == 0 {"\n"} E ‘ ’)
print()
E
L(i) 0 .< perLine
print(esths[i], end' ‘ ’)
print("\n............")
L(i) esths.len - perLine .< esths.len
print(esths[i], end' ‘ ’)
print()
print()
 
L(b) 2..16
print(‘Base ’b‘: ’(4 * b)‘th to ’(6 * b)‘th esthetic numbers:’)
V n = Int64(1)
V c = Int64(0)
L c < 6 * b
I isEsthetic(n, b)
c++
I c >= 4 * b
print(String(n, radix' b), end' ‘ ’)
n++
print("\n")
 
listEsths(1000, 1010, 9999, 9898, 16, 1B)
listEsths(100'000'000, 101'010'101, 130'000'000, 123'456'789, 9, 1B)
listEsths(100'000'000'000, 101'010'101'010, 130'000'000'000, 123'456'789'898, 7, 0B)
listEsths(100'000'000'000'000, 101'010'101'010'101, 130'000'000'000, 123'456'789'898'989, 5, 0B)
listEsths(100'000'000'000'000'000, 101'010'101'010'101'010, 130'000'000'000'000'000, 123'456'789'898'989'898, 4, 0B)</syntaxhighlight>
 
{{out}}
<pre>
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 130000000000:
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
 
</pre>
 
=={{header|ALGOL 68}}==
As with other solutions here, uses brute-force for the main task, generates the numbers for the stretch goal.
<langsyntaxhighlight lang="algol68">BEGIN # find some esthetic numbers: numbers whose successive digits differ by 1 #
# returns TRUE if n is esthetic in the specified base, FALSE otherwise #
PRIO ISESTHETIC = 1;
Line 134 ⟶ 289:
print( ( newline ) );
print( ( "Found ", whole( e count, 0 ), " esthetic numbers", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 191 ⟶ 346:
Found 126 esthetic numbers
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">esthetic?: function [n, b][
if n=0 -> return false
k: n % b
l: n / b
 
while [l>0][
j: l % b
if 1 <> abs k-j -> return false
l: l / b
k: j
]
return true
]
 
HEX: "0000000000ABCDEF"
 
getHex: function [ds][
map ds 'd [
(d < 10)? -> to :string d
-> to :string HEX\[d]
]
]
 
findEsthetics: function [base][
limDown: base * 4
limUp: base * 6
 
cnt: 0
i: 1
result: new []
while [cnt < limUp][
if esthetic? i base [
cnt: cnt + 1
if cnt >= limDown ->
'result ++ join getHex digits.base: base i
]
i: i + 1
]
print ["Base" base "->" (to :string limDown)++"th" "to" (to :string limUp)++"th" "esthetic numbers:"]
print result
print ""
]
 
loop 2..16 'bs ->
findEsthetics bs
 
print "Esthetic numbers between 1000 and 9999:"
 
loop split.every: 16 select 1000..9999 'num -> esthetic? num 10 'row [
print map to [:string] row 'item -> pad item 4
]</syntaxhighlight>
 
{{out}}
 
<pre>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
 
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</pre>
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <locale.h>
Line 311 ⟶ 573:
setlocale(LC_NUMERIC, oldLocale);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 320 ⟶ 582:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <functional>
#include <iostream>
#include <sstream>
Line 439 ⟶ 701:
listEsths((uint64_t)1e17, 101'010'101'010'101'010, 13 * (uint64_t)1e16, 123'456'789'898'989'898, 4, false);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 512 ⟶ 774:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.conv;
import std.stdio;
 
Line 610 ⟶ 872:
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);
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 680 ⟶ 942:
............
123456789898987898 123456789898989876 123456789898989878 123456789898989898 </pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
<syntaxhighlight lang="Delphi">
 
type TIntArray = array of integer;
 
function GetRadixString(L: Integer; Radix: Byte): string;
{Converts integer a string of any radix}
const HexChars: array[0..15] Of char =
('0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
var I: integer;
var S: string;
var Sign: string[1];
begin
Result:='';
If (L < 0) then
begin
Sign:='-';
L:=Abs(L);
end
else Sign:='';
S:='';
repeat
begin
I:=L mod Radix;
S:=HexChars[I] + S;
L:=L div Radix;
end
until L = 0;
Result:=Sign + S;
end;
 
 
 
procedure StrToInts(S: string; var IA: TIntArray);
{Convert numerical string of any radix and convert to numbers}
var I: integer;
begin
for I:=1 to Length(S) do
begin
SetLength(IA,Length(IA)+1);
if S[I]<#$40 then IA[High(IA)]:=Byte(S[I])-$30
else IA[High(IA)]:=(Byte(S[I])-$41)+10;
end;
end;
 
 
 
function IsEsthetic(N: integer; Radix: byte): boolean;
{Check number to see if neighboring digits are no more than one differents.}
var I: integer;
var S: string;
var IA: TIntArray;
begin
Result:=False;
S:=GetRadixString(N,Radix);
StrToInts(S,IA);
for I:=0 to Length(IA)-2 do
if Abs(IA[I+1]-IA[I])<>1 then exit;
Result:=True;
end;
 
 
 
function GetEstheticRange(Memo: TMemo; Range1,Range2,Count1,Count2,Base: integer): integer;
{Find an Esthetic number in the domain of Range and Counts specified}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0; Result:=0;
S:='';
for I:=Range1 to Range2 do
if IsEsthetic(I,Base) then
begin
Inc(Cnt);
if (Cnt>=Count1) and (Cnt<=Count2) then
begin
Inc(Result);
S:=S+' '+GetRadixString(I,Base);
if (Result mod 10)=0 then S:=S+#$0D#$0A;
end;
if Cnt>=Count2 then break;
end;
Memo.Lines.Add(S);
end;
 
 
procedure FindEstheticNumbers(Memo: TMemo);
{Find Esthetic numbers for Rosetta Code problem}
var Base,First,Last,Cnt: integer;
begin
for Base:=2 to 16 do
begin
First:=Base*4; Last:=Base*6;
Memo.Lines.Add(Format('Base %2d: %2dth to %2dth esthetic numbers:',[Base,First,Last]));
Cnt:=GetEstheticRange(Memo,0,High(Integer),First,Last,Base);
Memo.Lines.Add('Count: '+IntToStr(Cnt));
Memo.Lines.Add('');
end;
 
Memo.Lines.Add('Base 10: esthetic numbers between 1000,9999');
Cnt:=GetEstheticRange(Memo,1000,9999,0,High(Integer),10);
Memo.Lines.Add('Count: '+IntToStr(Cnt));
Memo.Lines.Add('');
 
Memo.Lines.Add('Base 10: esthetic numbers between 100000000,130000000');
Cnt:=GetEstheticRange(Memo,100000000,130000000,0,High(Integer),10);
Memo.Lines.Add('Count: '+IntToStr(Cnt));
Memo.Lines.Add('');
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Base 2: 8th to 12th esthetic numbers:
1010101 10101010 101010101 1010101010 10101010101
Count: 5
 
Base 3: 12th to 18th esthetic numbers:
1012 1210 1212 2101 2121 10101 10121
Count: 7
 
Base 4: 16th to 24th esthetic numbers:
321 323 1010 1012 1210 1212 1232 2101 2121
Count: 9
 
Base 5: 20th to 30th esthetic numbers:
321 323 343 432 434 1010 1012 1210 1212 1232
1234
Count: 11
 
Base 6: 24th to 36th esthetic numbers:
323 343 345 432 434 454 543 545 1010 1012
1210 1212 1232
Count: 13
 
Base 7: 28th to 42th esthetic numbers:
343 345 432 434 454 456 543 545 565 654
656 1010 1012 1210 1212
Count: 15
 
Base 8: 32th to 48th esthetic numbers:
345 432 434 454 456 543 545 565 567 654
656 676 765 767 1010 1012 1210
Count: 17
 
Base 9: 36th to 54th esthetic numbers:
432 434 454 456 543 545 565 567 654 656
676 678 765 767 787 876 878 1010 1012
Count: 19
 
Base 10: 40th to 60th esthetic numbers:
434 454 456 543 545 565 567 654 656 676
678 765 767 787 789 876 878 898 987 989
1010
Count: 21
 
Base 11: 44th to 66th esthetic numbers:
454 456 543 545 565 567 654 656 676 678
765 767 787 789 876 878 898 89A 987 989
9A9 A98 A9A
Count: 23
 
Base 12: 48th to 72th esthetic numbers:
456 543 545 565 567 654 656 676 678 765
767 787 789 876 878 898 89A 987 989 9A9
9AB A98 A9A ABA BA9
Count: 25
 
Base 13: 52th to 78th 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 ABC BA9 BAB BCB
Count: 27
 
Base 14: 56th to 84th 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 BCD CBA CBC
Count: 29
 
Base 15: 60th to 90th 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 CDE
DCB
Count: 31
 
Base 16: 64th to 96th 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 DED DEF
Count: 33
 
Base 10: esthetic numbers between 1000,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
Count: 61
 
Base 10: esthetic numbers between 100000000,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
Count: 126
</pre>
 
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
func$ to n b .
digs$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if n = 0
return 0
.
s$ = ""
while n > 0
idx = n mod b + 1
n = n div b
s$ = substr digs$ idx 1 & s$
.
return s$
.
func uabs a b .
if a > b
return a - b
.
return b - a
.
func isEsthetic n b .
if n = 0
return 0
.
i = n mod b
n = n div b
while n > 0
j = n mod b
if uabs i j <> 1
return 0
.
n = n div b
i = j
.
return 1
.
for b = 2 to 16
print "Base " & b & ": " & 4 * b & "th to " & 6 * b & "th esthetic numbers:"
n = 1
c = 0
while c < 6 * b
if isEsthetic n b = 1
c += 1
if c >= 4 * b
write to n b & " "
.
.
n += 1
.
print ""
print ""
.
print "Base 10 esthetic numbers between 1000 and 9999:"
for i = 1000 to 9999
if isEsthetic i 10 = 1
write i & " "
.
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
===The functions===
<langsyntaxhighlight lang="fsharp">
// Generate Esthetic Numbers. Nigel Galloway: March 21st., 2020
let rec fN Σ n g = match g with h::t -> match List.head h with
Line 696 ⟶ 1,247:
let EtoS n = let g = "0123456789abcdef".ToCharArray()
n |> List.map(fun n->g.[n]) |> List.rev |> Array.ofList |> System.String
</syntaxhighlight>
</lang>
===The Tasks===
; Esthetic numbers in bases 2 through 16
<langsyntaxhighlight lang="fsharp">
[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"))
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex">
Line 1,022 ⟶ 1,573:
; Base 10 esthetic numbers with a magnitude between 1000 and 9999
 
<langsyntaxhighlight lang="fsharp">
Esthetic 10|>Seq.map(EtoS>>int)|>Seq.skipWhile(fun n->n<1000)|>Seq.takeWhile(fun n->n<9999)|>Seq.iter(printfn "%d");;
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex">
Line 1,092 ⟶ 1,643:
; Base 10 esthetic numbers with a magnitude between 1.0e8 and 1.3e8
 
<langsyntaxhighlight lang="fsharp">
Esthetic 10|>Seq.map(EtoS>>int)|>Seq.skipWhile(fun n->n<100000000)|>Seq.takeWhile(fun n->n< 130000000)|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex">
Line 1,227 ⟶ 1,778:
; Base 10 esthetic numbers with a magnitude between 1.0e11 and 1.3e11
 
<langsyntaxhighlight lang="fsharp">
Esthetic 10|>Seq.map(EtoS>>int64)|>Seq.skipWhile(fun n->n<100000000000L)|>Seq.takeWhile(fun n->n<130000000000L)|>Seq.iter(printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex">
Line 2,149 ⟶ 2,700:
The <code>bfs</code> 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|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: combinators deques dlists formatting grouping io kernel
locals make math math.order math.parser math.ranges
math.text.english prettyprint sequences sorting strings ;
Line 2,202 ⟶ 2,753:
 
"Count of base 10 esthetic numbers between zero and one quadrillion:"
print 0 1e15 10 esthetics length .</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">
Line 2,282 ⟶ 2,833:
Count of base 10 esthetic numbers between zero and one quadrillion:
161914
</pre>
 
=={{header|Forth}}==
{{works with|Gforth}}
<syntaxhighlight lang="forth">\ Returns the next esthetic number in the given base after n, where n is an
\ esthetic number in that base or one less than a power of base.
: next_esthetic_number { n base -- n }
n 1+ base < if n 1+ exit then
n base / dup base mod
dup n base mod 1+ = if dup 1+ base < if 2drop n 2 + exit then then
drop base recurse
dup base mod
dup 0= if 1+ else 1- then
swap base * + ;
 
: print_esthetic_numbers { min max per_line -- }
." Esthetic numbers in base 10 between " min 1 .r ." and " max 1 .r ." :" cr
0
min 1- 10 next_esthetic_number
begin
dup max <=
while
dup 4 .r
swap 1+ dup per_line mod 0= if cr else space then swap
10 next_esthetic_number
repeat
drop
cr ." count: " . cr ;
 
: main
17 2 do
i 4 * i 6 * { min max }
." Esthetic numbers in base " i 1 .r ." from index " min 1 .r ." through index " max 1 .r ." :" cr
0
max 1+ 1 do
j next_esthetic_number
i min >= if dup ['] . j base-execute then
loop
drop
cr cr
loop
1000 9999 16 print_esthetic_numbers cr
100000000 130000000 8 print_esthetic_numbers ;
 
main
bye</syntaxhighlight>
 
{{out}}
<pre>
Esthetic numbers in base 2 from index 8 through index 12:
10101010 101010101 1010101010 10101010101 101010101010
 
Esthetic numbers in base 3 from index 12 through index 18:
1210 1212 2101 2121 10101 10121 12101
 
Esthetic numbers in base 4 from index 16 through index 24:
323 1010 1012 1210 1212 1232 2101 2121 2123
 
Esthetic numbers in base 5 from index 20 through index 30:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
 
Esthetic numbers in base 6 from index 24 through index 36:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
 
Esthetic numbers in base 7 from index 28 through index 42:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
 
Esthetic numbers in base 8 from index 32 through index 48:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
 
Esthetic numbers in base 9 from index 36 through index 54:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
 
Esthetic numbers in base 10 from index 40 through index 60:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
 
Esthetic numbers in base 11 from index 44 through index 66:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89A 987 989 9A9 A98 A9A 1010
 
Esthetic numbers in base 12 from index 48 through index 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
 
Esthetic numbers in base 13 from index 52 through index 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
 
Esthetic numbers in base 14 from index 56 through index 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
 
Esthetic numbers in base 15 from index 60 through index 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
 
Esthetic numbers in base 16 from index 64 through index 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
 
Esthetic numbers in base 10 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
count: 61
 
Esthetic numbers in base 10 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
count: 126
</pre>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
dim shared as string*16 digits = "0123456789ABCDEF"
 
Line 2,347 ⟶ 3,017:
make_base i, 10, number
if is_esthetic(number) then print number;" ";
next i</langsyntaxhighlight>
{{out}}
<pre>
Line 2,390 ⟶ 3,060:
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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,499 ⟶ 3,169:
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)
}</langsyntaxhighlight>
 
{{out}}
Line 2,589 ⟶ 3,259:
123456789898987898 123456789898989876 123456789898989878 123456789898989898
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.List (unfoldr, genericIndex)
import Control.Monad (replicateM, foldM, mzero)
 
-- a predicate for esthetic numbers
isEsthetic b = all ((== 1) . abs) . differences . toBase b
where
differences lst = zipWith (-) lst (tail lst)
 
-- Monadic solution, inefficient for small bases.
esthetics_m b =
do differences <- (\n -> replicateM n [-1, 1]) <$> [0..]
firstDigit <- [1..b-1]
differences >>= fromBase b <$> scanl (+) firstDigit
 
-- Much more efficient iterative solution (translation from Python).
-- Uses simple list as an ersatz queue.
esthetics b = tail $ fst <$> iterate step (undefined, q)
where
q = [(d, d) | d <- [1..b-1]]
step (_, queue) =
let (num, lsd) = head queue
new_lsds = [d | d <- [lsd-1, lsd+1], d < b, d >= 0]
in (num, tail queue ++ [(num*b + d, d) | d <- new_lsds])
 
-- representation of numbers as digits
fromBase b = foldM f 0
where f r d | d < 0 || d >= b = mzero
| otherwise = pure (r*b + d)
 
toBase b = reverse . unfoldr f
where
f 0 = Nothing
f n = let (q, r) = divMod n b in Just (r, q)
 
showInBase b = foldMap (pure . digit) . toBase b
where digit = genericIndex (['0'..'9'] <> ['a'..'z'])
</syntaxhighlight>
 
'''Tasks implementation'''
 
<pre>λ> isEsthetic 10 1234567654321
True
 
λ> isEsthetic 10 1234560654321
False
 
λ> isEsthetic 2 <$> fromBase 2 [1,0,1,0,1,0]
True
 
λ> isEsthetic 2 <$> fromBase 2 [1,0,1,0,1,1]
False
 
λ> take 50 $ esthetics 10
[1,2,3,4,5,6,7,8,9,10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98,101,121,123,210,212,232,234,321,323,343,345,432,434,
454,456,543,545,565,567,654,656,676,678,765]
 
λ> take 50 $ showInBase 3 <$> esthetics 3
["1","2","10","12","21","101","121","210","212","1010","1012","1210","1212","2101","2121","10101","10121","12101","12121",
"21010","21012","21210","21212","101010","101012","101210","101212","121010","121012","121210","121212","210101","210121",
"212101","212121","1010101","1010121","1012101","1012121","1210101","1210121","1212101","1212121","2101010","2101012",
"2101210","2101212","2121010","2121012","2121210"]
 
λ> :{
*Main| examples b = do {
*Main| putStrLn $ "Task for base " <> show b;
*Main| putStrLn $ unwords $ showInBase b <$> (drop (b*4-1) . take (b*6) $ esthetics b)}
*Main| :}
 
λ> mapM_ examples [2..16]
Task for base 2
10101010 101010101 1010101010 10101010101 101010101010
Task for base 3
1210 1212 2101 2121 10101 10121 12101
Task for base 4
323 1010 1012 1210 1212 1232 2101 2121 2123
Task for base 5
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
Task for base 6
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
Task for base 7
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
Task for base 8
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
Task for base 9
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
Task for base 10
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
Task for 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
Task for 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
Task for 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
Task for 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
Task for 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
Task for 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
 
λ> let takeWithin a b = dropWhile (< a) . takeWhile (<= b)
 
λ> takeWithin 1000 9999 $ esthetics 10
[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]
 
λ> takeWithin 100000000 130000000 $ esthetics 10
[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]
 
λ> length it
126
 
λ> length $ takeWithin 100000000000000000 130000000000000000 $ esthetics 10
44744</pre>
 
=={{header|J}}==
 
Implementation (brute force):
 
<syntaxhighlight lang="j">isesthetic=: 10&$: :(1 */ .=2 |@-/\ #.inv)"0
 
gen=: {{r=.$k=.1 while.y>#r do. r=.r,k#~u k
k=.1+({:k)+i.2*#k end.y{.r}}</syntaxhighlight>
 
Task examples:
 
<syntaxhighlight lang="j">tobase=: (a.{~;48 97(+ i.)each 10 26) {~ #.inv
taskB=: {{;:inv y tobase&.> (<:4*y)}. y&isesthetic gen 6*y}}
 
taskB 2
10101010 101010101 1010101010 10101010101 101010101010
taskB 3
1210 1212 2101 2121 10101 10121 12101
taskB 4
323 1010 1012 1210 1212 1232 2101 2121 2123
taskB 5
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
taskB 6
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
taskB 7
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
taskB 8
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
taskB 9
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
taskB 10
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
taskB 11
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010
taskB 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
taskB 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
taskB 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
taskB 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
taskB 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
 
(#~ isesthetic) 1000+i.1000
1010 1012 1210 1212 1232 1234</syntaxhighlight>
 
Stretch goal (the slow way):
 
<syntaxhighlight lang="j"> $e=: x: (#~ isesthetic) 1e8+i.1+3e7
126
q:126
2 3 3 7</syntaxhighlight>
 
(Result e not displayed here -- it's the same as <tt>next^:8]1</tt> below.)
 
It's much more efficient to generate sequences of digits from a base digit, rather than generating sequential integers and discarding those which are not suitable.
 
For example, we can generate a directed graph, from one digit to the one or two viable adjacent digits, and then build up a result based on all viable values of the rightmost digit of the current list of partially built candidates:
 
<syntaxhighlight lang="j">graph=: </./|:0 1,10 10#:(#~ isesthetic)10+i.90
next=: [:; (0 10#.],.graph {::~10|])each
 
next^:3]1
1010 1012 1210 1212 1232 1234
$next^:8]1
126
14 9$next^:8]1
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</syntaxhighlight>
 
(We organize the 126 values of the stretch goal into 14 rows of 9 columns (126=14*9) using the expression <tt>14 9 $</tt> ...)
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.ArrayList;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Line 2,697 ⟶ 3,580:
listEsths((long) 1e17, 101_010_101_010_101_010L, 13 * (long) 1e16, 123_456_789_898_989_898L, 4, false);
}
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 2,769 ⟶ 3,652:
 
=={{header|JavaScript}}==
===JavaScript :: Procedural===
<lang javaScript>
<syntaxhighlight lang="javascript">function isEsthetic(inp, base = 10) {
let arr = inp.toString(base).split('');
if (arr.length == 1) return false;
Line 2,808 ⟶ 3,691:
 
console.log( collectEsthetics(10, [1e8, 1.3e8]),
(new Date() - d) / 1000 + ' s' );</syntaxhighlight>
{{out}}<pre>2:
</lang>
{{out}}<pre>
2:
> Array(4) [ "1010101010", "10101010101", "101010101010", "1010101010101" ]
0.009 s
Line 2,919 ⟶ 3,800:
0.47 s
> Array(126) [ 101010101, 101010121, 101010123, 101012101, 101012121, 101012123, 101012321, 101012323, 101012343, 101012345, … ]
14.204 s</pre>
 
 
===JavaScript :: Functional===
Constrained generation – more efficient than a filter over a larger space.
{{Trans|Haskell}}
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
// -------- ESTHETIC NUMBERS IN A GIVEN BASE ---------
 
// estheticNumbersInBase :: Int -> [Int]
const estheticNumbersInBase = b =>
// An infinite sequence of numbers which
// are esthetic in the given base.
 
tail(fmapGen(x => x[0])(
iterate(([, queue]) => {
const [num, lsd] = queue[0];
const
newDigits = [lsd - 1, lsd + 1]
.flatMap(
d => (d < b && d >= 0) ? (
[d]
) : []
);
 
return Tuple(num)(
queue.slice(1).concat(
newDigits.flatMap(d => [
Tuple((num * b) + d)(d)
])
)
);
})(
Tuple()(
enumFromTo(1)(b - 1).flatMap(
d => [Tuple(d)(d)]
)
)
)
));
 
// ---------------------- TESTS ----------------------
const main = () => {
 
const samples = b => {
const
i = b * 4,
j = b * 6;
 
return unlines([
`Esthetics [${i}..${j}] for base ${b}:`,
...chunksOf(10)(
compose(drop(i - 1), take(j))(
estheticNumbersInBase(b)
).map(n => n.toString(b))
)
.map(unwords)
]);
};
 
const takeInRange = ([a, b]) =>
compose(
dropWhile(x => x < a),
takeWhileGen(x => x <= b)
);
 
return [
enumFromTo(2)(16)
.map(samples)
.join("\n\n"),
[
Tuple(1000)(9999),
Tuple(100000000)(130000000)
]
.map(
([lo, hi]) => unlines([
`Base 10 Esthetics in range [${lo}..${hi}]:`,
unlines(
chunksOf(6)(
takeInRange([lo, hi])(
estheticNumbersInBase(10)
)
)
.map(unwords)
)
])
).join("\n\n")
].join("\n\n");
};
 
// --------------------- GENERIC ---------------------
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: "Tuple",
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
yield this[k];
}
}
}
});
 
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n => {
// xs split into sublists of length n.
// The last sublist will be short if n
// does not evenly divide the length of xs .
const go = xs => {
const chunk = xs.slice(0, n);
 
return 0 < chunk.length ? (
[chunk].concat(
go(xs.slice(n))
)
) : [];
};
 
return go;
};
 
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
 
 
// drop :: Int -> [a] -> [a]
// drop :: Int -> Generator [a] -> Generator [a]
// drop :: Int -> String -> String
const drop = n =>
xs => Infinity > length(xs) ? (
xs.slice(n)
) : (take(n)(xs), xs);
 
 
// dropWhile :: (a -> Bool) -> [a] -> [a]
// dropWhile :: (Char -> Bool) -> String -> String
const dropWhile = p =>
// The suffix remaining after takeWhile p xs.
xs => {
const n = xs.length;
 
return xs.slice(
0 < n ? until(
i => n === i || !p(xs[i])
)(i => 1 + i)(0) : 0
);
};
 
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m =>
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
 
 
// fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
const fmapGen = f =>
// The map of f over a stream of generator values.
function* (gen) {
let v = gen.next();
 
while (!v.done) {
yield f(v.value);
v = gen.next();
}
};
 
 
// iterate :: (a -> a) -> a -> Gen [a]
const iterate = f =>
// An infinite list of repeated
// applications of f to x.
function* (x) {
let v = x;
 
while (true) {
yield v;
v = f(v);
}
};
 
 
// length :: [a] -> Int
const length = xs =>
// Returns Infinity over objects without finite
// length. This enables zip and zipWith to choose
// the shorter argument when one is non-finite,
// like cycle, repeat etc
"GeneratorFunction" !== xs.constructor
.constructor.name ? (
xs.length
) : Infinity;
 
 
// tail :: [a] -> [a]
const tail = xs =>
// A new list consisting of all
// items of xs except the first.
"GeneratorFunction" !== xs.constructor
.constructor.name ? (
(ys => 0 < ys.length ? ys.slice(1) : [])(
xs
)
) : (take(1)(xs), xs);
 
 
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = n =>
// The first n elements of a list,
// string of characters, or stream.
xs => "GeneratorFunction" !== xs
.constructor.constructor.name ? (
xs.slice(0, n)
) : Array.from({
length: n
}, () => {
const x = xs.next();
 
return x.done ? [] : [x.value];
}).flat();
 
 
// takeWhileGen :: (a -> Bool) -> Gen [a] -> [a]
const takeWhileGen = p => xs => {
const ys = [];
let
nxt = xs.next(),
v = nxt.value;
 
while (!nxt.done && p(v)) {
ys.push(v);
nxt = xs.next();
v = nxt.value;
}
 
return ys;
};
 
 
// unlines :: [String] -> String
const unlines = xs =>
// A single string formed by the intercalation
// of a list of strings with the newline character.
xs.join("\n");
 
 
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = p =>
// The value resulting from repeated applications
// of f to the seed value x, terminating when
// that result returns true for the predicate p.
f => x => {
let v = x;
 
while (!p(v)) {
v = f(v);
}
 
return v;
};
 
 
// unwords :: [String] -> String
const unwords = xs =>
// A space-separated string derived
// from a list of words.
xs.join(" ");
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>Esthetics [8..12] for base 2:
10101010 101010101 1010101010 10101010101 101010101010
 
Esthetics [12..18] for base 3:
1210 1212 2101 2121 10101 10121 12101
 
Esthetics [16..24] for base 4:
323 1010 1012 1210 1212 1232 2101 2121 2123
 
Esthetics [20..30] for base 5:
323 343 432 434 1010 1012 1210 1212 1232 1234
2101
 
Esthetics [24..36] for base 6:
343 345 432 434 454 543 545 1010 1012 1210
1212 1232 1234
 
Esthetics [28..42] for base 7:
345 432 434 454 456 543 545 565 654 656
1010 1012 1210 1212 1232
 
Esthetics [32..48] for base 8:
432 434 454 456 543 545 565 567 654 656
676 765 767 1010 1012 1210 1212
 
Esthetics [36..54] for base 9:
434 454 456 543 545 565 567 654 656 676
678 765 767 787 876 878 1010 1012 1210
 
Esthetics [40..60] for base 10:
454 456 543 545 565 567 654 656 676 678
765 767 787 789 876 878 898 987 989 1010
1012
 
Esthetics [44..66] for 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
 
Esthetics [48..72] for 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
 
Esthetics [52..78] for 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
 
Esthetics [56..84] for 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
 
Esthetics [60..90] for 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
 
Esthetics [64..96] for 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 Esthetics in range [1000..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 Esthetics in range [100000000..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</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
<syntaxhighlight lang="jq">
### Preliminaries
# _nwise/1 is included here for the sake of gojq:
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def tobase($b):
def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1];
def mod: . % $b;
def div: ((. - mod) / $b);
def digits: recurse( select(. > 0) | div) | mod ;
# For jq it would be wise to protect against `infinite` as input, but using `isinfinite` confuses gojq
select( (tostring|test("^[0-9]+$")) and 2 <= $b and $b <= 36)
| if . == 0 then "0"
else [digits | digit] | reverse[1:] | add
end;
 
### Esthetic Numbers
def isEsthetic($b):
if . == 0 then false
else {i: (. % $b), n: ((./$b)|floor) }
| until (.n <= 0;
(.n % $b) as $j
| if (.i - $j)|length != 1 # abs
then .n = -1 #flag
else .n |= ((./$b)|floor)
| .i = $j
end)
| .n != -1
end;
 
# depth-first search
# input: {esths}
def dfs($n; $m; $i):
if ($i >= $n and $i <= $m) then .esths += [$i] else . end
| if ($i == 0 or $i > $m) then .
else ($i % 10) as $d
| ($i*10 + $d - 1) as $i1
| ($i1 + 2) as $i2
| if $d == 0
then dfs($n; $m; $i2)
elif $d == 9
then dfs($n; $m; $i1)
else dfs($n; $m; $i1) | dfs($n; $m; $i2)
end
end;
 
### The tasks
def listEsths(n; n2; m; m2; $perLine; $all):
( {esths: []}
| reduce range(0;10) as $i (.; dfs(n2; m2; $i) )
| "Base 10: \(.esths|length) esthetic numbers between \(n) and \(m)",
if $all
then .esths | _nwise($perLine) | join(" ")
else
(.esths[:$perLine] | join(" ")),
"............",
(.esths[-$perLine:] | join(" "))
end ),
"";
 
def task($maxBase):
range (2; 1+$maxBase) as $b
| "Base \($b): \(4*$b)th to \(6*$b)th esthetic numbers:",
( [{ n: 1, c: 0 }
| while (.c <= 6*$b;
.emit = null
| if (.n|isEsthetic($b))
then .c += 1
| if .c >= 4*$b
then .emit = "\(.n | tobase($b))"
else .
end
else .
end
| .n += 1 )
| select(.emit).emit]
| _nwise(10) | join(" ") ),
"" ;
 
task(16),
 
# the following all use the obvious range limitations for the numbers in question
listEsths(1000; 1010; 9999; 9898; 16; true),
listEsths(1e8; 101010101; 13*1e7; 123456789; 9; true),
listEsths(1e11; 101010101010; 13*1e10; 123456789898; 7; false),
listEsths(1e14; 101010101010101; 13*1e13; 123456789898989; 5; false)
</syntaxhighlight>
{{output}}
(scrollable)
<pre style="height:20lh;overflow:auto">
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
</pre>
 
=={{header|Julia}}==
Illustrates both brute force and iterative methods of generating numbers in the sequence.
<langsyntaxhighlight lang="julia">using Formatting
import Base.iterate, Base.IteratorSize, Base.IteratorEltype
 
Line 3,031 ⟶ 4,495:
println("\nTotal esthetic numbers in interval: $count")
end
</langsyntaxhighlight>{{out}}
<pre>
For base 2, the esthetic numbers indexed from 8 to 12 are:
Line 3,163 ⟶ 4,627:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import kotlin.math.abs
 
fun isEsthetic(n: Long, b: Long): Boolean {
Line 3,260 ⟶ 4,724:
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);
}</langsyntaxhighlight>
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 3,333 ⟶ 4,797:
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">function to(n, b)
local BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
Line 3,443 ⟶ 4,907:
listEsths(1e11, 101010101010, 13 * 1e10, 123456789898, 7, false)
listEsths(1e14, 101010101010101, 13 * 1e13, 123456789898989, 5, false)
listEsths(1e17, 101010101010101010, 13 * 1e16, 123456789898989898, 4, false)</langsyntaxhighlight>
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 3,515 ⟶ 4,979:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[EstheticNumbersRangeHelper, EstheticNumbersRange]
EstheticNumbersRangeHelper[power_, mima : {mi_, max_}, b_ : 10] := Module[{steps, cands},
steps = Tuples[{-1, 1}, power - 1];
Line 3,535 ⟶ 4,999:
Table[{b, EstheticNumbersRange[{1, If[b == 2, 100000, If[b == 3, 100000, b^4]]}, b][[4 b ;; 6 b]]}, {b, 2, 16}] // Grid
EstheticNumbersRange[{1000, 9999}]
EstheticNumbersRange[{10^8, 1.3 10^8}]</langsyntaxhighlight>
{{out}}
<pre>2 {10101010,101010101,1010101010,10101010101,101010101010}
Line 3,559 ⟶ 5,023:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
func isEsthetic(n, b: int64): bool =
Line 3,640 ⟶ 5,104:
listEsths(100_000_000_000, 101_010_101_010, 130_000_000_000, 123_456_789_898, 7, false)
listEsths(100_000_000_000_000, 101_010_101_010_101, 130_000_000_000, 123_456_789_898_989, 5, false)
listEsths(100_000_000_000_000_000, 101_010_101_010_101_010, 130_000_000_000_000_000, 123_456_789_898_989_898, 4, false)</langsyntaxhighlight>
 
{{out}}
Line 3,728 ⟶ 5,192:
=={{header|Pascal}}==
Simple brute force, but fast and simple counting for complete first digit ranges.
<langsyntaxhighlight lang="pascal">program Esthetic;
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$codealign proc=16}
Line 3,928 ⟶ 5,392:
writeln(Numb2USA(IntToStr(Dgtcnt[64][0]-Dgtcnt[63][0])):28);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex">
Line 4,026 ⟶ 5,490:
{{libheader|ntheory}}
{{trans|Sidef}}
<langsyntaxhighlight lang="perl">use 5.020;
use warnings;
use experimental qw(signatures);
Line 4,075 ⟶ 5,539:
for (my @list = between_esthetic(1e8, 1.3e8) ; @list ;) {
say join(' ', splice(@list, 0, 9));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,148 ⟶ 5,612:
=={{header|Phix}}==
Simple string based approach, very fast, manages stretch goal and much further in the blink of an eye.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">aleph</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0123456789ABCDEF"</span>
Line 4,218 ⟶ 5,682:
<span style="color: #0000FF;">{</span><span style="color: #000000;">comma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">),</span><span style="color: #000000;">comma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,251 ⟶ 5,715:
In other words we can count the total number of 62-digit esthetic numbers with at most 574 additions from a
table of at most 610 entries, in practice obviously just do all 610.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">count_esthetic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- a start digit of 0 means sum all 1..9</span>
Line 4,271 ⟶ 5,735:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,26d esthetic numbers with max 65 digits\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">64</span><span style="color: #0000FF;">),</span><span style="color: #000000;">count_esthetic</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
<small>(It turns out that [1..9] is the same as [2..10] anyway, by symmetry, but technically the latter would be the more correct expression for digits 1..9)</small>
{{out}}
Line 4,300 ⟶ 5,764:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de esthetic (N Base)
(let Lst
(make
Line 4,346 ⟶ 5,810:
(while (cut 9 'L)
(mapc '((L) (prin L " ")) @)
(prinl) ) )</langsyntaxhighlight>
 
{{out}}
Line 4,387 ⟶ 5,851:
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">main:-
forall(between(2, 16, Base),
(Min_index is Base * 4, Max_index is Base * 6,
print_esthetic_numbers1(Base, Min_index, Max_index))),
print_esthetic_numbers2(1000, 9999, 16),
nl,
print_esthetic_numbers2(100000000, 130000000, 8).
 
print_esthetic_numbers1(Base, Min_index, Max_index):-
swritef(Format, '~%tr ', [Base]),
writef('Esthetic numbers in base %t from index %t through index %t:\n',
[Base, Min_index, Max_index]),
print_esthetic_numbers1(Base, Format, Min_index, Max_index, 0, 1).
 
print_esthetic_numbers1(Base, Format, Min_index, Max_index, M, I):-
I =< Max_index,
!,
next_esthetic_number(Base, M, N),
(I >= Min_index -> format(Format, [N]) ; true),
J is I + 1,
print_esthetic_numbers1(Base, Format, Min_index, Max_index, N, J).
print_esthetic_numbers1(_, _, _, _, _, _):-
write('\n\n').
 
print_esthetic_numbers2(Min, Max, Per_line):-
writef('Esthetic numbers in base 10 between %t and %t:\n', [Min, Max]),
M is Min - 1,
print_esthetic_numbers2(Max, Per_line, M, 0).
 
print_esthetic_numbers2(Max, Per_line, M, Count):-
next_esthetic_number(10, M, N),
N =< Max,
!,
write(N),
Count1 is Count + 1,
(0 is Count1 mod Per_line -> nl ; write(' ')),
print_esthetic_numbers2(Max, Per_line, N, Count1).
print_esthetic_numbers2(_, _, _, Count):-
writef('\nCount: %t\n', [Count]).
 
next_esthetic_number(Base, M, N):-
N is M + 1,
N < Base,
!.
next_esthetic_number(Base, M, N):-
A is M // Base,
B is A mod Base,
(B is M mod Base + 1, B + 1 < Base ->
N is M + 2
;
next_esthetic_number(Base, A, C),
D is C mod Base,
(D == 0 -> E = 1 ; E is D - 1),
N is C * Base + E).</syntaxhighlight>
 
{{out}}
<pre>
Esthetic numbers in base 2 from index 8 through index 12:
10101010 101010101 1010101010 10101010101 101010101010
 
Esthetic numbers in base 3 from index 12 through index 18:
1210 1212 2101 2121 10101 10121 12101
 
Esthetic numbers in base 4 from index 16 through index 24:
323 1010 1012 1210 1212 1232 2101 2121 2123
 
Esthetic numbers in base 5 from index 20 through index 30:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
 
Esthetic numbers in base 6 from index 24 through index 36:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
 
Esthetic numbers in base 7 from index 28 through index 42:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
 
Esthetic numbers in base 8 from index 32 through index 48:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
 
Esthetic numbers in base 9 from index 36 through index 54:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
 
Esthetic numbers in base 10 from index 40 through index 60:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
 
Esthetic numbers in base 11 from index 44 through index 66:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010
 
Esthetic numbers in base 12 from index 48 through index 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
 
Esthetic numbers in base 13 from index 52 through index 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
 
Esthetic numbers in base 14 from index 56 through index 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
 
Esthetic numbers in base 15 from index 60 through index 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
 
Esthetic numbers in base 16 from index 64 through index 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
 
Esthetic numbers in base 10 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
Count: 61
 
Esthetic numbers in base 10 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
Count: 126
</pre>
 
=={{header|Python}}==
===Python :: Procedural===
{{works with|Python|3.9.5}}
<syntaxhighlight lang="python">from collections import deque
from itertools import dropwhile, islice, takewhile
from textwrap import wrap
from typing import Iterable, Iterator
 
 
Digits = str # Alias for the return type of to_digits()
 
 
def esthetic_nums(base: int) -> Iterator[int]:
"""Generate the esthetic number sequence for a given base
 
>>> list(islice(esthetic_nums(base=10), 20))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65]
"""
queue: deque[tuple[int, int]] = deque()
queue.extendleft((d, d) for d in range(1, base))
while True:
num, lsd = queue.pop()
yield num
new_lsds = (d for d in (lsd - 1, lsd + 1) if 0 <= d < base)
num *= base # Shift num left one digit
queue.extendleft((num + d, d) for d in new_lsds)
 
 
def to_digits(num: int, base: int) -> Digits:
"""Return a representation of an integer as digits in a given base
 
>>> to_digits(0x3def84f0ce, base=16)
'3def84f0ce'
"""
digits: list[str] = []
while num:
num, d = divmod(num, base)
digits.append("0123456789abcdef"[d])
return "".join(reversed(digits)) if digits else "0"
 
 
def pprint_it(it: Iterable[str], indent: int = 4, width: int = 80) -> None:
"""Pretty print an iterable which returns strings
 
>>> pprint_it(map(str, range(20)), indent=0, width=40)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19
<BLANKLINE>
"""
joined = ", ".join(it)
lines = wrap(joined, width=width - indent)
for line in lines:
print(f"{indent*' '}{line}")
print()
 
 
def task_2() -> None:
nums: Iterator[int]
for base in range(2, 16 + 1):
start, stop = 4 * base, 6 * base
nums = esthetic_nums(base)
nums = islice(nums, start - 1, stop) # start and stop are 1-based indices
print(
f"Base-{base} esthetic numbers from "
f"index {start} through index {stop} inclusive:\n"
)
pprint_it(to_digits(num, base) for num in nums)
 
 
def task_3(lower: int, upper: int, base: int = 10) -> None:
nums: Iterator[int] = esthetic_nums(base)
nums = dropwhile(lambda num: num < lower, nums)
nums = takewhile(lambda num: num <= upper, nums)
print(
f"Base-{base} esthetic numbers with "
f"magnitude between {lower:,} and {upper:,}:\n"
)
pprint_it(to_digits(num, base) for num in nums)
 
 
if __name__ == "__main__":
print("======\nTask 2\n======\n")
task_2()
 
print("======\nTask 3\n======\n")
task_3(1_000, 9_999)
 
print("======\nTask 4\n======\n")
task_3(100_000_000, 130_000_000)
 
</syntaxhighlight>
{{out}}
<pre>======
Task 2
======
 
Base-2 esthetic numbers from index 8 through index 12 inclusive:
 
10101010, 101010101, 1010101010, 10101010101, 101010101010
 
Base-3 esthetic numbers from index 12 through index 18 inclusive:
 
1210, 1212, 2101, 2121, 10101, 10121, 12101
 
Base-4 esthetic numbers from index 16 through index 24 inclusive:
 
323, 1010, 1012, 1210, 1212, 1232, 2101, 2121, 2123
 
Base-5 esthetic numbers from index 20 through index 30 inclusive:
 
323, 343, 432, 434, 1010, 1012, 1210, 1212, 1232, 1234, 2101
 
Base-6 esthetic numbers from index 24 through index 36 inclusive:
 
343, 345, 432, 434, 454, 543, 545, 1010, 1012, 1210, 1212, 1232, 1234
 
Base-7 esthetic numbers from index 28 through index 42 inclusive:
 
345, 432, 434, 454, 456, 543, 545, 565, 654, 656, 1010, 1012, 1210, 1212,
1232
 
Base-8 esthetic numbers from index 32 through index 48 inclusive:
 
432, 434, 454, 456, 543, 545, 565, 567, 654, 656, 676, 765, 767, 1010, 1012,
1210, 1212
 
Base-9 esthetic numbers from index 36 through index 54 inclusive:
 
434, 454, 456, 543, 545, 565, 567, 654, 656, 676, 678, 765, 767, 787, 876,
878, 1010, 1012, 1210
 
Base-10 esthetic numbers from index 40 through index 60 inclusive:
 
454, 456, 543, 545, 565, 567, 654, 656, 676, 678, 765, 767, 787, 789, 876,
878, 898, 987, 989, 1010, 1012
 
Base-11 esthetic numbers from index 44 through index 66 inclusive:
 
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 esthetic numbers from index 48 through index 72 inclusive:
 
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 esthetic numbers from index 52 through index 78 inclusive:
 
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 esthetic numbers from index 56 through index 84 inclusive:
 
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 esthetic numbers from index 60 through index 90 inclusive:
 
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 esthetic numbers from index 64 through index 96 inclusive:
 
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
 
======
Task 3
======
 
Base-10 esthetic numbers with magnitude 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
 
======
Task 4
======
 
Base-10 esthetic numbers with magnitude 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</pre>
 
 
===Python :: Functional===
{{Trans|Haskell}}
<syntaxhighlight lang="python">'''Esthetic numbers'''
 
from functools import reduce
from itertools import (
accumulate, chain, count, dropwhile,
islice, product, takewhile
)
from operator import add
from string import digits, ascii_lowercase
from textwrap import wrap
 
 
# estheticNumbersInBase :: Int -> [Int]
def estheticNumbersInBase(b):
'''Infinite stream of numbers which are
esthetic in a given base.
'''
return concatMap(
compose(
lambda deltas: concatMap(
lambda headDigit: concatMap(
compose(
fromBaseDigits(b),
scanl(add)(headDigit)
)
)(deltas)
)(range(1, b)),
replicateList([-1, 1])
)
)(count(0))
 
 
# ------------------------ TESTS -------------------------
def main():
'''Specified tests'''
def samples(b):
i, j = b * 4, b * 6
return '\n'.join([
f'Esthetics [{i}..{j}] for base {b}:',
unlines(wrap(
unwords([
showInBase(b)(n) for n in compose(
drop(i - 1), take(j)
)(
estheticNumbersInBase(b)
)
]), 60
))
])
 
def takeInRange(a, b):
return compose(
dropWhile(lambda x: x < a),
takeWhile(lambda x: x <= b)
)
 
print(
'\n\n'.join([
samples(b) for b in range(2, 1 + 16)
])
)
for (lo, hi) in [(1000, 9999), (100_000_000, 130_000_000)]:
print(f'\nBase 10 Esthetics in range [{lo}..{hi}]:')
print(
unlines(wrap(
unwords(
str(x) for x in takeInRange(lo, hi)(
estheticNumbersInBase(10)
)
), 60
))
)
 
 
# ------------------- BASES AND DIGITS -------------------
 
# fromBaseDigits :: Int -> [Int] -> [Int]
def fromBaseDigits(b):
'''An empty list if any digits are out of range for
the base. Otherwise a list containing an integer.
'''
def go(digitList):
maybeNum = reduce(
lambda r, d: None if r is None or (
0 > d or d >= b
) else r * b + d,
digitList, 0
)
return [] if None is maybeNum else [maybeNum]
return go
 
 
# toBaseDigits :: Int -> Int -> [Int]
def toBaseDigits(b):
'''A list of the digits of n in base b.
'''
def f(x):
return None if 0 == x else (
divmod(x, b)[::-1]
)
return lambda n: list(reversed(unfoldr(f)(n)))
 
 
# showInBase :: Int -> Int -> String
def showInBase(b):
'''String representation of n in base b.
'''
charSet = digits + ascii_lowercase
return lambda n: ''.join([
charSet[i] for i in toBaseDigits(b)(n)
])
 
 
# ----------------------- GENERIC ------------------------
 
# compose :: ((a -> a), ...) -> (a -> a)
def compose(*fs):
'''Composition, from right to left,
of a series of functions.
'''
def go(f, g):
def fg(x):
return f(g(x))
return fg
return reduce(go, fs, lambda x: x)
 
 
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''A concatenated list over which a function has been
mapped.
The list monad can be derived by using a function f
which wraps its output in a list, (using an empty
list to represent computational failure).
'''
def go(xs):
return chain.from_iterable(map(f, xs))
return go
 
 
# drop :: Int -> [a] -> [a]
# drop :: Int -> String -> String
def drop(n):
'''The sublist of xs beginning at
(zero-based) index n.
'''
def go(xs):
if isinstance(xs, (list, tuple, str)):
return xs[n:]
else:
take(n)(xs)
return xs
return go
 
 
# dropWhile :: (a -> Bool) -> [a] -> [a]
# dropWhile :: (Char -> Bool) -> String -> String
def dropWhile(p):
'''The suffix remainining after takeWhile p xs.
'''
return lambda xs: list(
dropwhile(p, xs)
)
 
 
# replicateList :: [a] -> Int -> [[a]]
def replicateList(xs):
'''All distinct lists of length n that
consist of elements drawn from xs.
'''
def rep(n):
def go(x):
return [[]] if 1 > x else [
([a] + b) for (a, b) in product(
xs, go(x - 1)
)
]
return go(n)
return rep
 
 
# scanl :: (b -> a -> b) -> b -> [a] -> [b]
def scanl(f):
'''scanl is like reduce, but defines a succession of
intermediate values, building from the left.
'''
def go(a):
def g(xs):
return accumulate(chain([a], xs), f)
return g
return go
 
 
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
def go(xs):
return list(islice(xs, n))
return go
 
 
# takeWhile :: (a -> Bool) -> [a] -> [a]
# takeWhile :: (Char -> Bool) -> String -> String
def takeWhile(p):
'''The longest (possibly empty) prefix of xs
in which all elements satisfy p.
'''
return lambda xs: list(
takewhile(p, xs)
)
 
 
# unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
def unfoldr(f):
'''Dual to reduce or foldr.
Where catamorphism reduces a list to a summary value,
the anamorphic unfoldr builds a list from a seed value.
As long as f returns (a, b) a is prepended to the list,
and the residual b is used as the argument for the next
application of f.
When f returns None, the completed list is returned.
'''
def go(v):
xr = v, v
xs = []
while True:
xr = f(xr[1])
if None is not xr:
xs.append(xr[0])
else:
return xs
return go
 
 
# unlines :: [String] -> String
def unlines(xs):
'''A single string formed by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
 
 
# unwords :: [String] -> String
def unwords(xs):
'''A space-separated string derived
from a list of words.
'''
return ' '.join(xs)
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>Esthetics [8..12] for base 2:
10101010 101010101 1010101010 10101010101 101010101010
 
Esthetics [12..18] for base 3:
1210 1212 2101 2121 10101 10121 12101
 
Esthetics [16..24] for base 4:
323 1010 1012 1210 1212 1232 2101 2121 2123
 
Esthetics [20..30] for base 5:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
 
Esthetics [24..36] for base 6:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
 
Esthetics [28..42] for base 7:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212
1232
 
Esthetics [32..48] for base 8:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010
1012 1210 1212
 
Esthetics [36..54] for base 9:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876
878 1010 1012 1210
 
Esthetics [40..60] for base 10:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876
878 898 987 989 1010 1012
 
Esthetics [44..66] for 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
 
Esthetics [48..72] for 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
 
Esthetics [52..78] for 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
 
Esthetics [56..84] for 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
 
Esthetics [60..90] for 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
 
Esthetics [64..96] for 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 Esthetics in range [1000..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 Esthetics in range [100000000..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</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ base share /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ dup 0 = iff
[ drop false ]
done
digits
true swap
behead swap
witheach
[ tuck - abs 1 != if
[ dip not conclude ] ]
drop ] is esthetic ( n --> b )
 
15 times
[ i^ 2 +
say "Base " dup echo cr
base put
[] 0
[ 1+
dup esthetic if
[ tuck join swap ]
over size
base share 6 * = until ]
drop
base share 4 * 1 - split nip
echo cr cr
base release ]
say "Decimal between 1000 and 9999: "
cr
0 temp put
9000 times
[ i^ 1000 + dup
esthetic iff
[ echo
1 temp tally
temp share
10 mod iff
sp else cr ]
else drop ]
temp release</syntaxhighlight>
 
{{out}}
 
<pre>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 ]
 
Decimal 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
</pre>
 
Line 4,392 ⟶ 6,645:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
sub esthetic($base = 10) {
Line 4,418 ⟶ 6,671:
my $top = @array.first: * > $hi, :k;
@array[^$top].grep: * > $lo
}</langsyntaxhighlight>
{{out}}
<div style="font-size:70%;">
Line 4,499 ⟶ 6,752:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*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.*/
Line 4,559 ⟶ 6,812:
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 4,616 ⟶ 6,869:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
basePlus = []
decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Line 4,681 ⟶ 6,934:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,734 ⟶ 6,987:
5654 5656 5676 5678 6543 6545 6565 6567 6765 6767 6787 6789 7654 7656 7676
7876 7878 7898 8765 8767 8787 8789 8987 8989 9876 9878 9898
</pre>
 
=={{header|RPL}}==
No brute force here, but a step-by-step generation, starting from a seed made up of the list of the digits of the base.
 
Task is accomplished in 999 seconds - precisely - on a basic HP-28S (4-bit CPU, 32 Mb RAM), but just in a few seconds on an iOS-based emulator.
≪ "0123456789ABCDEF" 1 3 PICK SUB
→ deb fin base digits
≪ { }
2 base '''FOR''' j
digits j DUP SUB + '''NEXT'''
1
'''DO'''
DUP2 GET
digits OVER DUP SIZE DUP SUB POS
→ idx nbr pos
≪ '''IF''' pos 1 ≠ '''THEN''' nbr digits pos 1 - DUP SUB + + '''END'''
'''IF''' pos base ≠ '''THEN''' nbr digits pos 1 + DUP SUB + + '''END'''
idx
1 +
'''UNTIL''' DUP fin ≥ '''END'''
DROP deb fin SUB
≫ ≫ '<span style="color:blue">ESTIC</span>' STO
2 16 '''FOR''' b
b 4 * b 6 * b <span style="color:blue">ESTIC</span> '''NEXT'''
59 119 10 <span style="color:blue">ESTIC</span>
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre style="height:40ex;overflow:scroll;">
16: { "10101010" "101010101" "1010101010" "10101010101" "101010101010" }
15: { "1210" "1212" "2101" "2121" "10101" "10121" "12101" }
14: { "323" "1010" "1012" "1210" "1212" "1232" "2101" "2121" "2123" }
13: { "323" "343" "432" "434" "1010" "1012" "1210" "1212" "1232" "1234" "2101" }
12: { "343" "345" "432" "434" "454" "543" "545" "1010" "1012" "1210" "1212" "1232" "1234" }
11: { "345" "432" "434" "454" "456" "543" "545" "565" "654" "656" "1010" "1012" "1210" "1212" "1232" }
10: { "432" "434" "454" "456" "543" "545" "565" "567" "654" "656" "676" "765" "767" "1010" "1012" "1210" "1212" }
9: { "434" "454" "456" "543" "545" "565" "567" "654" "656" "676" "678" "765" "767" "787" "876" "878" "1010" "1012" "1210" }
8: { "454" "456" "543" "545" "565" "567" "654" "656" "676" "678" "765" "767" "787" "789" "876" "878" "898" "987" "989" "1010" "1012" }
7: { "456" "543" "545" "565" "567" "654" "656" "676" "678" "765" "767" "787" "789" "876" "878" "898" "89A" "987" "989" "9A9" "A98" "A9A" "1010" }
6: { "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" }
5: { "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" }
4: { "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" }
3: { "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" }
2: { "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" }
1: { "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" }
</pre>
 
=={{header|Ruby}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ruby">def isEsthetic(n, b)
if n == 0 then
return false
Line 4,830 ⟶ 7,131:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 4,900 ⟶ 7,201:
............
123456789898987898 123456789898989876 123456789898989878 123456789898989898</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
// radix_fmt = "1.0"
 
// Returns the next esthetic number in the given base after n, where n is an
// esthetic number in that base or one less than a power of base.
fn next_esthetic_number(base: u64, n: u64) -> u64 {
if n + 1 < base {
return n + 1;
}
let mut a = n / base;
let mut b = a % base;
if n % base + 1 == b && b + 1 < base {
return n + 2;
}
a = next_esthetic_number(base, a);
b = a % base;
a * base + if b == 0 { 1 } else { b - 1 }
}
 
fn print_esthetic_numbers(min: u64, max: u64, numbers_per_line: usize) {
let mut numbers = Vec::new();
let mut n = next_esthetic_number(10, min - 1);
while n <= max {
numbers.push(n);
n = next_esthetic_number(10, n);
}
let count = numbers.len();
println!(
"Esthetic numbers in base 10 between {} and {} ({}):",
min, max, count
);
if count > 200 {
for i in 0..numbers_per_line {
if i != 0 {
print!(" ");
}
print!("{}", numbers[i]);
}
println!("\n............\n");
for i in 0..numbers_per_line {
if i != 0 {
print!(" ");
}
print!("{}", numbers[count - numbers_per_line + i]);
}
println!();
} else {
for i in 0..count {
print!("{}", numbers[i]);
if (i + 1) % numbers_per_line == 0 {
println!();
} else {
print!(" ");
}
}
if count % numbers_per_line != 0 {
println!();
}
}
}
 
fn main() {
for base in 2..=16 {
let min = base * 4;
let max = base * 6;
println!(
"Esthetic numbers in base {} from index {} through index {}:",
base, min, max
);
let mut n = 0;
for index in 1..=max {
n = next_esthetic_number(base, n);
if index >= min {
print!("{} ", radix_fmt::radix(n, base as u8));
}
}
println!("\n");
}
 
let mut min = 1000;
let mut max = 9999;
print_esthetic_numbers(min, max, 16);
println!();
 
min = 100000000;
max = 130000000;
print_esthetic_numbers(min, max, 8);
println!();
 
for i in 0..3 {
min *= 1000;
max *= 1000;
let numbers_per_line = match i {
0 => 7,
1 => 5,
_ => 4,
};
print_esthetic_numbers(min, max, numbers_per_line);
println!();
}
}</syntaxhighlight>
 
{{out}}
<pre>Esthetic numbers in base 2 from index 8 through index 12:
10101010 101010101 1010101010 10101010101 101010101010
 
Esthetic numbers in base 3 from index 12 through index 18:
1210 1212 2101 2121 10101 10121 12101
 
Esthetic numbers in base 4 from index 16 through index 24:
323 1010 1012 1210 1212 1232 2101 2121 2123
 
Esthetic numbers in base 5 from index 20 through index 30:
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
 
Esthetic numbers in base 6 from index 24 through index 36:
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
 
Esthetic numbers in base 7 from index 28 through index 42:
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
 
Esthetic numbers in base 8 from index 32 through index 48:
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
 
Esthetic numbers in base 9 from index 36 through index 54:
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
 
Esthetic numbers in base 10 from index 40 through index 60:
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
 
Esthetic numbers in base 11 from index 44 through index 66:
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010
 
Esthetic numbers in base 12 from index 48 through index 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
 
Esthetic numbers in base 13 from index 52 through index 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
 
Esthetic numbers in base 14 from index 56 through index 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
 
Esthetic numbers in base 15 from index 60 through index 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
 
Esthetic numbers in base 16 from index 64 through index 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
 
Esthetic numbers in base 10 between 1000 and 9999 (61):
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
 
Esthetic numbers in base 10 between 100000000 and 130000000 (126):
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
 
Esthetic numbers in base 10 between 100000000000 and 130000000000 (911):
101010101010 101010101012 101010101210 101010101212 101010101232 101010101234 101010121010
............
 
123456787678 123456787876 123456787878 123456787898 123456789876 123456789878 123456789898
 
Esthetic numbers in base 10 between 100000000000000 and 130000000000000 (6225):
101010101010101 101010101010121 101010101010123 101010101012101 101010101012121
............
 
123456789898767 123456789898787 123456789898789 123456789898987 123456789898989
 
Esthetic numbers in base 10 between 100000000000000000 and 130000000000000000 (44744):
101010101010101010 101010101010101012 101010101010101210 101010101010101212
............
 
123456789898987898 123456789898989876 123456789898989878 123456789898989898
 
</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func generate_esthetic(root, upto, callback, b=10) {
 
var v = root.digits2num(b)
Line 4,939 ⟶ 7,433:
 
say "\nBase 10 esthetic numbers between 100,000,000 and 130,000,000:"
between_esthetic(1e8, 13e7).slices(9).each { .join(' ').say }</langsyntaxhighlight>
{{out}}
<pre>
Line 5,009 ⟶ 7,503:
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789
</pre>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">extension Sequence {
func take(_ n: Int) -> [Element] {
var res = [Element]()
 
for el in self {
guard res.count != n else {
return res
}
 
res.append(el)
}
 
return res
}
}
 
extension String {
func isEsthetic(base: Int = 10) -> Bool {
zip(dropFirst(0), dropFirst())
.lazy
.allSatisfy({ abs(Int(String($0.0), radix: base)! - Int(String($0.1), radix: base)!) == 1 })
}
}
 
func getEsthetics(from: Int, to: Int, base: Int = 10) -> [String] {
guard base >= 2, to >= from else {
return []
}
 
var start = ""
var end = ""
 
repeat {
if start.count & 1 == 1 {
start += "0"
} else {
start += "1"
}
} while Int(start, radix: base)! < from
 
let digiMax = String(base - 1, radix: base)
let lessThanDigiMax = String(base - 2, radix: base)
var count = 0
 
repeat {
if count != base - 1 {
end += String(count + 1, radix: base)
count += 1
} else {
if String(end.last!) == digiMax {
end += lessThanDigiMax
} else {
end += digiMax
}
}
} while Int(end, radix: base)! < to
 
if Int(start, radix: base)! >= Int(end, radix: base)! {
return []
}
 
var esthetics = [Int]()
 
func shimmer(_ n: Int, _ m: Int, _ i: Int) {
if (n...m).contains(i) {
esthetics.append(i)
} else if i == 0 || i > m {
return
}
 
let d = i % base
let i1 = i &* base &+ d &- 1
let i2 = i1 &+ 2
 
if (i1 < i || i2 < i) {
// overflow
return
}
 
switch d {
case 0: shimmer(n, m, i2)
case base-1: shimmer(n, m, i1)
case _:
shimmer(n, m, i1)
shimmer(n, m, i2)
}
}
 
for digit in 0..<base {
shimmer(Int(start, radix: base)!, Int(end, radix: base)!, digit)
}
 
return esthetics.filter({ $0 <= to }).map({ String($0, radix: base) })
}
 
for base in 2...16 {
let esthetics = (0...)
.lazy
.map({ String($0, radix: base) })
.filter({ $0.isEsthetic(base: base) })
.dropFirst(base * 4)
.take((base * 6) - (base * 4) + 1)
 
print("Base \(base) esthetics from \(base * 4) to \(base * 6)")
print(esthetics)
print()
}
 
let base10Esthetics = (1000...9999).filter({ String($0).isEsthetic() })
 
print("\(base10Esthetics.count) esthetics between 1000 and 9999:")
print(base10Esthetics)
print()
 
func printSlice(of array: [String]) {
print(array.take(5))
print("...")
print(Array(array.lazy.reversed().take(5).reversed()))
print("\(array.count) total\n")
}
 
print("Esthetics between \(Int(1e8)) and \(13 * Int(1e7)):")
printSlice(of: getEsthetics(from: Int(1e8), to: 13 * Int(1e7)))
 
print("Esthetics between \(Int(1e11)) and \(13 * Int(1e10))")
printSlice(of: getEsthetics(from: Int(1e11), to: 13 * Int(1e10)))
 
print("Esthetics between \(Int(1e14)) and \(13 * Int(1e13)):")
printSlice(of: getEsthetics(from: Int(1e14), to: 13 * Int(1e13)))
 
print("Esthetics between \(Int(1e17)) and \(13 * Int(1e16)):")
printSlice(of: getEsthetics(from: Int(1e17), to: 13 * Int(1e16)))</syntaxhighlight>
 
{{out}}
 
<pre style="height: 500px">Base 2 esthetics from 8 to 12
["10101010", "101010101", "1010101010", "10101010101", "101010101010"]
 
Base 3 esthetics from 12 to 18
["1210", "1212", "2101", "2121", "10101", "10121", "12101"]
 
Base 4 esthetics from 16 to 24
["323", "1010", "1012", "1210", "1212", "1232", "2101", "2121", "2123"]
 
Base 5 esthetics from 20 to 30
["323", "343", "432", "434", "1010", "1012", "1210", "1212", "1232", "1234", "2101"]
 
Base 6 esthetics from 24 to 36
["343", "345", "432", "434", "454", "543", "545", "1010", "1012", "1210", "1212", "1232", "1234"]
 
Base 7 esthetics from 28 to 42
["345", "432", "434", "454", "456", "543", "545", "565", "654", "656", "1010", "1012", "1210", "1212", "1232"]
 
Base 8 esthetics from 32 to 48
["432", "434", "454", "456", "543", "545", "565", "567", "654", "656", "676", "765", "767", "1010", "1012", "1210", "1212"]
 
Base 9 esthetics from 36 to 54
["434", "454", "456", "543", "545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "876", "878", "1010", "1012", "1210"]
 
Base 10 esthetics from 40 to 60
["454", "456", "543", "545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "987", "989", "1010", "1012"]
 
Base 11 esthetics from 44 to 66
["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 esthetics from 48 to 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"]
 
Base 13 esthetics from 52 to 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"]
 
Base 14 esthetics from 56 to 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"]
 
Base 15 esthetics from 60 to 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"]
 
Base 16 esthetics from 64 to 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"]
 
61 esthetics 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]
 
Esthetics between 100000000 and 130000000:
["101010101", "101010121", "101010123", "101012101", "101012121"]
...
["123456567", "123456765", "123456767", "123456787", "123456789"]
126 total
 
Esthetics between 100000000000 and 130000000000
["101010101010", "101010101012", "101010101210", "101010101212", "101010101232"]
...
["123456787878", "123456787898", "123456789876", "123456789878", "123456789898"]
911 total
 
Esthetics between 100000000000000 and 130000000000000:
["101010101010101", "101010101010121", "101010101010123", "101010101012101", "101010101012121"]
...
["123456789898767", "123456789898787", "123456789898789", "123456789898987", "123456789898989"]
6225 total
 
Esthetics between 100000000000000000 and 130000000000000000:
["101010101010101010", "101010101010101012", "101010101010101210", "101010101010101212", "101010101010101232"]
...
["123456789898987878", "123456789898987898", "123456789898989876", "123456789898989878", "123456789898989898"]
44744 total</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var isEsthetic = Fn.new { |n, b|
Line 5,037 ⟶ 7,740:
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)
Line 5,052 ⟶ 7,754:
for (i in 0..9) dfs.call(n2, m2, i)
var le = esths.count
SystemFmt.print("Base 10: %(Fmt.dc(0$, le))d esthetic numbers between %(Fmt.dc(0$, n))d and %(Fmt.dc(0$,d", le, n, m))")
if (all) {
var c = 0
Line 5,086 ⟶ 7,788:
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)</langsyntaxhighlight>
 
{{out}}
Line 5,169 ⟶ 7,871:
 
123456789898767 123456789898787 123456789898789 123456789898987 123456789898989
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">proc NumOut(N); \Display N in the current Base
int N, R;
[N:= N/Base;
R:= rem(0);
if N # 0 then NumOut(N);
if Base <= 10 then
ChOut(0, R+^0)
else
ChOut(0, if R < 10 then R+^0 else R-10+^A);
];
 
func Esthetic(N); \Return 'true' if adjacent digits differ by 1
int N, D, D0;
[N:= N/Base;
D0:= rem(0);
while N # 0 do
[N:= N/Base;
D:= rem(0);
if D = D0 then return false;
if abs(D-D0) > 1 then return false;
D0:= D;
];
return true;
];
 
int Count, N;
[for Base:= 2 to 16 do
[Text(0, "Base "); IntOut(0, Base); Text(0, ": ");
Count:= 0; N:= 1;
loop [if Esthetic(N) then
[Count:= Count+1;
if Count >= Base*4 then
[NumOut(N); ChOut(0, ^ )];
if Count >= Base*6 then quit;
];
N:= N+1;
];
CrLf(0);
];
Base:= 10;
Text(0, "Base 10 numbers between 1000 and 9999:^m^j");
Count:= 0;
for N:= 1000 to 9999 do
[if Esthetic(N) then
[Count:= Count+1;
NumOut(N); ChOut(0, ^ );
if rem(Count/16) = 0 then CrLf(0);
];
];
CrLf(0);
Text(0, "Base 10 numbers between 1.0e8 and 1.3e8:^m^j");
Count:= 0;
for N:= 100_000_000 to 130_000_000 do
[if Esthetic(N) then
[Count:= Count+1;
NumOut(N); ChOut(0, ^ );
if rem(Count/9) = 0 then CrLf(0);
];
];
]</syntaxhighlight>
{{out}}
<pre>
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 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 numbers between 1.0e8 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
</pre>
1,969

edits