Task: Recaman's sequence


C++: #include <iostream>
#include <ostream>
#include <set>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
    auto i = v.cbegin();
    auto e = v.cend();
    os << '[';
    if (i != e) {
        os << *i;
        i = std::next(i);
    }
    while (i != e) {
        os << ", " << *i;
        i = std::next(i);
    }
    return os << ']';
}

int main() {
    using namespace std;

    vector<int> a{ 0 };
    set<int> used{ 0 };
    set<int> used1000{ 0 };
    bool foundDup = false;
    int n = 1;
    while (n <= 15 || !foundDup || used1000.size() < 1001) {
        int next = a[n - 1] - n;
        if (next < 1 || used.find(next) != used.end()) {
            next += 2 * n;
        }
        bool alreadyUsed = used.find(next) != used.end();
        a.push_back(next);
        if (!alreadyUsed) {
            used.insert(next);
            if (0 <= next && next <= 1000) {
                used1000.insert(next);
            }
        }
        if (n == 14) {
            cout << "The first 15 terms of the Recaman sequence are: " << a << '\n';
        }
        if (!foundDup && alreadyUsed) {
            cout << "The first duplicated term is a[" << n << "] = " << next << '\n';
            foundDup = true;
        }
        if (used1000.size() == 1001) {
            cout << "Terms up to a[" << n << "] are needed to generate 0 to 1000\n";
        }
        n++;
    }

    return 0;
}








COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. RECAMAN.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 RECAMAN-SEQUENCE COMP.
           02 A     PIC 999 OCCURS 99 TIMES INDEXED BY I.
           02 N     PIC 999 VALUE 0.
           
        01 VARIABLES COMP.
           02 ADDC  PIC S999.
           02 SUBC  PIC S999.
           02 SPTR  PIC 99 VALUE 1.
           
        01 OUTPUT-FORMAT.
           02 OUTI  PIC Z9.
           02 OUTN  PIC BZ9.
           02 OUTS  PIC X(79).
           
        PROCEDURE DIVISION.
        BEGIN.
            PERFORM GENERATE-NEXT-ITEM 15 TIMES.
            PERFORM COLLATE-ITEM VARYING I FROM 1 BY 1
                UNTIL I IS GREATER THAN 15.
            DISPLAY 'First 15 items:' OUTS.
        
        FIND-REPEATING.
            PERFORM GENERATE-NEXT-ITEM.
            SET I TO 1.
            SEARCH A VARYING I
                WHEN I IS NOT LESS THAN N 
                    NEXT SENTENCE
                WHEN A(I) IS EQUAL TO A(N)
                    SUBTRACT 1 FROM N GIVING OUTI
                    MOVE A(N) TO OUTN
                    DISPLAY 'First repeated item: A(' OUTI ') =' OUTN
                    STOP RUN.
            GO TO FIND-REPEATING.
        
        GENERATE-NEXT-ITEM.
            IF N IS EQUAL TO ZERO
                MOVE ZERO TO A(1)
            ELSE
                ADD N, A(N) GIVING ADDC
                SUBTRACT N FROM A(N) GIVING SUBC
                IF SUBC IS NOT GREATER THAN ZERO
                    MOVE ADDC TO A(N + 1)
                ELSE
                    SET I TO 1
                    SEARCH A VARYING I
                        WHEN I IS NOT LESS THAN N
                            MOVE SUBC TO A(N + 1)
                        WHEN A(I) IS EQUAL TO SUBC
                            MOVE ADDC TO A(N + 1).
            ADD 1 TO N.
            
        COLLATE-ITEM.
            MOVE A(I) TO OUTN.
            STRING OUTN DELIMITED BY SIZE INTO OUTS WITH POINTER SPTR.