WeixiangYan/CodeTransOcean huggingface dataset:
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
>>>
Task: Floyd's triangle








VB: Option Explicit
Dim o As String
Sub floyd(L As Integer)
    Dim r, c, m, n As Integer
    n = L * (L - 1) / 2
    m = 1
    For r = 1 To L
        o = o & vbCrLf
        For c = 1 To r
            o = o & Space(Len(CStr(n + c)) - Len(CStr(m))) & m & " "
            m = m + 1
        Next
    Next
End Sub
Sub triangle()
    o = "5 lines"
    Call floyd(5)
    o = o & vbCrLf & "14 lines"
    Call floyd(14)
    With Selection
        .Font.Name = "Courier New"
        .TypeText Text:=o
    End With
End Sub
Task: Floyd's triangle




Go: package main

import "fmt"

func main() {
    floyd(5)
    floyd(14)
}

func floyd(n int) {
    fmt.Printf("Floyd %d:\n", n)
    lowerLeftCorner := n*(n-1)/2 + 1
    lastInColumn := lowerLeftCorner
    lastInRow := 1
    for i, row := 1, 1; row <= n; i++ {
        w := len(fmt.Sprint(lastInColumn))
        if i < lastInRow {
            fmt.Printf("%*d ", w, i)
            lastInColumn++
        } else {
            fmt.Printf("%*d\n", w, i)
            row++
            lastInRow += row
            lastInColumn = lowerLeftCorner
        }
    }
}
Task: Floyd's triangle



C#: using System;
using System.Text;

public class FloydsTriangle
{
    internal static void Main(string[] args)
    {
        int count;
        if (args.Length >= 1 && int.TryParse(args[0], out count) && count > 0)
        {
            Console.WriteLine(MakeTriangle(count));
        }
        else
        {
            Console.WriteLine(MakeTriangle(5));
            Console.WriteLine();
            Console.WriteLine(MakeTriangle(14));
        }
    }

    public static string MakeTriangle(int rows)
    {
        int maxValue = (rows * (rows + 1)) / 2;
        int digit = 0;
        StringBuilder output = new StringBuilder();

        for (int row = 1; row <= rows; row++)
        {
            for (int column = 0; column < row; column++)
            {
                int colMaxDigit = (maxValue - rows) + column + 1;
                if (column > 0)
                {
                    output.Append(' ');
                }

                digit++;
                output.Append(digit.ToString().PadLeft(colMaxDigit.ToString().Length));
            }

            output.AppendLine();
        }

        return output.ToString();
    }
}








Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle


C++: #include <windows.h>
#include <sstream>
#include <iostream>


using namespace std;


class floyds_tri
{
public:
    floyds_tri()  { lastLineLen = 0; }
    ~floyds_tri() { killArray(); }

    void create( int rows )
    {
	_rows = rows;
	calculateLastLineLen();
	display();
    }

private:
    void killArray()
    {
	if( lastLineLen ) 
	    delete [] lastLineLen;
    }

    void calculateLastLineLen()
    {
	killArray();
	lastLineLen = new BYTE[_rows];

	int s = 1 + ( _rows * ( _rows - 1 ) ) / 2;

	for( int x = s, ix = 0; x < s + _rows; x++, ix++ )
	{
	    ostringstream cvr;
	    cvr << x;
	    lastLineLen[ix] = static_cast<BYTE>( cvr.str().size() );
	}
    }

    void display()
    {
	cout << endl << "Floyd\'s Triangle - " << _rows << " rows" << endl << "===============================================" << endl;
	int number = 1;
	for( int r = 0; r < _rows; r++ )
	{
	    for( int c = 0; c <= r; c++ )
	    {
		ostringstream cvr;
		cvr << number++;
		string str = cvr.str();
		while( str.length() < lastLineLen[c] )
		    str = " " + str;
		cout << str << " ";
	    }
	    cout << endl;
	}
    }

    int _rows;
    BYTE* lastLineLen;
};

int main( int argc, char* argv[] )
{
    floyds_tri t;
    int s;
    while( true )
    {
	cout << "Enter the size of the triangle ( 0 to QUIT ): "; cin >> s;
	if( !s ) return 0;
	if( s > 0 ) t.create( s );

	cout << endl << endl;
	system( "pause" );
    }

    return 0;
}










Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle

