Task: Cousin primes







Python: 

from itertools import chain, takewhile



def cousinPrimes():
    
    def go(x):
        n = 4 + x
        return [(x, n)] if isPrime(n) else []

    return chain.from_iterable(
        map(go, primes())
    )




def main():
    

    pairs = list(
        takewhile(
            lambda ab: 1000 > ab[1],
            cousinPrimes()
        )
    )

    print(f'{len(pairs)} cousin pairs below 1000:\n')
    print(
        spacedTable(list(
            chunksOf(4)([
                repr(x) for x in pairs
            ])
        ))
    )





def chunksOf(n):
    
    def go(xs):
        return (
            xs[i:n + i] for i in range(0, len(xs), n)
        ) if 0 < n else None
    return go



def isPrime(n):
    
    if n in (2, 3):
        return True
    if 2 > n or 0 == n % 2:
        return False
    if 9 > n:
        return True
    if 0 == n % 3:
        return False

    def p(x):
        return 0 == n % x or 0 == n % (2 + x)

    return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))



def primes():
    
    n = 2
    dct = {}
    while True:
        if n in dct:
            for p in dct[n]:
                dct.setdefault(n + p, []).append(p)
            del dct[n]
        else:
            yield n
            dct[n * n] = [n]
        n = 1 + n



def listTranspose(xss):
    
    def go(xss):
        if xss:
            h, *t = xss
            return (
                [[h[0]] + [xs[0] for xs in t if xs]] + (
                    go([h[1:]] + [xs[1:] for xs in t])
                )
            ) if h and isinstance(h, list) else go(t)
        else:
            return []
    return go(xss)



def spacedTable(rows):
    
    columnWidths = [
        len(str(row[-1])) for row in listTranspose(rows)
    ]
    return '\n'.join([
        ' '.join(
            map(
                lambda w, s: s.rjust(w, ' '),
                columnWidths, row
            )
        ) for row in rows
    ])



if __name__ == '__main__':
    main()



COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. COUSIN-PRIMES.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 PRIME-SIEVE.
           02 PRIME-FLAG        PIC 9 OCCURS 1000 INDEXED BY P, Q.
              88 PRIME          VALUE 1.
           02 STEP-SIZE         PIC 999.
           02 X                 PIC 999.
           02 P-START           PIC 999.
           02 AMOUNT            PIC 999 VALUE 0.
        01 OUTPUT-FORMAT.
           02 COUSIN1           PIC ZZ9.
           02 COUSIN2           PIC ZZ9.
           
        PROCEDURE DIVISION.
        BEGIN.
            PERFORM SIEVE.
            PERFORM TEST-COUSINS VARYING P FROM 2 BY 1
                UNTIL P IS GREATER THAN 996.
            MOVE AMOUNT TO COUSIN1.
            DISPLAY COUSIN1 ' pairs found.'
            STOP RUN.
        
        TEST-COUSINS.
            IF PRIME(P) AND PRIME(P + 4)
                SET X TO P 
                MOVE X TO COUSIN1
                ADD X, 4 GIVING COUSIN2
                DISPLAY COUSIN1 ' ' COUSIN2
                ADD 1 TO AMOUNT.
        
        SIEVE SECTION.
        BEGIN.
            PERFORM FLAG-PRIME VARYING Q FROM 1 BY 1
                UNTIL Q IS GREATER THAN 1000.
            PERFORM SIEVE-PRIME VARYING P FROM 2 BY 1
                UNTIL P IS GREATER THAN 32.
            GO TO DONE.
        
        SIEVE-PRIME.
            IF PRIME(P)
                SET X TO P
                COMPUTE P-START = X ** 2
                PERFORM UNFLAG-PRIME VARYING Q FROM P-START BY X
                    UNTIL Q IS GREATER THAN 1000.
                    
        FLAG-PRIME.   MOVE 1 TO PRIME-FLAG(Q).
        UNFLAG-PRIME. MOVE 0 TO PRIME-FLAG(Q).
        DONE. EXIT.