Esthetic numbers: Difference between revisions
Content added Content deleted
(add RPL) |
|||
Line 4,128: | Line 4,128: | ||
123454543 123454545 123454565 123454567 123456543 123456545 |
123454543 123454545 123454565 123454567 123456543 123456545 |
||
123456565 123456567 123456765 123456767 123456787 123456789</pre> |
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 or .emit != null; |
|||
(.n % $b) as $j |
|||
| if (.i - $j)|length != 1 # abs |
|||
then .emit = false |
|||
else .n |= ((./$b)|floor) |
|||
| .i = $j |
|||
end) |
|||
| if .emit != null then .emit else true end |
|||
end; |
|||
# 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}}== |
=={{header|Julia}}== |