Esthetic numbers: Difference between revisions

Content added Content deleted
(Added Algol 68)
Line 37: Line 37:
:;*[https://www.geeksforgeeks.org/stepping-numbers/ Geeks for Geeks - Stepping numbers]
:;*[https://www.geeksforgeeks.org/stepping-numbers/ Geeks for Geeks - Stepping numbers]
<br><br>
<br><br>

=={{header|ALGOL 68}}==
As with other solutions here, uses brute-force for the main task, generates the numbers for the stretch goal.
<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;
OP ISESTHETIC = ( INT n, base )BOOL:
BEGIN
INT v := ABS n;
BOOL is esthetic := TRUE;
INT prev digit := v MOD base;
v OVERAB base;
WHILE v > 0 AND is esthetic
DO
INT next digit := v MOD base;
is esthetic := ABS ( next digit - prev digit ) = 1;
prev digit := next digit;
v OVERAB base
OD;
is esthetic
END # ISESTHETIC # ;
# returns an array of the first n esthetic numbers in the specified base #
PRIO ESTHETIC = 1;
OP ESTHETIC = ( INT n, base )[]INT:
BEGIN
[ 1 : n ]INT result;
INT e count := 0;
FOR i WHILE e count < n DO
IF i ISESTHETIC base THEN result[ e count +:= 1 ] := i FI
OD;
result
END # ESTHETIC # ;
# returns a string erpresentation of n in the specified base, 2 <= base <= 16 must be TRUE #
PRIO TOBASESTRING = 1;
OP TOBASESTRING = ( INT n, base )STRING:
IF base < 2 OR base > 16
THEN # invalid vbase #
"?" + whole( n, 0 ) + ":" + whole( base, 0 ) + "?"
ELSE
INT v := ABS n;
STRING digits = "0123456789abcdef";
STRING result := digits[ ( v MOD base ) + 1 ];
WHILE ( v OVERAB base ) > 0 DO
digits[ ( v MOD base ) + 1 ] +=: result
OD;
IF n < 0 THEN "-" +=: result FI;
result
FI # TOBASESTRING # ;
# sets count to the number of esthetic numbers with length digits in base b less than max #
# also displays the esthetic numbers #
PROC show esthetic = ( INT number, base, length, max, REF INT count )VOID:
IF length = 1
THEN # number is esthetic #
IF number <= max THEN
# number is in the required range #
print( ( " ", whole( number, 0 ) ) );
IF ( count +:= 1 ) MOD 9 = 0 THEN print( ( newline ) ) FI
FI
ELSE
# find the esthetic numbers that start with number #
INT digit = number MOD base;
INT prefix = number * base;
IF digit > 0 THEN # can have a lower digit #
show esthetic( prefix + ( digit - 1 ), base, length - 1, max, count )
FI;
IF digit < base - 1 THEN # can have a higher digit #
show esthetic( prefix + ( digit + 1 ), base, length - 1, max, count )
FI
FI # show esthetic # ;
# task #
# esthetic numbers from base * 4 to base * 6 for bases 2 to 16 #
FOR base FROM 2 TO 16 DO
INT e from = base * 4;
INT e to = base * 6;
print( ( "Esthetic numbers ", whole( e from, 0 ), " to ", whole( e to, 0 ), " in base ", whole( base, 0 ), newline ) );
[]INT e numbers = e to ESTHETIC base;
print( ( " " ) );
FOR n FROM e from TO e to DO
print( ( " ", e numbers[ n ] TOBASESTRING base ) )
OD;
print( ( newline ) )
OD;
# esthetic base 10 numbers between 1000 and 9999 #
print( ( "Base 10 eshetic numbers between 1000 and 9999", newline ) );
INT e count := 0;
FOR i FROM 1000 TO 9999 DO
IF i ISESTHETIC 10 THEN
print( ( " ", whole( i, 0 ) ) );
IF ( e count +:= 1 ) MOD 16 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline, newline ) );
print( ( "Esthetic numbers between 100 000 000 and 130 000 000:", newline ) );
e count := 0;
show esthetic( 1, 10, 9, 130 000 000, e count );
print( ( newline ) );
print( ( "Found ", whole( e count, 0 ), " esthetic numbers", newline ) )
END</lang>
{{out}}
<pre>
Esthetic numbers 8 to 12 in base 2
10101010 101010101 1010101010 10101010101 101010101010
Esthetic numbers 12 to 18 in base 3
1210 1212 2101 2121 10101 10121 12101
Esthetic numbers 16 to 24 in base 4
323 1010 1012 1210 1212 1232 2101 2121 2123
Esthetic numbers 20 to 30 in base 5
323 343 432 434 1010 1012 1210 1212 1232 1234 2101
Esthetic numbers 24 to 36 in base 6
343 345 432 434 454 543 545 1010 1012 1210 1212 1232 1234
Esthetic numbers 28 to 42 in base 7
345 432 434 454 456 543 545 565 654 656 1010 1012 1210 1212 1232
Esthetic numbers 32 to 48 in base 8
432 434 454 456 543 545 565 567 654 656 676 765 767 1010 1012 1210 1212
Esthetic numbers 36 to 54 in base 9
434 454 456 543 545 565 567 654 656 676 678 765 767 787 876 878 1010 1012 1210
Esthetic numbers 40 to 60 in base 10
454 456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 987 989 1010 1012
Esthetic numbers 44 to 66 in base 11
456 543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 a98 a9a 1010
Esthetic numbers 48 to 72 in base 12
543 545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba ba9 bab
Esthetic numbers 52 to 78 in base 13
545 565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb cba
Esthetic numbers 56 to 84 in base 14
565 567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc
Esthetic numbers 60 to 90 in base 15
567 654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd
Esthetic numbers 64 to 96 in base 16
654 656 676 678 765 767 787 789 876 878 898 89a 987 989 9a9 9ab a98 a9a aba abc ba9 bab bcb bcd cba cbc cdc cde dcb dcd ded def edc
Base 10 eshetic 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

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

Found 126 esthetic numbers
</pre>


=={{header|C}}==
=={{header|C}}==