logoStarPatterns
Back to Patterns

Pascal's Triangle

Pattern Result

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Code Implementation

Explanation

Pascal's Triangle is a triangular array of numbers where each number is the sum of the two numbers directly above it. The pattern starts with 1 at the top, and each subsequent row begins and ends with 1. The algorithm uses a mathematical formula to calculate each value in the triangle: for each position (i, j), the value is calculated as val = val * (i - j + 1) / j, where val starts as 1 for each row. This formula efficiently computes the binomial coefficients without having to store the entire triangle.