Recaman's sequence: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) →{{header|PL/I}}: remove two unnecessary declarations |
Not a robot (talk | contribs) Add PL/M |
||
Line 1,918:
<pre>First 15 members: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
First repeated term: A(24) = 42</pre>
=={{header|PL/M}}==
<lang pli>100H:
BDOS: PROCEDURE(F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PRINT$CH: PROCEDURE(C); DECLARE C BYTE; CALL BDOS(2,C); END PRINT$CH;
PRINT$STR: PROCEDURE(S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT$STR;
/* PRINT NUMBER */
PRINT$NUM: PROCEDURE(N);
DECLARE (N, P) ADDRESS, C BASED P BYTE;
DECLARE S(6) BYTE INITIAL('.....$');
P = .S(5);
DIGIT:
P = P-1;
C = '0' + N MOD 10;
N = N/10;
IF N>0 THEN GO TO DIGIT;
CALL PRINT$STR(P);
END PRINT$NUM;
/* IS X IN THE FIRST N TERMS OF THE SEQUENCE */
FIND: PROCEDURE(SEQ,X,N) BYTE;
DECLARE SEQ ADDRESS, (I, X, N, A BASED SEQ) BYTE;
DO I=0 TO N-1;
IF A(I)=X THEN RETURN 0FFH;
END;
RETURN 0;
END FIND;
/* GENERATE THE N'TH TERM OF THE SEQUENCE */
GENERATE: PROCEDURE(SEQ,N) BYTE;
DECLARE SEQ ADDRESS, (N, A BASED SEQ) BYTE;
IF N=0 THEN
A(N)=0;
ELSE DO;
DECLARE (SUB, ADD) BYTE;
SUB = A(N-1) - N;
ADD = A(N-1) + N;
/* A(N-1) - N NEGATIVE? */
IF A(N-1) <= N THEN
A(N) = ADD;
/* A(N-1) - N ALREADY GENERATED? */
ELSE IF FIND(SEQ,SUB,N) THEN
A(N) = ADD;
ELSE
A(N) = SUB;
END;
RETURN A(N);
END GENERATE;
DECLARE I BYTE, A(30) BYTE;
CALL PRINT$STR(.'FIRST 15 MEMBERS: $');
DO I=0 TO 14;
CALL PRINT$NUM(GENERATE(.A, I));
CALL PRINT$CH(' ');
END;
CALL PRINT$STR(.(13,10,'FIRST REPEATED TERM: A($'));
I=15;
DO WHILE NOT FIND(.A, GENERATE(.A, I), I);
I = I+1;
END;
CALL PRINT$NUM(I);
CALL PRINT$STR(.') = $');
CALL PRINT$NUM(A(I));
CALL PRINT$STR(.(13,10,'$'));
CALL EXIT;
EOF</lang>
{{out}}
<pre>FIRST 15 MEMBERS: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
FIRST REPEATED TERM: A(24) = 42</pre>
=={{header|PureBasic}}==
|