Smallest numbers: Difference between revisions
Content added Content deleted
(Add Factor) |
(Added Algol 68) |
||
Line 4: | Line 4: | ||
Smallest number k > 0 such that the decimal expansion of k^k contains n, where '''n < 51''' |
Smallest number k > 0 such that the decimal expansion of k^k contains n, where '''n < 51''' |
||
<br><br> |
<br><br> |
||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
|||
Uses ALGOL 68G's LOMG LONG INT which provides large integers (the default precision is sufficient for the task). Also uses the ALGOL 68G string in string procedure. |
|||
<lang algol68>BEGIN # find the smallest k such that the decimal representation of k^k contains n for 0 <= n <= 50 # |
|||
# start with powers up to 20^20, if this proves insufficient, the kk array will be extended # |
|||
FLEX[ 1 : 20 ]STRING kk; |
|||
FOR k TO UPB kk DO kk[ k ] := whole( LONG LONG INT( k ) ^ k, 0 ) OD; |
|||
# find the numbers # |
|||
FOR i FROM 0 TO 50 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( k, -3 ) ) ) |
|||
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 TO UPB kk DO kk[ k ] := whole( LONG LONG INT( k ) ^ k, 0 ) OD; |
|||
try again := TRUE |
|||
FI |
|||
OD; |
|||
IF i MOD 10 = 9 THEN print( ( newline ) ) FI |
|||
OD |
|||
END</lang> |
|||
{{out}} |
|||
<pre> |
|||
9 1 3 5 2 4 4 3 7 9 |
|||
10 11 5 19 22 26 8 17 16 19 |
|||
9 8 13 7 17 4 17 3 11 18 |
|||
13 5 23 17 18 7 17 15 9 18 |
|||
16 17 9 7 12 28 6 23 9 24 |
|||
23 |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |