Sum to 100: Difference between revisions

→‎Batch processing: Rigt-to-left turned out to be about as messy.
m (→‎{{header|REXX}}: re-combined the last two lines into one (which somehow got separated).)
(→‎Batch processing: Rigt-to-left turned out to be about as messy.)
Line 2,204:
 
===Batch processing===
By the simple expedient of storing all evaluations in an array (which is not so large) and then sorting the array, the required results appear in a blink. The source is essentially F77 except for the usage of an array assignment of OP = -1, writing out the highest ten results via an array expression instead of a DO-loop, array OPNAME extending from -1 to +1, a CYCLE statement rather than a GO TO, and the use of the I0 format code. Subroutine DEBLANK is straightforward, and omitted. It was only to remove spaces from the text of the expression. Reading the expression from right to left is about as picky as left-to-right. <lang Fortran> INTEGER NDIGITS,TARGET !Document the shape.
PARAMETER (NDIGITS = 9, TARGET = 100)
INTEGER*1 OP(NDIGITS) !A set of operation codes, associated with each digit.
INTEGER N,D,P,S !Number, digit, power, sign.
CHARACTER*1 OPNAME(-1:+1) !Encodement of the operations.
PARAMETER (OPNAME = (/"-"," ","+"/)) !These will look nice.
Line 2,231:
100 LOOP = LOOP + 1 !Here we go again.
N = 0 !Clear the number.
D = 0 !AndNo theprevious waitingdigits digithave been seen.
SP = SIGN(1,OP(1)) !Syncopation.The -1power iffor OP(1)the isfirst -1, otherwise +1digit.
DO I = NDIGITS,1,NDIGITS -1 !StepGoing throughbackwards sees the operationsdigits prefixedbefore to eachthe digitsign.
D = D + I*P !Assimilate the digit string backwards.
IF (OP(I).EQ.0) THEN !A no-operation?
DP = DP*10 + I !Yes. GluePrepare the digitspower for the next digit togetherleftwards.
ELSE !Otherwise, an add or subtract hasthe digit beenstring's waitingvalue.
N = N + S*SIGN(D ,OP(I)) !DoBy ittransferring the sign to D..
D = I0 !GrabClear, theready digitfor associatedthe withnext thedigit operationstring.
SP = OP(I)1 !RecallThe howstarting itpower, beginsagain.
END IF !So much for that step.
END DO !On to the next.
IF (OP(1).EQ.0) N = N + S*D !Don'tProvide an implicit add forgetfor thean waitingunsigned digitstart.
VALUE(LOOP) = N !Save the value for later...
IF (N.EQ.TARGET) THEN !Well then?
Line 2,249 ⟶ 2,250:
101 FORMAT (10(A1,I1)) !Single-character operation codes, single-digit number parts.
CALL DEBLANK(TEXT,L) !Squeeze out the no-operations, so numbers are together.
WRITE (MSG,102) NHITN,TEXT(1:L) !Result!
102 FORMAT (I5,": ",A) !This should do.
END IF !So much for that.
1,220

edits