Task: Floyd's triangle







Python: >>> def floyd(rowcount=5):
	rows = [[1]]
	while len(rows) < rowcount:
		n = rows[-1][-1] + 1
		rows.append(list(range(n, n + len(rows[-1]) + 1)))
	return rows

>>> floyd()
[[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15]]
>>> def pfloyd(rows=[[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]):
	colspace = [len(str(n)) for n in rows[-1]]
	for row in rows:
		print( ' '.join('%*i' % space_n for space_n in zip(colspace, row)))

		
>>> pfloyd()
1
2 3
4 5 6
7 8 9 10
>>> pfloyd(floyd(5))
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
>>> pfloyd(floyd(14))
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44  45
46 47 48 49 50 51 52 53  54  55
56 57 58 59 60 61 62 63  64  65  66
67 68 69 70 71 72 73 74  75  76  77  78
79 80 81 82 83 84 85 86  87  88  89  90  91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
>>>



COBOL:         IDENTIFICATION DIVISION.
        PROGRAM-ID. FLOYD-TRIANGLE.
        
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 VARIABLES        COMP.
           02 NUM-LINES     PIC 99.
           02 CUR-LINE      PIC 99.
           02 CUR-COL       PIC 99.
           02 CUR-NUM       PIC 999.
           02 ZERO-SKIP     PIC 9.
           02 LINE-PTR      PIC 99.
           02 MAX-NUM       PIC 999.
        
        01 OUTPUT-FORMAT.
           02 OUT-LINE      PIC X(72).
           02 ONE-DIGIT     PIC B9.
           02 TWO-DIGITS    PIC BZ9.
           02 THREE-DIGITS  PIC BZZ9.
           02 MAX-COL-NUM   PIC 999.
        
        PROCEDURE DIVISION.
        BEGIN.
            MOVE 5 TO NUM-LINES. PERFORM FLOYD.
            DISPLAY ' '.
            MOVE 14 TO NUM-LINES. PERFORM FLOYD.
            STOP RUN.
            
        FLOYD.
            MOVE 1 TO CUR-NUM.
            COMPUTE MAX-NUM = NUM-LINES * (NUM-LINES + 1) / 2.
            PERFORM FLOYD-LINE 
                VARYING CUR-LINE FROM 1 BY 1
                UNTIL CUR-LINE IS GREATER THAN NUM-LINES.
            
        FLOYD-LINE.
            MOVE ' ' TO OUT-LINE.
            MOVE 1 TO LINE-PTR.
            PERFORM FLOYD-NUM
                VARYING CUR-COL FROM 1 BY 1
                UNTIL CUR-COL IS GREATER THAN CUR-LINE.
            DISPLAY OUT-LINE.

        FLOYD-NUM.
            COMPUTE MAX-COL-NUM = MAX-NUM - NUM-LINES + CUR-COL.
            MOVE 0 TO ZERO-SKIP.
            INSPECT MAX-COL-NUM TALLYING ZERO-SKIP FOR LEADING '0'.
            IF ZERO-SKIP IS EQUAL TO ZERO
                PERFORM FLOYD-THREE-DIGITS
            ELSE IF ZERO-SKIP IS EQUAL TO 1
                PERFORM FLOYD-TWO-DIGITS
            ELSE IF ZERO-SKIP IS EQUAL TO 2
                PERFORM FLOYD-ONE-DIGIT.
            ADD 1 TO CUR-NUM.
                
        FLOYD-ONE-DIGIT.
            MOVE CUR-NUM TO ONE-DIGIT.
            STRING ONE-DIGIT DELIMITED BY SIZE INTO OUT-LINE
                WITH POINTER LINE-PTR.
        
        FLOYD-TWO-DIGITS.
            MOVE CUR-NUM TO TWO-DIGITS.
            STRING TWO-DIGITS DELIMITED BY SIZE INTO OUT-LINE
                WITH POINTER LINE-PTR.
        
        FLOYD-THREE-DIGITS.
            MOVE CUR-NUM TO THREE-DIGITS.
            STRING THREE-DIGITS DELIMITED BY SIZE INTO OUT-LINE
                WITH POINTER LINE-PTR.