Smallest power of 6 whose decimal expansion contains n: Difference between revisions

Added Algol 68
(→‎{{header|Pascal}}: searching for strings changed to Phix version.)
(Added Algol 68)
Line 6:
 
 
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses ALGOL 68G's LONG LONG INT large integers, the default precision is sufficient for this task. Also uses the ALGOL 68G specific string in string procedure.
<lang algol68>BEGIN # find the smallest k such that the decimal representation of 6^k contains n for 0 <= n <= 21 #
# returns s blank-padded on the right to at least len characters #
PROC right pad = ( STRING s, INT len )STRING:
BEGIN
INT s len = ( UPB s - LWB s ) + 1;
IF s len >= len THEN s ELSE s + ( len - s len ) * " " FI
END # right pad # ;
# returns s blank-padded on the left to at least len characters #
PROC left pad = ( STRING s, INT len )STRING:
BEGIN
INT s len = ( UPB s - LWB s ) + 1;
IF s len >= len THEN s ELSE ( ( len - s len ) * " " ) + s FI
END # left pad # ;
# returns a string representation of unformatted with space separators #
PROC space separate = ( STRING unformatted )STRING:
BEGIN
STRING result := "";
INT ch count := 0;
FOR c FROM UPB unformatted BY -1 TO LWB unformatted DO
IF ch count <= 2 THEN ch count +:= 1
ELSE ch count := 1; " " +=: result
FI;
unformatted[ c ] +=: result
OD;
result
END # space separate # ;
# start with powers up to 6^12, if this proves insufficient, the kk array will be extended #
FLEX[ 0 : 12 ]STRING kk;
FOR k FROM LWB kk TO UPB kk DO kk[ k ] := whole( LONG LONG INT( 6 ) ^ k, 0 ) OD;
# find the numbers #
FOR i FROM 0 TO 21 DO
STRING n = whole( i, 0 );
BOOL try again := TRUE;
WHILE try again DO
try again := FALSE;
BOOL found := FALSE;
FOR k FROM LWB kk TO UPB kk WHILE NOT found DO
IF string in string( n, NIL, kk[ k ] ) THEN
found := TRUE;
print( ( whole( i, -2 ), right pad( ": 6^" + whole( k, 0 ), 8 ), " ", left pad( space separate( kk[ k ] ), 30 ), newline ) )
FI
OD;
IF NOT found THEN
# haven't got enough k^k values - get some more #
kk := HEAP[ 1 : UPB kk * 2 ]STRING;
FOR k FROM LWB kk TO UPB kk DO kk[ k ] := whole( LONG LONG INT( 6 ) ^ k, 0 ) OD;
try again := TRUE
FI
OD
OD
END</lang>
{{out}}
<pre>
0: 6^9 10 077 696
1: 6^0 1
2: 6^3 216
3: 6^2 36
4: 6^6 46 656
5: 6^6 46 656
6: 6^1 6
7: 6^5 7 776
8: 6^12 2 176 782 336
9: 6^4 1 296
10: 6^9 10 077 696
11: 6^16 2 821 109 907 456
12: 6^4 1 296
13: 6^13 13 060 694 016
14: 6^28 6 140 942 214 464 815 497 216
15: 6^18 101 559 956 668 416
16: 6^3 216
17: 6^10 60 466 176
18: 6^15 470 184 984 576
19: 6^21 21 936 950 640 377 856
20: 6^26 170 581 728 179 578 208 256
21: 6^3 216
</pre>
 
=={{header|C}}==
3,032

edits