```python
def bell_number(n):
    """
    Calculates the nth Bell number using the Bell triangle.
    The nth Bell number is the number of ways to partition a set with n elements.
    """
    # Initialize the first row of the Bell triangle (for B_0)
    row = [1]

    # Generate n subsequent rows of the triangle
    for _ in range(n):
        # The first element of the new row is the last element of the previous row
        new_row = [row[-1]]
        
        # Each subsequent element is the sum of the element to its left in the new row
        # and the element diagonally above it in the previous row.
        for i in range(len(row)):
            new_row.append(new_row[i] + row[i])
        
        # The new row becomes the current row for the next iteration
        row = new_row
        
    # After n iterations, the first element of the current row is B_n.
    return row[0]

```