Largest int from concatenated ints: Difference between revisions

→‎{{header|REXX}}: added a REXX version that supports exponentiated intergers.
m (→‎{{header|REXX}}: changed a comment.)
(→‎{{header|REXX}}: added a REXX version that supports exponentiated intergers.)
Line 1,952:
The   '''numeric digits'''   (the number of decimal digits for the precision)   is automatically adjusted for any sized integer.
<br>The absolute value is used for negative numbers.
 
===simple integers===
<lang rexx>/*REXX program constructs the largest integer from an integer list using concatenation.*/
@.=.; @.1 = '{1, 34, 3, 98, 9, 76, 45, 4}' /*the 1st integer list to be used. */
Line 1,986 ⟶ 1,988:
6054854654 max for: {54, 546, 548, 60}
554454 max for: { 4, 45, 54, 5}
</pre>
 
===exponentiated integers===
In REXX, a number such as &nbsp; '''6.6e77''' &nbsp; would be considered an integer &nbsp; ''if'' &nbsp; the (current) &nbsp; '''numeric digits''' &nbsp; is
<br>large enough to express that number as an integer without the exponent.
<lang rexx>/*REXX program constructs the largest integer from an integer list using concatenation.*/
@.=.; @.1 = '{1, 34, 3, 98, 9, 76, 45, 4}' /*the 1st integer list to be used. */
@.2 = '{54, 546, 548, 60}' /* " 2nd " " " " " */
@.3 = '{ 4, 45, 54, 5}' /* " 3rd " " " " " */
@.4 = '{ 4, 45, 54, 5, 6.6e77}' /* " 4th " " " " " */
/* [↓] process all the integer lists.*/
do j=1 while @.j\==.; $= /*keep truckin' until lists exhausted. */
z=space( translate(@.j, , '])},{([') ) /*perform scrubbing on the integer list*/
_=length( space(z, 0) ) + 2 /*determine largest integer possibility*/
if _>digits() then numeric digits _ /*use enough decimal digits for maximum*/
/* [↓] examine each number in the list*/
do while z\==''; p=uPow(1) /*keep examining the list until done.*/
if p\==0 then numeric digits p /*# has big exponent? Then adjust digs*/
big=isOK(y); index=1; LB=length(big) /*assume that first integer is biggest.*/
/* [↓] check the rest of the integers.*/
do k=2 to words(z); p=uPow(k) /*obtain an a number from the list. */
if p\==0 then numeric digits p + Lm +3 /*# has big exponent? Then adjust digs*/
#=isOK(y) /*obtain an an integer from the list. */
L=max(LB, length(#) ) /*get the maximum length of the integer*/
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big=#; index=k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
 
z=space( delword(z, index, 1) ) /*delete this maximum integer from list*/
$=$ || big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
 
say right($, digits()) ' max for: ' @.j /*show the maximum integer and the list*/
end /*j*/ /* [↑] process each list of integers. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1 /*normalize the integer.*/
say; say '***error*** number ' ? "isn't an integer."; say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
uPow: y=word(z, arg(1) ); upper y /*extract a number from the list. */
if \datatype(y, 'N') then return 0 /*isn't a number, let isOK handle it.*/
if pos('E', y)==0 then return 0 /*Has the number an exponent? No, ret.*/
parse var y mant 'E' pow; Lm=length(mant) /*get mantissa's length and exponent. */
if pow +Lm <=digits() then return 0 /*is number smaller than current digs? */
return pow + Lm /*use a new NUMERIC DIGITS pow + Lm */</lang>
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}
<pre>
998764543431 max for: {1, 34, 3, 98, 9, 76, 45, 4}
6054854654 max for: {54, 546, 548, 60}
554454 max for: { 4, 45, 54, 5}
660000000000000000000000000000000000000000000000000000000000000000000000000000545454 max for: { 4, 45, 54, 5, 6.6e77}
</pre>