C: #include <stdio.h>

void t(int n)
{
	int i, j, c, len;

	i = n * (n - 1) / 2;
	for (len = c = 1; c < i; c *= 10, len++);
	c -= i; 

#define SPEED_MATTERS 0
#if SPEED_MATTERS	
	char tmp[32], s[4096], *p;

	sprintf(tmp, "%*d", len, 0);

	inline void inc_numstr(void) {
		int k = len;

	redo:	if (!k--) return;

		if (tmp[k] == '9') {
			tmp[k] = '0';
			goto redo;
		}

		if (++tmp[k] == '!')
			tmp[k] = '1';
	}

	for (p = s, i = 1; i <= n; i++) {
		for (j = 1; j <= i; j++) {
			inc_numstr();
			__builtin_memcpy(p, tmp + 1 - (j >= c), len - (j < c));
			p += len - (j < c);

			*(p++) = (i - j)? ' ' : '\n';

			if (p - s + len >= 4096) {
				fwrite(s, 1, p - s, stdout);
				p = s;
			}
		}
	}

	fwrite(s, 1, p - s, stdout);
#else 
	int num;
	for (num = i = 1; i <= n; i++)
		for (j = 1; j <= i; j++)
			printf("%*d%c",	len - (j < c), num++, i - j ? ' ':'\n');
#endif
}

int main(void)
{
	t(5), t(14);

	
	
	return 0;
}










Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle




Go: package main

import "fmt"

func main() {
    floyd(5)
    floyd(14)
}

func floyd(n int) {
    fmt.Printf("Floyd %d:\n", n)
    lowerLeftCorner := n*(n-1)/2 + 1
    lastInColumn := lowerLeftCorner
    lastInRow := 1
    for i, row := 1, 1; row <= n; i++ {
        w := len(fmt.Sprint(lastInColumn))
        if i < lastInRow {
            fmt.Printf("%*d ", w, i)
            lastInColumn++
        } else {
            fmt.Printf("%*d\n", w, i)
            row++
            lastInRow += row
            lastInColumn = lowerLeftCorner
        }
    }
}







Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle





Java: public class Floyd {
	public static void main(String[] args){
		printTriangle(5);
		printTriangle(14);
	}
	
	private static void printTriangle(int n){
		System.out.println(n + " rows:");
		for(int rowNum = 1, printMe = 1, numsPrinted = 0;
				rowNum <= n; printMe++){
			int cols = (int)Math.ceil(Math.log10(n*(n-1)/2 + numsPrinted + 2));
			System.out.printf("%"+cols+"d ", printMe);
			if(++numsPrinted == rowNum){
				System.out.println();
				rowNum++;
				numsPrinted = 0;
			}
		}
	}
}






Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
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
>>>




Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle








VB: Option Explicit
Dim o As String
Sub floyd(L As Integer)
    Dim r, c, m, n As Integer
    n = L * (L - 1) / 2
    m = 1
    For r = 1 To L
        o = o & vbCrLf
        For c = 1 To r
            o = o & Space(Len(CStr(n + c)) - Len(CStr(m))) & m & " "
            m = m + 1
        Next
    Next
End Sub
Sub triangle()
    o = "5 lines"
    Call floyd(5)
    o = o & vbCrLf & "14 lines"
    Call floyd(14)
    With Selection
        .Font.Name = "Courier New"
        .TypeText Text:=o
    End With
End Sub



Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle






PHP: <?php
floyds_triangle(5);
floyds_triangle(14);

