Task: Bell numbers





Java: import java.util.ArrayList;
import java.util.List;

public class Bell {
    private static class BellTriangle {
        private List<Integer> arr;

        BellTriangle(int n) {
            int length = n * (n + 1) / 2;
            arr = new ArrayList<>(length);
            for (int i = 0; i < length; ++i) {
                arr.add(0);
            }

            set(1, 0, 1);
            for (int i = 2; i <= n; ++i) {
                set(i, 0, get(i - 1, i - 2));
                for (int j = 1; j < i; ++j) {
                    int value = get(i, j - 1) + get(i - 1, j - 1);
                    set(i, j, value);
                }
            }
        }

        private int index(int row, int col) {
            if (row > 0 && col >= 0 && col < row) {
                return row * (row - 1) / 2 + col;
            } else {
                throw new IllegalArgumentException();
            }
        }

        public int get(int row, int col) {
            int i = index(row, col);
            return arr.get(i);
        }

        public void set(int row, int col, int value) {
            int i = index(row, col);
            arr.set(i, value);
        }
    }

    public static void main(String[] args) {
        final int rows = 15;
        BellTriangle bt = new BellTriangle(rows);

        System.out.println("First fifteen Bell numbers:");
        for (int i = 0; i < rows; ++i) {
            System.out.printf("%2d: %d\n", i + 1, bt.get(i + 1, 0));
        }

        for (int i = 1; i <= 10; ++i) {
            System.out.print(bt.get(i, 0));
            for (int j = 1; j < i; ++j) {
                System.out.printf(", %d", bt.get(i, j));
            }
            System.out.println();
        }
    }
}




VB: Imports System.Numerics
Imports System.Runtime.CompilerServices

Module Module1

    <Extension()>
    Sub Init(Of T)(array As T(), value As T)
        If IsNothing(array) Then Return
        For i = 0 To array.Length - 1
            array(i) = value
        Next
    End Sub

    Function BellTriangle(n As Integer) As BigInteger()()
        Dim tri(n - 1)() As BigInteger
        For i = 0 To n - 1
            Dim temp(i - 1) As BigInteger
            tri(i) = temp
            tri(i).Init(0)
        Next
        tri(1)(0) = 1
        For i = 2 To n - 1
            tri(i)(0) = tri(i - 1)(i - 2)
            For j = 1 To i - 1
                tri(i)(j) = tri(i)(j - 1) + tri(i - 1)(j - 1)
            Next
        Next
        Return tri
    End Function

    Sub Main()
        Dim bt = BellTriangle(51)
        Console.WriteLine("First fifteen Bell numbers:")
        For i = 1 To 15
            Console.WriteLine("{0,2}: {1}", i, bt(i)(0))
        Next
        Console.WriteLine("50: {0}", bt(50)(0))
        Console.WriteLine()
        Console.WriteLine("The first ten rows of Bell
        For i = 1 To 10
            Dim it = bt(i).GetEnumerator()
            Console.Write("[")
            If it.MoveNext() Then
                Console.Write(it.Current)
            End If
            While it.MoveNext()
                Console.Write(", ")
                Console.Write(it.Current)
            End While
            Console.WriteLine("]")
        Next
    End Sub

End Module