Jump to content

Largest int from concatenated ints: Difference between revisions

m
→‎{{header|REXX}}: simplified some code, added/changed whitespace and comments, used a smaller font for the output for the 2nd example.
(Updated to work with version 1.4 of Nim.)
m (→‎{{header|REXX}}: simplified some code, added/changed whitespace and comments, used a smaller font for the output for the 2nd example.)
Line 1,937:
 
The absolute value is used for negative numbers.
 
===simple integers===
<lang rexx>/*REXX program constructs the largest integer from an integer list using concatenation.*/
Line 1,944 ⟶ 1,943:
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
w=0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx= 1; big= norm(1) /*keep examining the list until done.*/
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */
L= max(length(big), length(#) ) /*get the maximum length of the integer*/
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big= #; idx= k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
z= delword(z, idx, 1) /*delete this maximum integer from list*/
$= $ || big big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is ─────► " $
end /*j*/ /* [↑] process each list of integers. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #= word(z, i); er=' '***error*** '; if left(#, 1)=="-" then #= substr(#, 2)
if \datatype(#,'W') then do; say er 'number' # "isn't an integer."; exit 13; end; endreturn #/1</lang>
return # / 1 /*it's an integer, then normalize it. */</lang>
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}
<pre>
Line 1,983 ⟶ 1,981:
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
@.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */
w=0 0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx=1; big= norm(1) /*keep examining the list until done.*/
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */
L= max(length(big), length(#) ) /*get the maximum length of the integer*/
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big=#; idx=k k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
z= delword(z, idx, 1) /*delete this maximum integer from list*/
$= $ || big big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is " $
Line 1,999 ⟶ 1,997:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #= word(z, i); er=' '***error*** '; if left(#, 1)=="-" then #= substr(#, 2)
if \datatype(#, 'N') then do;signal sayer13 er 'number' # "isn't an number."; exit 13; end /*go and tell err msg.*/
else #= # / 1 /*a #, so normalize it*/
if pos('E',#)>0 then do; parse var # mant "E" pow /*Has exponent? Expand*/
numeric digits pow + length(mand) /*expand digs, adjust#*/
end
if \datatype(#, 'W') then do; say er 'number' return # / "isn't an integer."; exit 13; end1
er13: say er # return"isn't #/1an integer."; exit 13</lang>
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}
 
<pre>
(Output shown at three-quarter size.)
 
<pre style="font-size:75%">
largest concatenatated integer from 1 34 3 98 9 76 45 4 is 998764543431
largest concatenatated integer from 54 546 548 60 is 6054854654
Cookies help us deliver our services. By using our services, you agree to our use of cookies.