function floyds_triangle($n) {
    echo "n = " . $n . "\r\n";

    for($r = 1, $i = 1, $c = 0; $r <= $n; $i++) {
        $cols = ceil(log10($n*($n-1)/2 + $c + 2));
        printf("%".$cols."d ", $i);
        if(++$c == $r) {
            echo "\r\n";
            $r++;
            $c = 0;
        }
    }
?>





Fortran: 































program p
  integer, dimension(2) :: examples = [5, 14]
  integer :: i
  do i=1, size(examples)
    call floyd(examples(i))
    write(6, '(/)')
  end do

contains

  subroutine floyd(rows)
    integer, intent(in) :: rows
    integer :: n, i, j, k
    integer, dimension(60) :: L
    character(len=504) :: fmt
    n = (rows*(rows+1))/2 
    do i=1,rows 
      L(i) = 2+int(log10(real(n-rows+i)))
    end do
    k = 0
    do i=1,rows
      do j=1,i
        k = k+1
        write(fmt,'(a2,i1,a1)')'(i',L(j),')'
        write(6,fmt,advance='no') k
      enddo
      write(6,*) ''
    end do
  end subroutine floyd
  
end program p
Task: Floyd's triangle





Java: public class Floyd {
	public static void main(String[] args){
		printTriangle(5);
		printTriangle(14);
	}
	
	private static void printTriangle(int n){
		System.out.println(n + " rows:");
		for(int rowNum = 1, printMe = 1, numsPrinted = 0;
				rowNum <= n; printMe++){
			int cols = (int)Math.ceil(Math.log10(n*(n-1)/2 + numsPrinted + 2));
			System.out.printf("%"+cols+"d ", printMe);
			if(++numsPrinted == rowNum){
				System.out.println();
				rowNum++;
				numsPrinted = 0;
			}
		}
	}
}





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.
Task: Floyd's triangle








VB: Option Explicit
Dim o As String
Sub floyd(L As Integer)
    Dim r, c, m, n As Integer
    n = L * (L - 1) / 2
    m = 1
    For r = 1 To L
        o = o & vbCrLf
        For c = 1 To r
            o = o & Space(Len(CStr(n + c)) - Len(CStr(m))) & m & " "
            m = m + 1
        Next
    Next
End Sub
Sub triangle()
    o = "5 lines"
    Call floyd(5)
    o = o & vbCrLf & "14 lines"
    Call floyd(14)
    With Selection
        .Font.Name = "Courier New"
        .TypeText Text:=o
    End With
End Sub


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.
Task: Floyd's triangle




Go: package main

import "fmt"

func main() {
    floyd(5)
    floyd(14)
}

func floyd(n int) {
    fmt.Printf("Floyd %d:\n", n)
    lowerLeftCorner := n*(n-1)/2 + 1
    lastInColumn := lowerLeftCorner
    lastInRow := 1
    for i, row := 1, 1; row <= n; i++ {
        w := len(fmt.Sprint(lastInColumn))
        if i < lastInRow {
            fmt.Printf("%*d ", w, i)
            lastInColumn++
        } else {
            fmt.Printf("%*d\n", w, i)
            row++
            lastInRow += row
            lastInColumn = lowerLeftCorner
        }
    }
}






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.
Task: Floyd's triangle






PHP: <?php
floyds_triangle(5);
floyds_triangle(14);

function floyds_triangle($n) {
    echo "n = " . $n . "\r\n";

    for($r = 1, $i = 1, $c = 0; $r <= $n; $i++) {
        $cols = ceil(log10($n*($n-1)/2 + $c + 2));
        printf("%".$cols."d ", $i);
        if(++$c == $r) {
            echo "\r\n";
            $r++;
            $c = 0;
        }
    }
?>
Task: Floyd's triangle






PHP: <?php
floyds_triangle(5);
floyds_triangle(14);

function floyds_triangle($n) {
    echo "n = " . $n . "\r\n";

    for($r = 1, $i = 1, $c = 0; $r <= $n; $i++) {
        $cols = ceil(log10($n*($n-1)/2 + $c + 2));
        printf("%".$cols."d ", $i);
        if(++$c == $r) {
            echo "\r\n";
            $r++;
            $c = 0;
        }
    }
?>






Ada: with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Command_Line;

procedure Floyd_Triangle is
  rows : constant Natural := Natural'Value(Ada.Command_Line.Argument(1));
begin
  for r in 1..rows loop
    for i in 1..r loop 
      Ada.Integer_Text_IO.put (r*(r-1)/2+i, Width=> Natural'Image(rows*(rows-1)/2+i)'Length); 
    end loop;
    Ada.Text_IO.New_Line;
  end loop;
end Floyd_Triangle;